about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src/interpret
AgeCommit message (Collapse)AuthorLines
2024-09-14Auto merge of #128543 - RalfJung:const-interior-mut, r=fee1-deadbors-22/+45
const-eval interning: accept interior mutable pointers in final value …but keep rejecting mutable references This fixes https://github.com/rust-lang/rust/issues/121610 by no longer firing the lint when there is a pointer with interior mutability in the final value of the constant. On stable, such pointers can be created with code like: ```rust pub enum JsValue { Undefined, Object(Cell<bool>), } impl Drop for JsValue { fn drop(&mut self) {} } // This does *not* get promoted since `JsValue` has a destructor. // However, the outer scope rule applies, still giving this 'static lifetime. const UNDEFINED: &JsValue = &JsValue::Undefined; ``` It's not great to accept such values since people *might* think that it is legal to mutate them with unsafe code. (This is related to how "infectious" `UnsafeCell` is, which is a [wide open question](https://github.com/rust-lang/unsafe-code-guidelines/issues/236).) However, we [explicitly document](https://doc.rust-lang.org/reference/behavior-considered-undefined.html) that things created by `const` are immutable. Furthermore, we also accept the following even more questionable code without any lint today: ```rust let x: &'static Option<Cell<i32>> = &None; ``` This is even more questionable since it does *not* involve a `const`, and yet still puts the data into immutable memory. We could view this as promotion [potentially introducing UB](https://github.com/rust-lang/unsafe-code-guidelines/issues/493). However, we've accepted this since ~forever and it's [too late to reject this now](https://github.com/rust-lang/rust/pull/122789); the pattern is just too useful. So basically, if you think that `UnsafeCell` should be tracked fully precisely, then you should want the lint we currently emit to be removed, which this PR does. If you think `UnsafeCell` should "infect" surrounding `enum`s, the big problem is really https://github.com/rust-lang/unsafe-code-guidelines/issues/493 which does not trigger the lint -- the cases the lint triggers on are actually the "harmless" ones as there is an explicit surrounding `const` explaining why things end up being immutable. What all this goes to show is that the hard error added in https://github.com/rust-lang/rust/pull/118324 (later turned into the future-compat lint that I am now suggesting we remove) was based on some wrong assumptions, at least insofar as it concerns shared references. Furthermore, that lint does not help at all for the most problematic case here where the potential UB is completely implicit. (In fact, the lint is actively in the way of [my preferred long-term strategy](https://github.com/rust-lang/unsafe-code-guidelines/issues/493#issuecomment-2028674105) for dealing with this UB.) So I think we should go back to square one and remove that error/lint for shared references. For mutable references, it does seem to work as intended, so we can keep it. Here it serves as a safety net in case the static checks that try to contain mutable references to the inside of a const initializer are not working as intended; I therefore made the check ICE to encourage users to tell us if that safety net is triggered. Closes https://github.com/rust-lang/rust/issues/122153 by removing the lint. Cc `@rust-lang/opsem` `@rust-lang/lang`
2024-09-14Rollup merge of #130294 - nnethercote:more-lifetimes, r=lcnrLeón Orell Valerian Liehr-5/+5
Lifetime cleanups The last commit is very opinionated, let's see how we go. r? `@oli-obk`
2024-09-14interpret: fix dealing with overflow during slice indexingRalf Jung-7/+25
2024-09-13interpret: simplify SIMD type handlingRalf Jung-52/+40
2024-09-13Rename and reorder lots of lifetimes.Nicholas Nethercote-5/+5
- Replace non-standard names like 's, 'p, 'rg, 'ck, 'parent, 'this, and 'me with vanilla 'a. These are cases where the original name isn't really any more informative than 'a. - Replace names like 'cx, 'mir, and 'body with vanilla 'a when the lifetime applies to multiple fields and so the original lifetime name isn't really accurate. - Put 'tcx last in lifetime lists, and 'a before 'b.
2024-09-12Rollup merge of #130250 - compiler-errors:useless-conversion, r=jieyouxuStuart Cook-1/+1
Fix `clippy::useless_conversion` Self-explanatory. Probably the last clippy change I'll actually put up since this is the only other one I've actually seen in the wild.
2024-09-12Rollup merge of #130235 - compiler-errors:nested-if, r=michaelwoeristerStuart Cook-7/+15
Simplify some nested `if` statements Applies some but not all instances of `clippy::collapsible_if`. Some ended up looking worse afterwards, though, so I left those out. Also applies instances of `clippy::collapsible_else_if` Review with whitespace disabled please.
2024-09-11Rollup merge of #130239 - RalfJung:miri-ptr-offset-unsigned, r=compiler-errorsJubilee-1/+8
miri: fix overflow detection for unsigned pointer offset This is the Miri part of https://github.com/rust-lang/rust/pull/130229. This is already UB in codegen so we better make Miri detect it; updating the docs may take time if we have to follow some approval process, but let's make Miri match reality ASAP. r? ``@scottmcm``
2024-09-11Rollup merge of #130114 - eduardosm:needless-returns, r=compiler-errorsJubilee-2/+2
Remove needless returns detected by clippy in the compiler
2024-09-11clippy::useless_conversionMichael Goulet-1/+1
2024-09-11miri: fix overflow detection for unsigned pointer offsetRalf Jung-1/+8
2024-09-11Simplify some nested if statementsMichael Goulet-7/+15
2024-09-10interpret: mark some hot functions inline(always)Ralf Jung-0/+5
recovers some of the perf regressions from #129778
2024-09-10turn errors that should be impossible due to our static checks into ICEsRalf Jung-21/+40
2024-09-10const-eval interning: accpt interior mutable pointers in final value (but ↵Ralf Jung-7/+11
keep rejecting mutable references)
2024-09-10miri: treat non-memory local variables properly for data race detectionRalf Jung-11/+51
2024-09-09union padding computation: add fast-path for ZSTRalf Jung-12/+27
Also avoid even tracking empty ranges, and add fast-path for arrays of scalars
2024-09-09Remove needless returns detected by clippy in the compilerEduardo Sánchez Muñoz-2/+2
2024-09-08interpret: reset padding during validationRalf Jung-29/+312
2024-09-08interpret: reset provenance on typed copiesRalf Jung-126/+254
2024-09-08interpret: factor out common code for place mutationRalf Jung-81/+89
2024-09-08interpret: make Writeable trait about a to_place operationRalf Jung-18/+13
2024-09-08interpret: remove Readable trait, we can use Projectable insteadRalf Jung-43/+17
2024-09-06Make `Ty::boxed_ty` return an `Option`Pavel Grigorenko-1/+1
2024-09-03Auto merge of #129777 - nnethercote:unreachable_pub-4, r=Urgaubors-2/+2
Add `unreachable_pub`, round 4 A follow-up to #129732. r? `@Urgau`
2024-09-03Add `warn(unreachable_pub)` to `rustc_const_eval`.Nicholas Nethercote-2/+2
2024-09-02chore: Fix typos in 'compiler' (batch 1)Alexander Cyon-6/+6
2024-08-31Auto merge of #129831 - matthiaskrgr:rollup-befq6zx, r=matthiaskrgrbors-11/+1
Rollup of 11 pull requests Successful merges: - #128523 (Add release notes for 1.81.0) - #129605 (Add missing `needs-llvm-components` directives for run-make tests that need target-specific codegen) - #129650 (Clean up `library/profiler_builtins/build.rs`) - #129651 (skip stage 0 target check if `BOOTSTRAP_SKIP_TARGET_SANITY` is set) - #129684 (Enable Miri to pass pointers through FFI) - #129762 (Update the `wasm-component-ld` binary dependency) - #129782 (couple more crash tests) - #129816 (tidy: say which feature gate has a stability issue mismatch) - #129818 (make the const-unstable-in-stable error more clear) - #129824 (Fix code examples buttons not appearing on click on mobile) - #129826 (library: Fix typo in `core::mem`) r? `@ghost` `@rustbot` modify labels: rollup
2024-08-31Rollup merge of #129684 - Strophox:miri-pass-pointer-to-ffi, r=RalfJungMatthias Krüger-11/+1
Enable Miri to pass pointers through FFI Following https://github.com/rust-lang/rust/pull/126787, the purpose of this PR is to now enable Miri to execute native calls that make use of pointers. > <details> > > <summary> Simple example </summary> > > ```rust > extern "C" { > fn ptr_printer(ptr: *mut i32); > } > > fn main() { > let ptr = &mut 42 as *mut i32; > unsafe { > ptr_printer(ptr); > } > } > ``` > ```c > void ptr_printer(int *ptr) { > printf("printing pointer dereference from C: %d\n", *ptr); > } > ``` > should now show `printing pointer dereference from C: 42`. > > </details> Note that this PR does not yet implement any logic involved in updating Miri's "analysis" state (byte initialization, provenance) upon such a native call. r? ``@RalfJung``
2024-08-31Rollup merge of #129812 - RalfJung:box-custom-alloc, r=compiler-errorsMatthias Krüger-5/+7
interpret, codegen: tweak some comments and checks regarding Box with custom allocator Cc https://github.com/rust-lang/rust/issues/95453
2024-08-31interpret, codegen: tweak some comments and checks regarding Box with custom ↵Ralf Jung-5/+7
allocator
2024-08-30enable Miri to pass const pointers through FFIStrophox-11/+1
Co-authored-by: Ralf Jung <post@ralfj.de>
2024-08-29interpret/visitor: make memory order iteration slightly more efficientRalf Jung-9/+10
2024-08-28Rollup merge of #129666 - RalfJung:raw-eq-align, r=compiler-errorsMatthias Krüger-5/+5
interpret: add missing alignment check in raw_eq The intrinsic requires alignment, but we forgot to check for that in Miri and const-eval.
2024-08-28Rollup merge of #129613 - RalfJung:interpret-target-feat, r=saethlinMatthias Krüger-42/+24
interpret: do not make const-eval query result depend on tcx.sess The check against calling functions with missing target features uses `tcx.sess` to determine which target features are available. However, this can differ between different crates in a crate graph, so the same const-eval query can come to different conclusions about whether a constant evaluates successfully or not -- which is bad, we should consistently get the same result everywhere.
2024-08-28Rollup merge of #129608 - RalfJung:const-eval-ub-checks, r=saethlinMatthias Krüger-1/+11
const-eval: do not make UbChecks behavior depend on current crate's flags Fixes https://github.com/rust-lang/rust/issues/129552 Let's see if we can get away with just always enabling these checks.
2024-08-27Auto merge of #128506 - compiler-errors:by-move-body, r=cjgillotbors-1/+0
Stop storing a special inner body for the coroutine by-move body for async closures ...and instead, just synthesize an item which is treated mostly normally by the MIR pipeline. This PR does a few things: * We synthesize a new `DefId` for the by-move body of a closure, which has its `mir_built` fed with the output of the `ByMoveBody` MIR transformation, and some other relevant queries. * This has the `DefKind::ByMoveBody`, which we use to distinguish it from "real" bodies (that come from HIR) which need to be borrowck'd. Introduce `TyCtxt::is_synthetic_mir` to skip over `mir_borrowck` which is called by `mir_promoted`; borrowck isn't really possible to make work ATM since it heavily relies being called on a body generated from HIR, and is redundant by the construction of the by-move-body. * Remove the special `PassManager` hacks for handling the inner `by_move_body` stored within the coroutine's mir body. Instead, this body is fed like a regular MIR body, so it's goes through all of the `tcx.*_mir` stages normally (build -> promoted -> ...etc... -> optimized) ✨. * Remove the `InstanceKind::ByMoveBody` shim, since now we have a "regular" def id, we can just use `InstanceKind::Item`. This also allows us to remove the corresponding hacks from codegen, such as in `fn_sig_for_fn_abi` ✨. Notable remarks: * ~~I know it's kind of weird to be using `DefKind::Closure` here, since it's not a distinct closure but just a new MIR body. I don't believe it really matters, but I could also use a different `DefKind`... maybe one that we could use for synthetic MIR bodies in general?~~ edit: We're doing this now.
2024-08-27interpret: add missing alignment check in raw_eqRalf Jung-5/+5
2024-08-27Rollup merge of #128942 - RalfJung:interpret-weak-memory, r=saethlinTrevor Gross-1/+1
miri weak memory emulation: put previous value into initial store buffer Fixes https://github.com/rust-lang/miri/issues/2164 by doing a read before each atomic write so that we can initialize the store buffer. The read suppresses memory access hooks and UB exceptions, to avoid otherwise influencing the program behavior. If the read fails, we store that as `None` in the store buffer, so that when an atomic read races with the first atomic write to some memory and previously the memory was uninitialized, we can report UB due to reading uninit memory. ``@cbeuw`` this changes a bit the way we initialize the store buffers. Not sure if you still remember all this code, but if you could have a look to make sure this still makes sense, that would be great. :) r? ``@saethlin``
2024-08-26Stop using a special inner body for the coroutine by-move body for async ↵Michael Goulet-1/+0
closures
2024-08-26interpret: do not make const-eval query result depend on tcx.sessRalf Jung-42/+24
2024-08-26const-eval: do not make UbChecks behavior depend on current crate's flagsRalf Jung-1/+11
2024-08-25Avoid taking reference of &TyKindMichael Goulet-3/+3
2024-08-24interpret: ImmTy: tighten sanity checks in offset logicRalf Jung-6/+15
2024-08-18rename AddressOf -> RawBorrow inside the compilerRalf Jung-1/+1
2024-08-14Auto merge of #128812 - nnethercote:shrink-TyKind-FnPtr, r=compiler-errorsbors-6/+6
Shrink `TyKind::FnPtr`. By splitting the `FnSig` within `TyKind::FnPtr` into `FnSigTys` and `FnHeader`, which can be packed more efficiently. This reduces the size of the hot `TyKind` type from 32 bytes to 24 bytes on 64-bit platforms. This reduces peak memory usage by a few percent on some benchmarks. It also reduces cache misses and page faults similarly, though this doesn't translate to clear cycles or wall-time improvements on CI. r? `@compiler-errors`
2024-08-13Auto merge of #128742 - RalfJung:miri-vtable-uniqueness, r=saethlinbors-4/+32
miri: make vtable addresses not globally unique Miri currently gives vtables a unique global address. That's not actually matching reality though. So this PR enables Miri to generate different addresses for the same type-trait pair. To avoid generating an unbounded number of `AllocId` (and consuming unbounded amounts of memory), we use the "salt" technique that we also already use for giving constants non-unique addresses: the cache is keyed on a "salt" value n top of the actually relevant key, and Miri picks a random salt (currently in the range `0..16`) each time it needs to choose an `AllocId` for one of these globals -- that means we'll get up to 16 different addresses for each vtable. The salt scheme is integrated into the global allocation deduplication logic in `tcx`, and also used for functions and string literals. (So this also fixes the problem that casting the same function to a fn ptr over and over will consume unbounded memory.) r? `@saethlin` Fixes https://github.com/rust-lang/miri/issues/3737
2024-08-12miri weak memory emulation: initialize store buffer only on atomic writes; ↵Ralf Jung-1/+1
pre-fill with previous value
2024-08-11Use assert_matches around the compilerMichael Goulet-5/+8
2024-08-09interpret: make identity upcasts a NOP again to avoid them generating a new ↵Ralf Jung-0/+6
random vtable