Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_mir_transform/src/impossible_predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::pass_manager::MirPass;

pub(crate) struct ImpossiblePredicates;

fn has_impossible_predicates(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
pub(crate) fn has_impossible_predicates(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
let predicates = tcx.predicates_of(def_id).instantiate_identity(tcx);
tracing::trace!(?predicates);
let predicates =
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_mir_transform/src/trivial_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ where
return None;
}

// If there are impossible predicates then MIR passes will replace the body with
// `unreachable` causing const eval errors when trying to evaluate the body. For
// now we avoid using trivial consts for such bodies so that the behaviour doesn't
// change.
if crate::impossible_predicates::has_impossible_predicates(tcx, def.into()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if crate::impossible_predicates::has_impossible_predicates(tcx, def.into()) {
// If there are impossible predicates then MIR passes will replace the body with
// `unreachable` causing const eval errors when trying to evaluate the body. For
// now we avoid using trivial consts for such bodies so that the behaviour doesn't
// change.
if crate::impossible_predicates::has_impossible_predicates(tcx, def.into()) {

@BoxyUwU BoxyUwU Jun 8, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though, writing this out, your logic here just straight up looks at the MIR so I'm surprised there'd wind up being a divergence? are you looking at different MIR than what const eval actually executes? if so why :3

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you looking at different MIR than what const eval actually executes? if so why :3

Yes. The entire idea of trivial_const is to bypass the numerous MIR passes and queries that are done on MIR bodies to lower consts, because in the common case all those passes and queries just add compile time.

It would probably make sense for trivial_const to intercept const lowering before MIR is built.

return None;
}

if !tcx.opaque_types_defined_by(def).is_empty() {
return None;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/traits/trivial-const-with-impossible-bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![crate_type = "lib"]

struct Dummy;
impl Dummy where for<'a> &'a mut i32: Copy {
const C: usize = 1; //~ ERROR entering unreachable code
}

fn foo() where for<'a> &'a mut i32: Copy {
if let Dummy::C = 1 {}
}
9 changes: 9 additions & 0 deletions tests/ui/traits/trivial-const-with-impossible-bounds.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0080]: entering unreachable code
--> $DIR/trivial-const-with-impossible-bounds.rs:5:5
|
LL | const C: usize = 1;
| ^^^^^^^^^^^^^^^^^^^ evaluation of `Dummy::C` failed here

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0080`.
Loading