From 377dbc96a6d5b7d22b75d4bd98fd00d9e1bb194e Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 14 Jan 2025 01:50:53 +0000 Subject: [PATCH 1/2] Leak check in impossible_predicates to avoid monomorphizing impossible instances --- .../rustc_trait_selection/src/traits/mod.rs | 15 +++++-- .../impossible-method-modulo-binders-2.rs | 39 ++++++++++++++++++ .../impossible-method-modulo-binders.rs | 40 +++++++++++++++++++ 3 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 tests/ui/traits/trait-upcasting/impossible-method-modulo-binders-2.rs create mode 100644 tests/ui/traits/trait-upcasting/impossible-method-modulo-binders.rs diff --git a/compiler/rustc_trait_selection/src/traits/mod.rs b/compiler/rustc_trait_selection/src/traits/mod.rs index 1dcd0d0dfb830..da16a74209965 100644 --- a/compiler/rustc_trait_selection/src/traits/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/mod.rs @@ -714,9 +714,18 @@ pub fn impossible_predicates<'tcx>(tcx: TyCtxt<'tcx>, predicates: Vec( diff --git a/tests/ui/traits/trait-upcasting/impossible-method-modulo-binders-2.rs b/tests/ui/traits/trait-upcasting/impossible-method-modulo-binders-2.rs new file mode 100644 index 0000000000000..bc43360553194 --- /dev/null +++ b/tests/ui/traits/trait-upcasting/impossible-method-modulo-binders-2.rs @@ -0,0 +1,39 @@ +//@ build-pass +//@ revisions: current next +//@ ignore-compare-mode-next-solver (explicit revisions) +//@[next] compile-flags: -Znext-solver + +#![allow(coherence_leak_check)] + +type A = fn(&'static ()); +type B = fn(&()); + +trait Bound: From> { +} +impl Bound for String {} + +trait Trt { + fn __(&self, x: T) where T: Bound { + T::from(()); + } +} + +impl Trt for S {} + +type GetAssoc = ::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 = &(); +} diff --git a/tests/ui/traits/trait-upcasting/impossible-method-modulo-binders.rs b/tests/ui/traits/trait-upcasting/impossible-method-modulo-binders.rs new file mode 100644 index 0000000000000..63ad1c0a06082 --- /dev/null +++ b/tests/ui/traits/trait-upcasting/impossible-method-modulo-binders.rs @@ -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 Bar for T { + type Assoc = usize; +} +impl Bar for fn(&()) { + type Assoc = (); +} + +fn needs_foo() -> usize { + needs_bar::() +} + +fn needs_bar() -> ::Assoc { + Default::default() +} + +trait Evil { + fn bad(&self) + where + T: Foo, + { + needs_foo::(); + } +} + +impl Evil for () {} + +fn main() { + let x: &dyn Evil = &(); +} From 94ffced66747a94554e0e0fa420eb21abdced095 Mon Sep 17 00:00:00 2001 From: lcnr Date: Tue, 14 Jan 2025 10:06:22 +0100 Subject: [PATCH 2/2] add note to test --- .../trait-upcasting/impossible-method-modulo-binders-2.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ui/traits/trait-upcasting/impossible-method-modulo-binders-2.rs b/tests/ui/traits/trait-upcasting/impossible-method-modulo-binders-2.rs index bc43360553194..c03b5145aa73b 100644 --- a/tests/ui/traits/trait-upcasting/impossible-method-modulo-binders-2.rs +++ b/tests/ui/traits/trait-upcasting/impossible-method-modulo-binders-2.rs @@ -2,7 +2,7 @@ //@ revisions: current next //@ ignore-compare-mode-next-solver (explicit revisions) //@[next] compile-flags: -Znext-solver - +// Regression test for #135462. #![allow(coherence_leak_check)] type A = fn(&'static ());