about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src
AgeCommit message (Collapse)AuthorLines
2024-12-06Rollup merge of #133211 - Strophox:miri-correct-state-update-ffi, r=RalfJungMatthias Krüger-12/+60
Extend Miri to correctly pass mutable pointers through FFI Based off of https://github.com/rust-lang/rust/pull/129684, this PR further extends Miri to execute native calls that make use of pointers to *mutable* memory. We adapt Miri's bookkeeping of internal state upon any FFI call that gives external code permission to mutate memory. Native code may now possibly write and therefore initialize and change the pointer provenance of bytes it has access to: Such memory is assumed to be *initialized* afterwards and bytes are given *arbitrary (wildcard) provenance*. This enables programs that correctly use mutating FFI calls to run Miri without errors, at the cost of possibly missing Undefined Behaviour caused by incorrect usage of mutating FFI. > <details> > > <summary> Simple example </summary> > > ```rust > extern "C" { > fn init_int(ptr: *mut i32); > } > > fn main() { > let mut x = std::mem::MaybeUninit::<i32>::uninit(); > let x = unsafe { > init_int(x.as_mut_ptr()); > x.assume_init() > }; > > println!("C initialized my memory to: {x}"); > } > ``` > ```c > void init_int(int *ptr) { > *ptr = 42; > } > ``` > should now show `C initialized my memory to: 42`. > > </details> r? ``@RalfJung``
2024-12-05extend Miri to correctly pass mutable pointers through FFIStrophox-12/+60
Co-authored-by: Ralf Jung <post@ralfj.de>
2024-12-03Rollup merge of #133681 - RalfJung:niches, r=wesleywiserMatthias Krüger-21/+29
improve TagEncoding::Niche docs, sanity check, and UB checks Turns out the `niche_variants` range can actually contain the `untagged_variant`. We should report this as UB in Miri, so this PR implements that. Also rename `partially_check_layout` to `layout_sanity_check` for better consistency with how similar functions are called in other parts of the compiler. Turns out my adjustments to the transmutation logic also fix https://github.com/rust-lang/rust/issues/126267.
2024-12-03Bounds-check with PtrMetadata instead of Len in MIRScott McMurray-5/+20
2024-12-03Auto merge of #133321 - compiler-errors:const-checker, r=wesleywiserbors-6/+5
Get rid of HIR const checker As far as I can tell, the HIR const checker was implemented in https://github.com/rust-lang/rust/pull/66170 because we were not able to issue useful const error messages in the MIR const checker. This seems to have changed in the last 5 years, probably due to work like #90532. I've tweaked the diagnostics slightly and think the error messages have gotten *better* in fact. Thus I think the HIR const checker has reached the end of its usefulness, and we can retire it. cc `@RalfJung`
2024-12-01fix ICE when promoted has layout size overflowRalf Jung-12/+16
2024-12-01fix safe-transmute handling of enumsRalf Jung-1/+1
2024-11-30report UB when the niche value refers to the untagged variantRalf Jung-20/+28
2024-11-26Move `always_storage_live_locals`.Nicholas Nethercote-3/+2
It's very closely related to `MaybeStorageLive` and `MaybeStorageDead`. It's weird that it's currently in a different module.
2024-11-23remove is_trivially_const_dropRalf Jung-5/+0
2024-11-22Check drop is trivial before checking ty needs dropMichael Goulet-1/+6
2024-11-22Deduplicate checking drop terminatorMichael Goulet-92/+53
2024-11-22Gate const drop behind const_destruct feature, and fix ↵Michael Goulet-82/+119
const_precise_live_drops post-drop-elaboration check
2024-11-22More comments, reverse polarity of structural checkMichael Goulet-35/+41
2024-11-22Implement ~const Destruct in new solverMichael Goulet-39/+42
2024-11-22Get rid of HIR const checkerMichael Goulet-6/+5
2024-11-20Auto merge of #131326 - dingxiangfei2009:issue-130836-attempt-2, r=nikomatsakisbors-0/+4
Reduce false positives of tail-expr-drop-order from consumed values (attempt #2) r? `@nikomatsakis` Tracked by #123739. Related to #129864 but not replacing, yet. Related to #130836. This is an implementation of the approach suggested in the [Zulip stream](https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/temporary.20drop.20order.20changes). A new MIR statement `BackwardsIncompatibleDrop` is added to the MIR syntax. The lint now works by inspecting possibly live move paths before at the `BackwardsIncompatibleDrop` location and the actual drop under the current edition, which should be one before Edition 2024 in practice.
2024-11-20reduce false positives of tail-expr-drop-order from consumed valuesDing Xiang Fei-0/+4
take 2 open up coroutines tweak the wordings the lint works up until 2021 We were missing one case, for ADTs, which was causing `Result` to yield incorrect results. only include field spans with significant types deduplicate and eliminate field spans switch to emit spans to impl Drops Co-authored-by: Niko Matsakis <nikomat@amazon.com> collect drops instead of taking liveness diff apply some suggestions and add explantory notes small fix on the cache let the query recurse through coroutine new suggestion format with extracted variable name fine-tune the drop span and messages bugfix on runtime borrows tweak message wording filter out ecosystem types earlier apply suggestions clippy check lint level at session level further restrict applicability of the lint translate bid into nop for stable mir detect cycle in type structure
2024-11-20interpret: make typing_env field privateRalf Jung-7/+6
2024-11-19`InterpCx` store `TypingEnv` instead of a `ParamEnv`lcnr-127/+115
2024-11-19move `fn is_item_raw` to `TypingEnv`lcnr-11/+10
2024-11-19Auto merge of #133164 - RalfJung:promoted-oom, r=jieyouxubors-1/+11
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-154/+145
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-1/+11
2024-11-18Rollup merge of #133163 - RalfJung:cold, r=saethlinGuillaume Gomez-3/+0
remove pointless cold_path impl in interpreter This has a fallback impl so the interpreter impl is not needed. r? ``@saethlin``
2024-11-18use `TypingEnv` when no `infcx` is availablelcnr-154/+145
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-18rename rustc_const_stable_intrinsic -> rustc_intrinsic_const_stable_indirectRalf Jung-1/+1
2024-11-17Auto merge of #120370 - x17jiri:likely_unlikely_fix, r=saethlinbors-0/+3
Likely unlikely fix RFC 1131 ( https://github.com/rust-lang/rust/issues/26179 ) added likely/unlikely intrinsics, but they have been broken for a while: https://github.com/rust-lang/rust/issues/96276 , https://github.com/rust-lang/rust/issues/96275 , https://github.com/rust-lang/rust/issues/88767 . This PR tries to fix them. Changes: - added a new `cold_path()` intrinsic - `likely()` and `unlikely()` changed to regular functions implemented using `cold_path()`
2024-11-17Likely unlikely fixJiri Bobek-0/+3
2024-11-16stabilize const_ptr_is_nullRalf Jung-0/+6
2024-11-13check_consts: fix error requesting feature gate when that gate is not ↵Ralf Jung-12/+36
actually needed
2024-11-12allow rustc_private feature in force-unstable-if-unmarked cratesRalf Jung-3/+20
2024-11-10ensure that all publicly reachable const fn have const stability infoRalf Jung-17/+20
2024-11-10honor rustc_const_stable_indirect in non-staged_api crate with ↵Ralf Jung-3/+4
-Zforce-unstable-if-unmarked
2024-11-09Rollup merge of #132823 - RalfJung:conditional-const-calls, ↵Jubilee-78/+74
r=fee1-dead,compiler-errors require const_impl_trait gate for all conditional and trait const calls Alternative to https://github.com/rust-lang/rust/pull/132786. `@compiler-errors` this is basically what I meant with my proposals. I found it's easier to express this in code than English. ;) r? `@compiler-errors`
2024-11-09give a hint which feature is missingRalf Jung-15/+11
2024-11-09require const_impl_trait gate for all conditional and trait const callsRalf Jung-69/+69
2024-11-09make return type of get_alloc_info a struct, and reduce some code ↵Ralf Jung-154/+87
duplication with validity checking
2024-11-09interpret: get_alloc_info: also return mutabilityRalf Jung-16/+27
2024-11-07Rollup merge of #132057 - RalfJung:miri-abi-compat, r=wesleywiserJonas Böttiger-18/+35
miri: update ABI compat checks to accept Option-like types This implements the t-lang decision described [here](https://github.com/rust-lang/rust/pull/130628#issuecomment-2402761599). Fixes https://github.com/rust-lang/miri/issues/3983
2024-11-04when an intrinsic has a const-stable fallback body, we can easily expose it ↵Ralf Jung-4/+12
on stable
2024-11-04add new rustc_const_stable_intrinsic attribute for const-stable intrinsicsRalf Jung-7/+10
2024-11-04remove support for extern-block const intrinsicsRalf Jung-9/+3
2024-11-04miri: update ABI compat checks to accept Option-like typesRalf Jung-18/+35
2024-11-03Rollup merge of #132423 - RalfJung:const-eval-align-offset, r=dtolnayJubilee-82/+6
remove const-support for align_offset and is_aligned As part of the recent discussion to stabilize `ptr.is_null()` in const context, the general vibe was that it's okay for a const function to panic when the same operation would work at runtime (that's just a case of "dynamically detecting that something is not supported as a const operation"), but it is *not* okay for a const function to just return a different result. Following that, `is_aligned` and `is_aligned_to` have their const status revoked in this PR, since they do return actively wrong results at const time. In the future we can consider having a new intrinsic or so that can check whether a pointer is "guaranteed to be aligned", but the current implementation based on `align_offset` does not have the behavior we want. In fact `align_offset` itself behaves quite strangely in const, and that support needs a bunch of special hacks. That doesn't seem worth it. Instead, the users that can fall back to a different implementation should just use const_eval_select directly, and everything else should not be made const-callable. So this PR does exactly that, and entirely removes const support for align_offset. Closes some tracking issues by removing the associated features: Closes https://github.com/rust-lang/rust/issues/90962 Closes https://github.com/rust-lang/rust/issues/104203 Cc `@rust-lang/wg-const-eval` `@rust-lang/libs-api`
2024-11-03Rollup merge of #132574 - workingjubilee:abi-in-compiler, r=compiler-errorsJubilee-32/+29
compiler: Directly use rustc_abi almost everywhere Use rustc_abi instead of rustc_target where applicable. This is mostly described by the following substitutions: ```rust match path_substring { rustc_target::spec::abi::Abi => rustc_abi::ExternAbi, rustc_target::abi::call => rustc_target::callconv, rustc_target::abi => rustc_abi, } ``` A number of spot-fixes make that not quite the whole story. The main exception is in 33edc68 where I get a lot more persnickety about how things are imported, especially in `rustc_middle::ty::layout`, not just from where. This includes putting an end to a reexport of `rustc_middle::ty::ReprOptions`, for the same reason that the rest of this change is happening: reexports mostly confound things. This notably omits rustc_passes and the ast crates, as I'm still examining a question I have about how they do stability checking of `extern "Abi"` strings and if I can simplify their logic. The rustc_abi and rustc_target crates also go untouched because they will be entangled in that cleanup. r? compiler-errors
2024-11-03compiler: Directly use rustc_abi in const_evalJubilee Young-32/+29
2024-11-03Rename the FIXMEs, remove a few that dont matter anymoreMichael Goulet-4/+5
2024-11-03Gate checking ~const bounds on const_trait_implMichael Goulet-9/+3