Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leak check in impossible_predicates to avoid monomorphizing impossible instances #135466

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 12 additions & 3 deletions compiler/rustc_trait_selection/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,18 @@ pub fn impossible_predicates<'tcx>(tcx: TyCtxt<'tcx>, predicates: Vec<ty::Clause
}
let errors = ocx.select_all_or_error();

let result = !errors.is_empty();
debug!("impossible_predicates = {:?}", result);
result
if !errors.is_empty() {
return true;
}

// Leak check for any higher-ranked trait mismatches.
// We only need to do this in the old solver, since the new solver already
// leak-checks.
if !infcx.next_trait_solver() && infcx.leak_check(ty::UniverseIndex::ROOT, None).is_err() {
return true;
}

false
}

fn instantiate_and_check_impossible_predicates<'tcx>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//@ build-pass
//@ revisions: current next
//@ ignore-compare-mode-next-solver (explicit revisions)
//@[next] compile-flags: -Znext-solver

lcnr marked this conversation as resolved.
Show resolved Hide resolved
#![allow(coherence_leak_check)]

type A = fn(&'static ());
type B = fn(&());

trait Bound<P: WithAssoc>: From<GetAssoc<P>> {
}
impl Bound<B> for String {}

trait Trt<T> {
fn __(&self, x: T) where T: Bound<A> {
T::from(());
}
}

impl<T, S> Trt<T> for S {}

type GetAssoc<T> = <T as WithAssoc>::Ty;

trait WithAssoc {
type Ty;
}

impl WithAssoc for B {
type Ty = String;
}

impl WithAssoc for A {
type Ty = ();
}

fn main() {
let x: &'static dyn Trt<String> = &();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//@ build-pass
//@ revisions: current next
//@ ignore-compare-mode-next-solver (explicit revisions)
//@[next] compile-flags: -Znext-solver

trait Foo {}
impl Foo for fn(&'static ()) {}

trait Bar {
type Assoc: Default;
}
impl<T: Foo> Bar for T {
type Assoc = usize;
}
impl Bar for fn(&()) {
type Assoc = ();
}

fn needs_foo<T: Foo>() -> usize {
needs_bar::<T>()
}

fn needs_bar<T: Bar>() -> <T as Bar>::Assoc {
Default::default()
}

trait Evil<T> {
fn bad(&self)
where
T: Foo,
{
needs_foo::<T>();
}
}

impl Evil<fn(&())> for () {}

fn main() {
let x: &dyn Evil<fn(&())> = &();
}
Loading