Abort const-eval queries early when there are generics in the type - #159504
Conversation
|
Some changes occurred in match checking cc @Nadrieril |
|
|
| // become necessary to just use type system normalization for all const patterns | ||
| // but that's not yet possible. | ||
| let mut thir_pat = if alias_const.kind.is_type_const(self.tcx) { | ||
| let const_value = if alias_const.kind.is_type_const(self.tcx) { |
There was a problem hiding this comment.
It seems kind of odd to me that we are treating type constants separately here, I would think there should be a uniform code path... but that is pre-existing so 🤷
There was a problem hiding this comment.
Yeah it's a bit sus, i think we do want to unify these code paths but I'm not entirely sure how that would look like. I've been thinking it might make sense to make this type const code path the "main" code path even for normal const items but that means we get more constants in the type system which aren't actually type system constants. unforchies :>
|
I think this PR doesn't change anything at all. If there's a breaking change it already occurred when #156977 landed which made it so that evaluating a constant with generics in its type can never get past validation. Maybe we should just have that check in the beginning of the query rather than the end, and make it a proper guarantee of our const-eval queries rather than something ad-hoc in const-to-pat? Cc @rust-lang/wg-const-eval |
That sounds a lot more principled, yes please |
Okay, the last commit implements that. |
|
|
||
| /// This constant cannot go back into the type system, as it represents | ||
| /// something the type system cannot handle (e.g. pointers). | ||
| /// `Ty` should always be monomorphic, i.e. `has_params()` should return `false`. |
There was a problem hiding this comment.
FWIW this comment is mostly aspirational. I am not sure if this is true and I am not sure how to effectively check it.^^
I did find one place to put a debug_assert!, let's see if CI finds anything.
There was a problem hiding this comment.
Oh well, it already ICE'd.^^
So apparently this is not currently a valid invariant. However that means const-to-pat can encounter generics even if we block them in the const-eval query, since it might get a const that doesn't come from that query.
|
Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri |
This comment has been minimized.
This comment has been minimized.
1e64ddf to
eb27207
Compare
| self.valtree_to_pat(ty::Value { ty, valtree }) | ||
| ty::Value { ty, valtree } | ||
| }; | ||
| debug_assert!(!const_value.ty.has_param()); |
There was a problem hiding this comment.
It seems fairly clear that we want this property to hold here. However I have no idea how to ensure that it actually holds without checking it just above (which we currently don't do, but an earlier version of this PR did). OTOH at least our test suite also doesn't seem to have a counterexample to this...
There was a problem hiding this comment.
@Nadrieril I wonder what your thoughts on this are. You said you'd prefer this check to already happen in the const-eval queries rather than here, but I don't see how doing that is enough to ensure this property for patterns because there are various other codepaths that can lead us here (trivial consts, and type consts) which never invoke a const-eval query.
There was a problem hiding this comment.
Ah, I didn't think of that. Is the check you've got now enough, provided we turn it into a proper error rather than an ICE?
There was a problem hiding this comment.
In theory type const normalization should never result in a valtree with a generic type (I guess that's not entirely true because I messed up our definition of a value here under mGCA but ignoring that...). I guess the actual point here is that normalization will never give you back a "full value" with a generic type.
There was a problem hiding this comment.
@BoxyUwU
Well the assertion found a Const::Val with generic type. I didn't dig into where that value came from, but something is still constructing them even with the new query checks in this PR. This is for MIR const values, not valtrees. This might have been happening while trying to evaluate a generic MIR body where maybe that is expected? But then the invariant isn't as simple as "Const::Val never has generic type".
There was a problem hiding this comment.
Sounds good to me
Okay, I did that now. PR should be good to go then.
|
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Abort const-eval queries early when there are generics in the type
This comment has been minimized.
This comment has been minimized.
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
@rustbot ready |
|
@bors r=BoxyUwU,Nadrieril |
…,Nadrieril Abort const-eval queries early when there are generics in the type Since [recently](rust-lang#156977), const validation has a `has_param` check, which means ConstToPat also implicitly has that check. But it seems better to check this again explicitly here rather then rely on an undocumented property of some other component. The new test behaves the same with or without the PR. I recommend hiding whitespace difference, since rustfmt re-indendet a bunch of stuff. Fixes rust-lang#150296 r? @BoxyUwU
…,Nadrieril Abort const-eval queries early when there are generics in the type Since [recently](rust-lang#156977), const validation has a `has_param` check, which means ConstToPat also implicitly has that check. But it seems better to check this again explicitly here rather then rely on an undocumented property of some other component. The new test behaves the same with or without the PR. I recommend hiding whitespace difference, since rustfmt re-indendet a bunch of stuff. Fixes rust-lang#150296 r? @BoxyUwU
…,Nadrieril Abort const-eval queries early when there are generics in the type Since [recently](rust-lang#156977), const validation has a `has_param` check, which means ConstToPat also implicitly has that check. But it seems better to check this again explicitly here rather then rely on an undocumented property of some other component. The new test behaves the same with or without the PR. I recommend hiding whitespace difference, since rustfmt re-indendet a bunch of stuff. Fixes rust-lang#150296 r? @BoxyUwU
Rollup of 8 pull requests Successful merges: - #159504 (Abort const-eval queries early when there are generics in the type) - #159523 (std: fix Xous UDP recv length over-report and OOB panic) - #159605 (Add fallback for `intrinsics::fabs`) - #159699 (bump std libc to 0.2.189) - #159306 (Add new variant to iterating-updating-mutref borrowck test) - #159346 (fs::hard_link: use linkat on Android) - #159513 (Consider `()` as suspicious only when expecting `!` for runtime symbols) - #159734 (Document the link_section attribute)
Rollup merge of #159504 - RalfJung:generic-in-pat, r=BoxyUwU,Nadrieril Abort const-eval queries early when there are generics in the type Since [recently](#156977), const validation has a `has_param` check, which means ConstToPat also implicitly has that check. But it seems better to check this again explicitly here rather then rely on an undocumented property of some other component. The new test behaves the same with or without the PR. I recommend hiding whitespace difference, since rustfmt re-indendet a bunch of stuff. Fixes #150296 r? @BoxyUwU
Rollup of 8 pull requests Successful merges: - rust-lang/rust#159504 (Abort const-eval queries early when there are generics in the type) - rust-lang/rust#159523 (std: fix Xous UDP recv length over-report and OOB panic) - rust-lang/rust#159605 (Add fallback for `intrinsics::fabs`) - rust-lang/rust#159699 (bump std libc to 0.2.189) - rust-lang/rust#159306 (Add new variant to iterating-updating-mutref borrowck test) - rust-lang/rust#159346 (fs::hard_link: use linkat on Android) - rust-lang/rust#159513 (Consider `()` as suspicious only when expecting `!` for runtime symbols) - rust-lang/rust#159734 (Document the link_section attribute)
Pkgsrc changes: * Adapt to changes in vendored crate versions. * Version & checksum changes. Upstream changes: Version 1.99.0 (2026-10-01) ========================== Language -------- - [Add allow-by-default `raw_borrows_via_references` lint that checks for references that decay immediately into raw borrows](rust-lang/rust#138230) - [Extend `unconditional_panic` lint to function calls that panic when the chunks/windows size is zero] (rust-lang/rust#153563) - [Stabilize C-variadic function definitions] (rust-lang/rust#155697) - [Stabilize the ability to use `#[unsafe(naked)]` functions to define C-variadic functions (`#![feature(c_variadic_naked_functions)]`).] (rust-lang/rust#159746) - [Trait methods are now resolved on an adjusted never type (producing a FCW)] (rust-lang/rust#156047) - [Coerce from inference variables to trait objects if the inference variable is related via subtyping to a type that is known to be `Sized`] (rust-lang/rust#157820) - [Stabilize `#[my_macro] mod foo;`] (rust-lang/rust#157857). This allows outlined modules (`mod foo;`) anywhere in the body of a custom attribute or derive macro. - [Fix the `overflowing_literals` lint with repeated negation] (rust-lang/rust#158302). For instance, it will now no longer lint on `--128_i8`, which is already detected by the `arithmetic_overflow` lint. - [Add POSIX symbols to the `invalid_runtime_symbol_definitions` and `suspicious_runtime_symbol_definitions` lints] (rust-lang/rust#158522) - [Lint unused `#[path]` attributes on inline modules] (rust-lang/rust#158835) - [Enable `unreachable_cfg_select_predicates` lint as part of `unused` lint group] (rust-lang/rust#159179) - [Stabilize passing 128-bit integers via vector registers with `asm!` on x86] (rust-lang/rust#159525) - [Explicitly document that some allocations are allowed to grow in-place (but none are allowed to shrink)] (rust-lang/rust#159729) - We now [guarantee] (rust-lang/rust#159730) that the contents of an `UnsafeCell` can be accessed without going through `get` - [The `invalid_reference_casting` lint was adjusted accordingly] (rust-lang/rust#159960) - [Account for globally enabled target features in `global_asm!`] (rust-lang/rust#160594) - [Warn if an invalid `doc` attribute is used on a macro invocation] (rust-lang/rust#161003) Compiler -------- - [Convert `-Ctarget-cpu` into a target-modifier for AVR, AMDGCN and NVPTX] (rust-lang/rust#150732) - [Enable `static_position_independent_executables` on all gnu and musl targets] (rust-lang/rust#158510) - When providing a suggestion about a missing method, rustc now prefers an exactly matching name from a [doc alias attribute] (https://doc.rust-lang.org/rustdoc/advanced-features.html#add-aliases-for-an-item-in-documentation-search) over a similarity search from other method names. If your new users sometimes expect a method under a different name, adding a doc alias will now help them find it via rustc suggestions, in addition to helping them find it via rustdoc search: [When suggesting method names, prefer *exact* doc aliases over similar names](rust-lang/rust#160369) Platform Support ---------------- - [Promote `riscv64-unknown-linux-musl` to Tier 2 with host tools] (rust-lang/rust#158766) Refer to Rust's [platform support page][platform-support-doc] for more information on Rust's tiered platform support. [platform-support-doc]: https://doc.rust-lang.org/rustc/platform-support.html Libraries --------- - Iteration on `RangeInclusive` (`a..=b` ranges) is now [optimized better in some circumstances] (rust-lang/rust#155114). As a side effect of this, the behavior of `RangeInclusive` values that has already been exhausted (as an iterator) has changed. For example, the return values of `start()` and `end()` on such ranges may return different values, and using such ranges as slice indexes may have different behavior. These behaviors were not guaranteed to be stable, so these changes are considered to not be breaking changes. - [Relax `transmute_copy` to accept `?Sized` types] (rust-lang/rust#155989) - [Update `transmute_copy` to use a non-unwinding panic] (rust-lang/rust#155989) - [Don't escape U+FF9E and U+FF9F in `escape_debug_ext`] (rust-lang/rust#158057) - [Re-export `core::fmt::NumBuffer` in `alloc` (and `std`)] (rust-lang/rust#161430) Stabilized APIs --------------- - [`IntoIterator` for `Box<[T; N]>`] (https://doc.rust-lang.org/stable/std/iter/trait.IntoIterator.html#impl-IntoIterator-for-Box%3C%5BT;+N%5D,+A%3E) - [`IntoIterator` for `&Box<[T; N]>`] (https://doc.rust-lang.org/stable/std/iter/trait.IntoIterator.html#impl-IntoIterator-for-%26Box%3C%5BT;+N%5D,+A%3E) - [`IntoIterator` for `&mut Box<[T; N]>`] (https://doc.rust-lang.org/stable/std/iter/trait.IntoIterator.html#impl-IntoIterator-for-%26mut+Box%3C%5BT;+N%5D,+A%3E) - [`VecDeque::retain_back`] (https://doc.rust-lang.org/stable/std/collections/struct.VecDeque.html#method.retain_back) - [`core::ffi::VaList`] (https://doc.rust-lang.org/stable/core/ffi/struct.VaList.html) - [`Box::into_non_null`] (https://doc.rust-lang.org/stable/std/boxed/struct.Box.html#method.into_non_null) - [`Box::from_non_null`] (https://doc.rust-lang.org/stable/std/boxed/struct.Box.html#method.from_non_null) - [`Vec::into_parts`] (https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#method.into_parts) - [`Vec::from_parts`] (https://doc.rust-lang.org/stable/std/vec/struct.Vec.html#method.from_parts) - [`core::mem::size_of_val_raw`] (https://doc.rust-lang.org/stable/core/mem/fn.size_of_val_raw.html) - [`core::mem::align_of_val_raw`] (https://doc.rust-lang.org/stable/core/mem/fn.align_of_val_raw.html) - [`core::alloc::Layout::for_value_raw`] (https://doc.rust-lang.org/stable/core/alloc/struct.Layout.html#method.for_value_raw) - [`String::from_utf8_lossy_owned`] (https://doc.rust-lang.org/stable/std/string/struct.String.html#method.from_utf8_lossy_owned) - [`string::FromUtf8Error::into_utf8_lossy`] (https://doc.rust-lang.org/stable/std/string/struct.FromUtf8Error.html#method.into_utf8_lossy) - [`FusedIterator for StepBy<I>`] (https://doc.rust-lang.org/stable/std/iter/struct.StepBy.html#impl-FusedIterator-for-StepBy%3CI%3E) - [`std::fs::set_times`] (https://doc.rust-lang.org/stable/std/fs/fn.set_times.html) - [`std::fs::set_times_nofollow`] (https://doc.rust-lang.org/stable/std/fs/fn.set_times_nofollow.html) Cargo ----- - Add a new built-in profile `debug`. This is a preparation for transitioning the `dev` profile away from debugging to give a saner default for faster development iterations. Currently there is no difference between `dev` and `debug` profiles. [docs] (https://doc.rust-lang.org/nightly/cargo/reference/profiles.html#debug-1) [#17214] (rust-lang/cargo#17214) - Workspace members on edition 2024 or later can now override an inherited workspace dependency's `default-features` field. For example, `serde = { workspace = true, default-features = false }` now turns off default features even when the workspace definition enables them. On earlier editions, `default-features = false` is ignored with a warning. ([RFC 3945] (rust-lang/rfcs#3945)) [#17126](rust-lang/cargo#17126) - Incremental compilation is now disabled by default when running in CI. CI is detected via the CI environment variable. [#17220] (rust-lang/cargo#17220) See also the [full Cargo changelog] (https://doc.rust-lang.org/nightly/cargo/CHANGELOG.html#cargo-199-2026-10-01) Rustdoc ----- - [Add new `unused_footnote_definition` rustdoc lint] (rust-lang/rust#137858) - Smarter filtering of trait impls yields performance improvements of 20% on average and up to 40% on some real-world crates. ([1] (rust-lang/rust#159623), [2](rust-lang/rust#159721), [3](rust-lang/rust#159779), [4](rust-lang/rust#159854), [5](rust-lang/rust#159091)) Compatibility Notes ------------------- - [Fully deprecate the legacy integral modules] (rust-lang/rust#146882). For example, `std::i32::MAX` should be accessed via `i32::MAX` instead. - [Upgrade `no_mangle_generic_items` into hard error] (rust-lang/rust#154585) - [The `Pin::new_unchecked` has had its safety invariants changed slightly] (rust-lang/rust#156935) - [Do not promote references to extern statics] (rust-lang/rust#157641) - [Ensure that the inferred types of `let` patterns typecheck] (rust-lang/rust#157841) - [hermit/fs: Return `unsupported()` instead of `from_raw_os_error(22)`] (rust-lang/rust#158247) - [Fixed a bug where `#[repr(simd)]` was accidentally allowed on macro invocations on stable Rust] (rust-lang/rust#158523) - [Abort const-eval when there are generics in the type of the value being produced] (rust-lang/rust#159504) - [Attributes not applying to anything are now an error in code blocks in doc comments] (rust-lang/rust#159849) - [`Box::leak`: tell people to avoid unleaking] (rust-lang/rust#160323) - [PowerPC inline ASM: Fix scalar floats being in the wrong vector lane on little endian] (rust-lang/rust#160441) - [Do not take `doc(cfg())` into account when filtering doctests] (rust-lang/rust#159014) - [Infer anonymous lifetimes in the types of associated consts as `'static`] (rust-lang/rust#156508) - Macros that expand to a semicolon now produce a warning lint (`semicolon_in_expressions_from_non_local_macros`) even when the macro comes from another crate. Previously, such warnings only appeared for macros from the same crate, to avoid showing warnings that can't be fixed locally; however, this masked problems, as integration tests from the crate providing the macro get compiled as a separate crate, so tests often wouldn't reveal this issue. If you encounter a lint like this, please make sure to report it to the crate providing the macro so they can fix it; don't just silence it in your own crate. - [`semicolon_in_expressions_from_macros`: Lint on non-local macros too] (rust-lang/rust#159222) - [Split non-local `semicolon_in_expressions_from_macros` into a separate lint] (rust-lang/rust#159700) Internal Changes ---------------- These changes do not affect any public interfaces of Rust, but they represent significant improvements to the performance or internals of rustc and related tools. - [Update to LLVM 23] (rust-lang/rust#158734)
View all comments
Since recently, const validation has a
has_paramcheck, which means ConstToPat also implicitly has that check. But it seems better to check this again explicitly here rather then rely on an undocumented property of some other component. The new test behaves the same with or without the PR.I recommend hiding whitespace difference, since rustfmt re-indendet a bunch of stuff.
Fixes #150296
r? @BoxyUwU