about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src/interpret
AgeCommit message (Collapse)AuthorLines
2024-11-19Auto merge of #133164 - RalfJung:promoted-oom, r=jieyouxubors-0/+3
interpret: do not ICE when a promoted fails with OOM Fixes https://github.com/rust-lang/rust/issues/130687 try-job: aarch64-apple try-job: dist-x86_64-linux
2024-11-18Auto merge of #132460 - lcnr:questionable-uwu, r=compiler-errorsbors-97/+87
Use `TypingMode` throughout the compiler instead of `ParamEnv` Hopefully the biggest single PR as part of https://github.com/rust-lang/types-team/issues/128. ## `infcx.typing_env` while defining opaque types I don't know how'll be able to correctly handle opaque types when using something taking a `TypingEnv` while defining opaque types. To correctly handle the opaques we need to be able to pass in the current `opaque_type_storage` and return constraints, i.e. we need to use a proper canonical query. We should migrate all the queries used during HIR typeck and borrowck where this matters to proper canonical queries. This is ## `layout_of` and `Reveal::All` We convert the `ParamEnv` to `Reveal::All` right at the start of the `layout_of` query, so I've changed callers of `layout_of` to already use a post analysis `TypingEnv` when encountering it. https://github.com/rust-lang/rust/blob/ca87b535a05097df6abbe2a031b057de2cefac5b/compiler/rustc_ty_utils/src/layout.rs#L51 ## `Ty::is_[unpin|sized|whatever]` I haven't migrated `fn is_item_raw` to use `TypingEnv`, will do so in a followup PR, this should significantly reduce the amount of `typing_env.param_env`. At some point there will probably be zero such uses as using the type system while ignoring the `typing_mode` is incorrect. ## `MirPhase` and phase-transitions When inside of a MIR-body, we can mostly use its `MirPhase` to figure out the right `typing_mode`. This does not work during phase transitions, most notably when transitioning from `Analysis` to `Runtime`: https://github.com/rust-lang/rust/blob/dae7ac133b9eda152784c075facb31a6688c92b1/compiler/rustc_mir_transform/src/lib.rs#L606-L625 All these passes still run with `MirPhase::Analysis`, but we should only use `Reveal::All` once we're run the `RevealAll` pass. This required me to manually construct the right `TypingEnv` in all these passes. Given that it feels somewhat easy to accidentally miss this going forward, I would maybe like to change `Body::phase` to an `Option` and replace it at the start of phase transitions. This then makes it clear that the MIR is currently in a weird state. r? `@ghost`
2024-11-18interpret: do not ICE when a promoted fails with OOMRalf Jung-0/+3
2024-11-18use `TypingEnv` when no `infcx` is availablelcnr-97/+87
the behavior of the type system not only depends on the current assumptions, but also the currentnphase of the compiler. This is mostly necessary as we need to decide whether and how to reveal opaque types. We track this via the `TypingMode`.
2024-11-18remove pointless cold_path impl in interpreterRalf Jung-3/+0
2024-11-17Likely unlikely fixJiri Bobek-0/+3
2024-11-09make return type of get_alloc_info a struct, and reduce some code ↵Ralf Jung-152/+84
duplication with validity checking
2024-11-09interpret: get_alloc_info: also return mutabilityRalf Jung-15/+26
2024-11-04miri: update ABI compat checks to accept Option-like typesRalf Jung-18/+35
2024-11-03compiler: Directly use rustc_abi in const_evalJubilee Young-24/+22
2024-10-31stop using `ParamEnv::reveal` while handling MIRlcnr-13/+36
2024-10-30Rollup merge of #132246 - workingjubilee:campaign-on-irform, r=compiler-errorsJubilee-47/+63
Rename `rustc_abi::Abi` to `BackendRepr` Remove the confabulation of `rustc_abi::Abi` with what "ABI" actually means by renaming it to `BackendRepr`, and rename `Abi::Aggregate` to `BackendRepr::Memory`. The type never actually represented how things are passed, as that has to have `PassMode` considered, at minimum, but rather it just is how we represented some things to the backend. This conflation arose because LLVM, the primary backend at the time, would lower certain IR forms using certain ABIs. Even that only somewhat was true, as it broke down when one ventured significantly afield of what is described by the System V AMD64 ABI either by using different architectures, ABI-modifying IR annotations, the same architecture **with different ISA extensions enabled**, or other... unexpected delights. Unfortunately both names are still somewhat of a misnomer right now, as people have written code for years based on this misunderstanding. Still, their original names are even moreso, and for better or worse, this backend code hasn't received as much maintenance as the rest of the compiler, lately. Actually arriving at a correct end-state will simply require us to disentangle a lot of code in order to fix, much of it pointlessly repeated in several places. Thus this is not an "actual fix", just a way to deflect further misunderstandings.
2024-10-29compiler: `rustc_abi::Abi` => `BackendRepr`Jubilee Young-47/+63
The initial naming of "Abi" was an awful mistake, conveying wrong ideas about how psABIs worked and even more about what the enum meant. It was only meant to represent the way the value would be described to a codegen backend as it was lowered to that intermediate representation. It was never meant to mean anything about the actual psABI handling! The conflation is because LLVM typically will associate a certain form with a certain ABI, but even that does not hold when the special cases that actually exist arise, plus the IR annotations that modify the ABI. Reframe `rustc_abi::Abi` as the `BackendRepr` of the type, and rename `BackendRepr::Aggregate` as `BackendRepr::Memory`. Unfortunately, due to the persistent misunderstandings, this too is now incorrect: - Scattered ABI-relevant code is entangled with BackendRepr - We do not always pre-compute a correct BackendRepr that reflects how we "actually" want this value to be handled, so we leave the backend interface to also inject various special-cases here - In some cases `BackendRepr::Memory` is a "real" aggregate, but in others it is in fact using memory, and in some cases it is a scalar! Our rustc-to-backend lowering code handles this sort of thing right now. That will eventually be addressed by lifting duplicated lowering code to either rustc_codegen_ssa or rustc_target as appropriate.
2024-10-29TypingMode :thinking:lcnr-2/+4
2024-10-28compiler: Add `is_uninhabited` and use LayoutS accessorsJubilee Young-10/+10
This reduces the need of the compiler to peek on the fields of LayoutS.
2024-10-21Auto merge of #130950 - compiler-errors:yeet-eval, r=BoxyUwUbors-1/+3
Continue to get rid of `ty::Const::{try_}eval*` This PR mostly does: * Removes all of the `try_eval_*` and `eval_*` helpers from `ty::Const`, and replace their usages with `try_to_*`. * Remove `ty::Const::eval`. * Rename `ty::Const::normalize` to `ty::Const::normalize_internal`. This function is still used in the normalization code itself. * Fix some weirdness around the `TransmuteFrom` goal. I'm happy to split it out further; for example, I could probably land the first part which removes the helpers, or the changes to codegen which are more obvious than the changes to tools. r? BoxyUwU Part of https://github.com/rust-lang/rust/issues/130704
2024-10-19Get rid of const eval_* and try_eval_* helpersMichael Goulet-1/+3
2024-10-19interpret errors: add map_err_kind, rename InterpError -> InterpErrorKindRalf Jung-25/+27
2024-10-15Rollup merge of #130568 - eduardosm:const-float-methods, r=RalfJung,tgross35Michael Goulet-19/+99
Make some float methods unstable `const fn` Some float methods are now `const fn` under the `const_float_methods` feature gate. I also made some unstable methods `const fn`, keeping their constness under their respective feature gate. In order to support `min`, `max`, `abs` and `copysign`, the implementation of some intrinsics had to be moved from Miri to rustc_const_eval (cc `@RalfJung).` Tracking issue: https://github.com/rust-lang/rust/issues/130843 ```rust impl <float> { // #[feature(const_float_methods)] pub const fn recip(self) -> Self; pub const fn to_degrees(self) -> Self; pub const fn to_radians(self) -> Self; pub const fn max(self, other: Self) -> Self; pub const fn min(self, other: Self) -> Self; pub const fn clamp(self, min: Self, max: Self) -> Self; pub const fn abs(self) -> Self; pub const fn signum(self) -> Self; pub const fn copysign(self, sign: Self) -> Self; // #[feature(float_minimum_maximum)] pub const fn maximum(self, other: Self) -> Self; pub const fn minimum(self, other: Self) -> Self; // Only f16/f128 (f32/f64 already const) pub const fn is_sign_positive(self) -> bool; pub const fn is_sign_negative(self) -> bool; pub const fn next_up(self) -> Self; pub const fn next_down(self) -> Self; } ``` r? libs-api try-job: dist-s390x-linux
2024-10-15Make some float methods unstable `const fn`Eduardo Sánchez Muñoz-0/+80
Some float methods are now `const fn` under the `const_float_methods` feature gate. In order to support `min`, `max`, `abs` and `copysign`, the implementation of some intrinsics had to be moved from Miri to rustc_const_eval.
2024-10-14De-duplicate and move `adjust_nan` to `InterpCx`Eduardo Sánchez Muñoz-19/+19
2024-10-12miri: avoid cloning AllocExtraRalf Jung-13/+18
2024-10-08compiler: Factor rustc_target::abi out of const_evalJubilee Young-7/+8
2024-10-07Auto merge of #131068 - RalfJung:immediate-offset-sanity-check, r=nnethercotebors-31/+45
Don't use Immediate::offset to transmute pointers to integers This applies the relatively new `assert_matches_abi` check in the `offset` operation on immediates, which makes sure that if offsets are used to alter the layout (which is possible because the field layout is arbitrarily picked by the caller), this is not done in a way that breaks the invariant of the `Immediate` type. This leads to ICEs in a GVN mir-opt test, so the second commit fixes GVN. Fixes https://github.com/rust-lang/rust/issues/131064.
2024-10-06various fixes for `naked_asm!` implementationFolkert de Vries-1/+1
- fix for divergence - fix error message - fix another cranelift test - fix some cranelift things - don't set the NORETURN option for naked asm - fix use of naked_asm! in doc comment - fix use of naked_asm! in run-make test - use `span_bug` in unreachable branch
2024-10-04Use wide pointers consistenly across the compilerUrgau-3/+3
2024-10-03interpret: Immediate::offset: use shared sanity-check function to ensure ↵Ralf Jung-31/+45
invariant
2024-10-02Auto merge of #131006 - RalfJung:immediate-sanity, r=saethlinbors-3/+6
interpret: always enable write_immediate sanity checks Writing a wrongly-sized scalar somewhere can have quite confusing effects. Let's see how expensive it is to catch this early.
2024-10-01make InterpResult a dedicated type to avoid accidentally discarding the errorRalf Jung-413/+423
2024-09-30panic when an interpreter error gets unintentionally discardedRalf Jung-23/+34
2024-09-29interpret: always enable write_immediate sanity checksRalf Jung-3/+6
2024-09-29cleanup: don't `.into()` identical typesMatthias Krüger-5/+2
2024-09-27Rollup merge of #130826 - fmease:compiler-mv-obj-safe-dyn-compat, ↵Matthias Krüger-1/+1
r=compiler-errors Compiler: Rename "object safe" to "dyn compatible" Completed T-lang FCP: https://github.com/rust-lang/lang-team/issues/286#issuecomment-2338905118. Tracking issue: https://github.com/rust-lang/rust/issues/130852 Excludes `compiler/rustc_codegen_cranelift` (to be filed separately). Includes Stable MIR. Regarding https://github.com/rust-lang/rust/labels/relnotes, I guess I will manually open a https://github.com/rust-lang/rust/labels/relnotes-tracking-issue since this change affects everything (compiler, library, tools, docs, books, everyday language). r? ghost
2024-09-25Compiler: Rename "object safe" to "dyn compatible"León Orell Valerian Liehr-1/+1
2024-09-24be even more precise about "cast" vs "coercion"Lukas Markeffsky-5/+6
2024-09-24unify dyn* coercions with other pointer coercionsLukas Markeffsky-1/+1
2024-09-23Rollup merge of #130727 - compiler-errors:objects, r=RalfJungMichael Goulet-35/+60
Check vtable projections for validity in miri Currently, miri does not catch when we transmute `dyn Trait<Assoc = A>` to `dyn Trait<Assoc = B>`. This PR implements such a check, and fixes https://github.com/rust-lang/miri/issues/3905. To do this, we modify `GlobalAlloc::VTable` to contain the *whole* list of `PolyExistentialPredicate`, and then modify `check_vtable_for_type` to validate the `PolyExistentialProjection`s of the vtable, along with the principal trait that was already being validated. cc ``@RalfJung`` r? ``@lcnr`` or types I also tweaked the diagnostics a bit. --- **Open question:** We don't validate the auto traits. You can transmute `dyn Foo` into `dyn Foo + Send`. Should we check that? We currently have a test that *exercises* this as not being UB: https://github.com/rust-lang/rust/blob/6c6d210089e4589afee37271862b9f88ba1d7755/src/tools/miri/tests/pass/dyn-upcast.rs#L14-L20 I'm not actually sure if we ever decided that's actually UB or not 🤔 We could perhaps still check that the underlying type of the object (i.e. the concrete type that was unsized) implements the auto traits, to catch UB like: ```rust fn main() { let x: &dyn Trait = &std::ptr::null_mut::<()>(); let _: &(dyn Trait + Send) = std::mem::transmute(x); //~^ this vtable is not allocated for a type that is `Send`! } ```
2024-09-23Check vtable projections for validity in miriMichael Goulet-35/+60
2024-09-23fix unqualified_local_imports in rustc_const_evalRalf Jung-1/+1
2024-09-22Reformat using the new identifier sorting from rustfmtMichael Goulet-84/+79
2024-09-22Rollup merge of #130690 - RalfJung:interpret-abi-compat-fixme, r=jieyouxuGuillaume Gomez-1/+0
interpret: remove outdated FIXME The rule about `repr(C)` types with compatible fields got removed from the ABI compat docs before they landed, so this FIXME here is no longer correct. (So this is basically a follow-up to https://github.com/rust-lang/rust/pull/130185, doing some more cleanup around deciding not to guarantee ABI compatibility for structurally compatible `repr(C)` types.)
2024-09-22fix rustc_nonnull_optimization_guaranteed docsRalf Jung-0/+1
2024-09-22interpret: remove outdated FIXMERalf Jung-1/+0
2024-09-16layout computation: eagerly error for unexpected unsized fieldsLukas Markeffsky-1/+1
2024-09-16make `LayoutCx` not genericLukas Markeffsky-2/+2
2024-09-15const: don't ICE when encountering a mutable ref to immutable memoryRalf Jung-8/+2
2024-09-15Rollup merge of #130342 - RalfJung:slice-idx-overflow, r=saethlinMatthias Krüger-14/+34
interpret, miri: fix dealing with overflow during slice indexing and allocation This is mostly to fix https://github.com/rust-lang/rust/issues/130284. I then realized we're using somewhat sketchy arguments for a similar multiplication in `copy`/`copy_nonoverlapping`/`write_bytes`, so I made them all share the same function that checks exactly the right thing. (The intrinsics would previously fail on allocations larger than `1 << 47` bytes... which are theoretically possible maybe? Anyway it seems conceptually wrong to use any other bound than `isize::MAX` here.)
2024-09-15Rollup merge of #129828 - RalfJung:miri-data-race, r=saethlinMatthias Krüger-11/+51
miri: treat non-memory local variables properly for data race detection Fixes https://github.com/rust-lang/miri/issues/3242 Miri has an optimization where some local variables are not represented in memory until something forces them to be stored in memory (most notably, creating a pointer/reference to the local will do that). However, for a subsystem triggering on memory accesses -- such as the data race detector -- this means that the memory access seems to happen only when the local is moved to memory, instead of at the time that it actually happens. This can lead to UB reports in programs that do not actually have UB. This PR fixes that by adding machine hooks for reads and writes to such efficiently represented local variables. The data race system tracks those very similar to how it would track reads and writes to addressable memory, and when a local is moved to memory, the clocks get overwritten with the information stored for the local.
2024-09-15interpret: get_ptr_alloc_mut: lookup allocation only onceRalf Jung-24/+40
2024-09-15also use compute_size_in_bytes for relevant multiplications in MiriRalf Jung-9/+11