about summary refs log tree commit diff
path: root/compiler/rustc_const_eval/src
AgeCommit message (Collapse)AuthorLines
2025-06-27Rollup merge of #143051 - Stypox:tracing-validity, r=RalfJungGuillaume Gomez-4/+11
Add tracing to `validate_operand` This PR adds a tracing call to keep track of how much time is spent in `validate_operand` and `const_validate_operand`. Let me know if more fine-grained tracing is needed (e.g. adding tracing to `validate_operand_internal` too, which is just called from those two functions). I also fixed the rustdoc of `validate_operand` and `const_validate_operand` since it was referencing an older name for the `val` parameter which was renamed in cbdcbf0d6a586792c5e0a0b8965a3179bac56120. Here is some tracing output when Miri is run on `src/tools/miri/tests/pass/hello.rs`, visualizable in [ui.perfetto.dev](https://ui.perfetto.dev/): [trace-1750932222218210.json](https://github.com/user-attachments/files/20924000/trace-1750932222218210.json) **Note: obtaining tracing output depends on https://github.com/rust-lang/miri/pull/4406, but this PR is standalone and can be merged without waiting for https://github.com/rust-lang/miri/pull/4406.** r? `@RalfJung`
2025-06-27const-eval: error when initializing a static writes to that staticRalf Jung-18/+40
2025-06-27Add InterpCx::layout_of with tracing, shadowing LayoutOfStypox-17/+28
2025-06-27Insert checks for enum discriminants when debug assertions are enabledBastian Kersting-0/+1
Similar to the existing nullpointer and alignment checks, this checks for valid enum discriminants on creation of enums through unsafe transmutes. Essentially this sanitizes patterns like the following: ```rust let val: MyEnum = unsafe { std::mem::transmute<u32, MyEnum>(42) }; ``` An extension of this check will be done in a follow-up that explicitly sanitizes for extern enum values that come into Rust from e.g. C/C++. This check is similar to Miri's capabilities of checking for valid construction of enum values. This PR is inspired by saethlin@'s PR https://github.com/rust-lang/rust/pull/104862. Thank you so much for keeping this code up and the detailed comments! I also pair-programmed large parts of this together with vabr-g@.
2025-06-27Add tracing for instantiate_from_frame_and_normalize_erasing_regionsStypox-1/+7
2025-06-27Add tracing to validate_operandStypox-0/+7
2025-06-27Auto merge of #143074 - compiler-errors:rollup-cv64hdh, r=compiler-errorsbors-92/+34
Rollup of 18 pull requests Successful merges: - rust-lang/rust#137843 (make RefCell unstably const) - rust-lang/rust#140942 (const-eval: allow constants to refer to mutable/external memory, but reject such constants as patterns) - rust-lang/rust#142549 (small iter.intersperse.fold() optimization) - rust-lang/rust#142637 (Remove some glob imports from the type system) - rust-lang/rust#142647 ([perf] Compute hard errors without diagnostics in impl_intersection_has_impossible_obligation) - rust-lang/rust#142700 (Remove incorrect comments in `Weak`) - rust-lang/rust#142927 (Add note to `find_const_ty_from_env`) - rust-lang/rust#142967 (Fix RwLock::try_write documentation for WouldBlock condition) - rust-lang/rust#142986 (Port `#[export_name]` to the new attribute parsing infrastructure) - rust-lang/rust#143001 (Rename run always ) - rust-lang/rust#143010 (Update `browser-ui-test` version to `0.20.7`) - rust-lang/rust#143015 (Add `sym::macro_pin` diagnostic item for `core::pin::pin!()`) - rust-lang/rust#143033 (Expand const-stabilized API links in relnotes) - rust-lang/rust#143041 (Remove cache for citool) - rust-lang/rust#143056 (Move an ACE test out of the GCI directory) - rust-lang/rust#143059 (Fix 1.88 relnotes) - rust-lang/rust#143067 (Tracking issue number for `iter_macro`) - rust-lang/rust#143073 (Fix some fixmes that were waiting for let chains) Failed merges: - rust-lang/rust#143020 (codegen_fn_attrs: make comment more precise) r? `@ghost` `@rustbot` modify labels: rollup
2025-06-26clarify and unify 'transient mutable borrow' errorsRalf Jung-34/+12
2025-06-26const-eval: allow constants to refer to mutable/external memory, but reject ↵Ralf Jung-58/+22
such constants as patterns
2025-06-26Rollup merge of #141311 - folkertdev:tidy-natural-sort, r=jieyouxuMatthias Krüger-3/+3
make `tidy-alphabetical` use a natural sort The idea here is that these lines should be correctly sorted, even though a naive string comparison would say they are not: ``` foo2 foo10 ``` This is the ["natural sort order"](https://en.wikipedia.org/wiki/Natural_sort_order). There is more discussion in [#t-compiler/help > tidy natural sort](https://rust-lang.zulipchat.com/#narrow/channel/182449-t-compiler.2Fhelp/topic/tidy.20natural.20sort/with/519111079) Unfortunately, no standard sorting tools are smart enough to to this automatically (casting some doubt on whether we should make this change). Here are some sort outputs: ``` > cat foo.txt | sort foo foo1 foo10 foo2 mp mp1e2 np", np1e2", > cat foo.txt | sort -n foo foo1 foo10 foo2 mp mp1e2 np", np1e2", > cat foo.txt | sort -V foo foo1 foo2 foo10 mp mp1e2 np1e2", np", ``` Disappointingly, "numeric" sort does not actually have the behavior we want. It only sorts by numeric value if the line starts with a number. The "version" sort looks promising, but does something very unintuitive if you look at the final 4 values. None of the other options seem to have the desired behavior in all cases: ``` -b, --ignore-leading-blanks ignore leading blanks -d, --dictionary-order consider only blanks and alphanumeric characters -f, --ignore-case fold lower case to upper case characters -g, --general-numeric-sort compare according to general numerical value -i, --ignore-nonprinting consider only printable characters -M, --month-sort compare (unknown) < 'JAN' < ... < 'DEC' -h, --human-numeric-sort compare human readable numbers (e.g., 2K 1G) -n, --numeric-sort compare according to string numerical value -R, --random-sort shuffle, but group identical keys. See shuf(1) --random-source=FILE get random bytes from FILE -r, --reverse reverse the result of comparisons --sort=WORD sort according to WORD: general-numeric -g, human-numeric -h, month -M, numeric -n, random -R, version -V -V, --version-sort natural sort of (version) numbers within text ``` r? ```@Noratrieb``` (it sounded like you know this code?)
2025-06-26Change const trait bound syntax from ~const to [const]Oli Scherer-8/+8
2025-06-26Fix documentation of validate_operandStypox-4/+4
The name of the parameter changed from `op` to `val` in cbdcbf0d6a586792c5e0a0b8965a3179bac56120
2025-06-26make size_and_align_of_mplace work on all projectableRalf Jung-22/+16
2025-06-26const validation: properly ignore zero-sized UnsafeCellRalf Jung-3/+14
2025-06-25make `tidy-alphabetical` use a natural sortFolkert de Vries-3/+3
2025-06-25Rollup merge of #142724 - xizheyin:avoid_overwrite_args, r=oli-obkJana Dönszelmann-0/+3
Add runtime check to avoid overwrite arg in `Diag` ## Origin PR description At first, I set up a `debug_assert` check for the arg method to make sure that `args` in `Diag` aren't easily overwritten, and I added the `remove_arg()` method, so that if you do need to overwrite an arg, then you can explicitly call `remove_arg()` to remove it first, then call `arg()` to overwrite it. For the code before the rust-lang/rust#142015 change, it won't compile because it will report an error ``` arg `instance`already exists. ``` This PR also modifies all diagnostics that fail the check to pass the check. There are two cases of check failure: 1. ~~Between *the parent diagnostic and the subdiagnostic*, or *between the subdiagnostics* have the same field between them. In this case, I renamed the conflicting fields.~~ 2. ~~For subdiagnostics stored in `Vec`, the rendering may iteratively write the same arg over and over again. In this case, I changed the auto-generation with `derive(SubDiagnostic)` to manually implementing `SubDiagnostic` and manually rendered it with `eagerly_translate()`, similar to https://github.com/rust-lang/rust/issues/142031#issuecomment-2984812090, and after rendering it I manually deleted useless arg with the newly added `remove_arg` method.~~ ## Final Decision After trying and discussing, we made a final decision. For `#[derive(Subdiagnostic)]`, This PR made two changes: 1. After the subdiagnostic is rendered, remove all args of this subdiagnostic, which allows for usage like `Vec<Subdiag>`. 2. Store `diag.args` before setting arguments, so that you can restore the contents of the main diagnostic after deleting the arguments after subdiagnostic is rendered, to avoid deleting the main diagnostic's arg when they have the same name args.
2025-06-25Add runtime check to avoid overwrite arg easily in diag and store and ↵xizheyin-0/+3
restore snapshot when set subdiag arg Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-06-24Rollup merge of #135731 - frank-king:feature/pin-borrow, r=eholk,traviscrossJubilee-5/+7
Implement parsing of pinned borrows This PR implements part of #130494. EDIT: It introduces `&pin mut $place` and `&pin const $place` as sugars for `std::pin::pin!($place)` and its shared reference equivalent, except that `$place` will not be moved when borrowing. The borrow check will be in charge of enforcing places cannot be moved or mutably borrowed since being pinned till dropped. ### Implementation steps: - [x] parse the `&pin mut $place` and `&pin const $place` syntaxes - [ ] borrowck of `&pin mut|const` - [ ] support autoref of `&pin mut|const` when needed
2025-06-22centralize `-Zmin-function-alignment` logicFolkert de Vries-6/+1
2025-06-20Rollup merge of #142575 - oli-obk:sneaky-self-init, r=RalfJungTrevor Gross-1/+6
Ensure copy* intrinsics also perform the static self-init checks fixes rust-lang/rust#142532 r? `@RalfJung`
2025-06-20Ensure copy* intrinsics also perform the static self-init checksOli Scherer-1/+6
2025-06-19Do not include NUL-terminator in computed lengthAlice Ryhl-7/+5
2025-06-18Make feature suggestion more consistent.Camille GILLOT-30/+4
2025-06-15Implement pinned borrows, part of `pin_ergonomics`Frank King-5/+7
2025-06-13Rollup merge of #142405 - oli-obk:type-once, r=RalfJungJubilee-7/+7
Don't hardcode the intrinsic return types twice in the compiler We already manually check intrinsic types in intrinsicck so we don't need to do it in the interpreter
2025-06-12intrinsics: rename min_align_of to align_ofRalf Jung-2/+2
2025-06-12Don't hardcode the intrinsic return types twice in the compilerOli Scherer-7/+7
2025-06-11Fix enter_trace_span!() using wrong $crate pathsStypox-2/+2
2025-06-10const_eval: fix some outdated commentsRalf Jung-12/+8
2025-06-09Rollup merge of #142222 - compiler-errors:ocx-w-no-diag, r=lcnrMatthias Krüger-1/+1
Dont make `ObligationCtxt`s with diagnostics unnecessarily just a nit, shouldn't affect perf b/c `ObligationCtxt::new_with_diagnostics` should only be more expensive in the new trait solver, and I don't expect either of these to encounter errors today anyways. r? oli-obk
2025-06-08Dont make ObligationCtxts with diagnostics unnecessarilyMichael Goulet-1/+1
2025-06-09Auto merge of #142220 - workingjubilee:rollup-idgfpof, r=workingjubileebors-13/+2
Rollup of 12 pull requests Successful merges: - rust-lang/rust#141803 (Remove rustc's notion of "preferred" alignment AKA `__alignof`) - rust-lang/rust#142053 (Add new Tier-3 targets: `loongarch32-unknown-none*`) - rust-lang/rust#142089 (Replace all uses of sysroot_candidates with get_or_default_sysroot) - rust-lang/rust#142108 (compiler: Add track_caller to AbiMapping::unwrap) - rust-lang/rust#142132 (`tests/ui`: A New Order [6/N]) - rust-lang/rust#142162 (UnsafePinned: update get() docs and signature to allow shared mutation) - rust-lang/rust#142171 (`tests/ui`: A New Order [7/N]) - rust-lang/rust#142179 (store `target.min_global_align` as an `Align`) - rust-lang/rust#142183 (Added test for 30904) - rust-lang/rust#142194 (Remove all unused feature gates from the compiler) - rust-lang/rust#142199 (Do not free disk space in the `mingw-check-tidy` job) - rust-lang/rust#142210 (Run `mingw-check-tidy` on auto builds) r? `@ghost` `@rustbot` modify labels: rollup
2025-06-08Remove rustc's notion of "preferred" alignment AKA `__alignof`Jubilee Young-13/+2
In PR 90877 T-lang decided not to remove `intrinsics::pref_align_of`. However, the intrinsic and its supporting code 1. is a nightly feature, so can be removed at compiler/libs discretion 2. requires considerable effort in the compiler to support, as it necessarily complicates every single site reasoning about alignment 3. has been justified based on relevance to codegen, but it is only a requirement for C++ (not C, not Rust) stack frame layout for AIX, in ways Rust would not consider even with increased C++ interop 4. is only used by rustc to overalign some globals, not correctness 5. can be adequately replaced by other rules for globals, as it mostly affects alignments for a few types under 16 bytes of alignment 6. has only one clear benefactor: automating C -> Rust translation for GNU extensions like `__alignof` 7. such code was likely intended to be `alignof` or `_Alignof`, because the GNU extension is a "false friend" of the C keyword, which makes the choice to support such a mapping very questionable 8. makes it easy to do incorrect codegen in the compiler by its mere presence as usual Rust rules of alignment (e.g. `size == align * N`) do not hold with preferred alignment The implementation is clearly damaging the code quality of the compiler. Thus it is within the compiler team's purview to simply rip it out. If T-lang wishes to have this intrinsic restored for c2rust's benefit, it would have to use a radically different implementation that somehow does not cause internal incorrectness. Until then, remove the intrinsic and its supporting code, as one tool and an ill-considered GCC extension cannot justify risking correctness. Because we touch a fair amount of the compiler to change this at all, and unfortunately the duplication of AbiAndPrefAlign is deep-rooted, we keep an "AbiAlign" type which we can wean code off later.
2025-06-07const-eval error: always say in which item the error occurredRalf Jung-15/+3
also adjust the wording a little so that we don't say "the error occurred here" for two different spans
2025-06-06Rollup merge of #142103 - scottmcm:fieldidx-in-interp, r=oli-obkGuillaume Gomez-39/+46
Update `InterpCx::project_field` to take `FieldIdx` As suggested by Ralf in https://github.com/rust-lang/rust/pull/142005#discussion_r2125839015
2025-06-05Update `InterpCx::project_field` to take `FieldIdx`Scott McMurray-39/+46
As suggested by Ralf in 142005.
2025-06-06Rollup merge of #142012 - oli-obk:no-optional-spans, r=fee1-deadMatthias Krüger-10/+7
Replace some `Option<Span>` with `Span` and use DUMMY_SP instead of None Turns out many locations actually have a span available that we could use, so I used it
2025-06-05Replace some `Option<Span>` with `Span` and use DUMMY_SP instead of NoneOli Scherer-10/+7
2025-06-05Rollup merge of #142015 - oli-obk:wrong-instance, r=RalfJungMatthias Krüger-2/+3
Report the actual item that evaluation failed for instead of id of the last frame in the evaluation stack r? ``@RalfJung`` fixes rust-lang/rust#142010
2025-06-05Auto merge of #135054 - cramertj:file-cstr, r=m-ou-sebors-8/+13
Add Location::file_with_nul This is useful for C/C++ APIs which expect the const char* returned from __FILE__ or std::source_location::file_name. ACP: https://github.com/rust-lang/libs-team/issues/466 Tracking issue: https://github.com/rust-lang/rust/issues/141727
2025-06-04Add Location::file_with_nulTaylor Cramer-8/+13
This is useful for C/C++ APIs which expect the const char* returned from __FILE__ or std::source_location::file_name.
2025-06-04Report the actual item that evaluation failed forOli Scherer-2/+3
2025-06-03Change `tag_field` to `FieldIdx` in `Variants::Multiple`Scott McMurray-5/+5
It was already available as a generic parameter anyway, and it's not like we'll ever put a tag in the 5-billionth field.
2025-06-03Rollup merge of #141698 - oli-obk:ctfe-err-flip, r=RalfJungMatthias Krüger-51/+35
Use the informative error as the main const eval error message r? `@RalfJung` I only did the minimal changes necessary to the const eval error machinery. I'd prefer not to mix test changes with refactorings 😆
2025-06-03Add `iter` macroOli Scherer-16/+12
This adds an `iter!` macro that can be used to create movable generators. This also adds a yield_expr feature so the `yield` keyword can be used within iter! macro bodies. This was needed because several unstable features each need `yield` expressions, so this allows us to stabilize them separately from any individual feature. Co-authored-by: Oli Scherer <github35764891676564198441@oli-obk.de> Co-authored-by: Jieyou Xu <jieyouxu@outlook.com> Co-authored-by: Travis Cross <tc@traviscross.com>
2025-06-02Use the informative error as the main const eval error messageOli Scherer-51/+35
2025-05-31Add const support for float rounding methodsRuan Comelli-0/+113
Add const support for the float rounding methods floor, ceil, trunc, fract, round and round_ties_even. This works by moving the calculation logic from src/tools/miri/src/intrinsics/mod.rs into compiler/rustc_const_eval/src/interpret/intrinsics.rs. All relevant method definitions were adjusted to include the `const` keyword for all supported float types: f16, f32, f64 and f128. The constness is hidden behind the feature gate feature(const_float_round_methods) which is tracked in https://github.com/rust-lang/rust/issues/141555 This commit is a squash of the following commits: - test: add tests that we expect to pass when float rounding becomes const - feat: make float rounding methods `const` - fix: replace `rustc_allow_const_fn_unstable(core_intrinsics)` attribute with `#[rustc_const_unstable(feature = "f128", issue = "116909")]` in `library/core/src/num/f128.rs` - revert: undo update to `library/stdarch` - refactor: replace multiple `float_<mode>_intrinsic` rounding methods with a single, parametrized one - fix: add `#[cfg(not(bootstrap))]` to new const method tests - test: add extra sign tests to check `+0.0` and `-0.0` - revert: undo accidental changes to `round` docs - fix: gate `const` float round method behind `const_float_round_methods` - fix: remove unnecessary `#![feature(const_float_methods)]` - fix: remove unnecessary `#![feature(const_float_methods)]` [2] - revert: undo changes to `tests/ui/consts/const-eval/float_methods.rs` - fix: adjust after rebase - test: fix float tests - test: add tests for `fract` - chore: add commented-out `const_float_round_methods` feature gates to `f16` and `f128` - fix: adjust NaN when rounding floats - chore: add FIXME comment for de-duplicating float tests - test: remove unnecessary test file `tests/ui/consts/const-eval/float_methods.rs` - test: fix tests after upstream simplification of how float tests are run
2025-05-27Rollup merge of #141495 - compiler-errors:rename-unpack, r=fmeaseMatthias Krüger-2/+2
Rename `{GenericArg,Term}::unpack()` to `kind()` A well-deserved rename IMO. r? `@oli-obk` or `@lcnr` (or anyone) cc `@rust-lang/types,` but I'd be surprised if this is controversial.
2025-05-27Rename unpack to kindMichael Goulet-2/+2
2025-05-27Rollup merge of #141582 - RalfJung:cleanup, r=bjorn3Michael Goulet-1/+1
intrinsics, ScalarInt: minor cleanup Taken out of https://github.com/rust-lang/rust/pull/141507 while we resolve technical disagreements in that PR. r? ``@bjorn3``