Skip to content
Open
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
9 changes: 7 additions & 2 deletions compiler/rustc_const_eval/src/util/compare_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use rustc_infer::infer::TyCtxtInferExt;
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::{Ty, TyCtxt, TypingEnv, Unnormalized, Variance};
use rustc_middle::ty::{self, Ty, TyCtxt, TypingEnv, Unnormalized, Variance};
use rustc_trait_selection::traits::ObligationCtxt;

/// Returns whether `src` is a subtype of `dest`, i.e. `src <: dest`.
Expand Down Expand Up @@ -43,5 +43,10 @@ pub fn relate_types<'tcx>(
Ok(()) => {}
Err(_) => return false,
};
ocx.evaluate_obligations_error_on_ambiguity().is_empty()

if ocx.evaluate_obligations_error_on_ambiguity().is_empty() {
infcx.leak_check(ty::UniverseIndex::ROOT, None).is_ok()
Copy link
Copy Markdown
Member

@RalfJung RalfJung Apr 27, 2026

Choose a reason for hiding this comment

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

Suggested change
infcx.leak_check(ty::UniverseIndex::ROOT, None).is_ok()
// Ensure there are no pending lifetime constraints. This is needed to avoid issues like
// <https://github.com/rust-lang/rust/issues/155477>.
infcx.leak_check(ty::UniverseIndex::ROOT, None).is_ok()

View changes since the review

} else {
false
}
}
Copy link
Copy Markdown
Member

@RalfJung RalfJung Apr 27, 2026

Choose a reason for hiding this comment

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

This is more of a const-eval interpreter problem than a transmute problem so IMO putting it in the "consts/const-eval" folder makes more sense. Or at least have "const" somewhere in the name so that it gets picked up by ./x test ui -- const.

View changes since the review

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//@ check-pass

// Regression test for #155477. We previously didn't detect higher-ranked region
// errors when checking whether types are equal in `rustc_const_eval::relate_types`, that
// then caused us to trigger an assert that equal types have the same layout.

use std::mem::transmute;

type F1 = for<'a> fn(&'a ());
type F2 = fn(&'static ());

trait Trait {
type Assoc;
}

impl Trait for F1 {
type Assoc = i64;
}
#[expect(coherence_leak_check)]
Copy link
Copy Markdown
Member

@RalfJung RalfJung Apr 27, 2026

Choose a reason for hiding this comment

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

What does this do...?

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it expects the coherence_leak_check lint. That lint is in a horrible state but fixing the lint doesn't seem too useful given that this thing is currently still in flux and updating it properly is non-trivial 😅

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.

Ah, your fix makes this code emit a lint (#56105). That doesn't seem to make much sense but 🤷 sure whatever.

impl Trait for F2 {
type Assoc = [i32; 2];
}

struct Thing<T: Trait>(T::Assoc);

const fn foo(x: Thing<F1>) -> Thing<F2> {
// This transmute is legal: The two types have the same size.
unsafe { transmute(x) }
}

fn main() {
const { foo(Thing(1i64)); }
}
Loading