diff --git a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs index adb2d2c185cac..0fde0009debe7 100644 --- a/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs +++ b/compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs @@ -272,18 +272,21 @@ impl Trait for X { values.found, values.expected, ) }; - if !(self.suggest_constraining_opaque_associated_type( - diag, - msg, - proj_ty, - values.expected, - ) || self.suggest_constraint( - diag, - &msg, - body_owner_def_id, - proj_ty, - values.expected, - )) { + let suggested_projection_constraint = proj_ty.kind(tcx) + == ty::AliasTyKind::Projection + && (self.suggest_constraining_opaque_associated_type( + diag, + msg, + proj_ty, + values.expected, + ) || self.suggest_constraint( + diag, + &msg, + body_owner_def_id, + proj_ty, + values.expected, + )); + if !suggested_projection_constraint { diag.help(msg()); diag.note( "for more information, visit \ diff --git a/tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.rs b/tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.rs new file mode 100644 index 0000000000000..960e19c21a2c5 --- /dev/null +++ b/tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.rs @@ -0,0 +1,21 @@ +//@ compile-flags: -Znext-solver=globally +#![feature(inherent_associated_types)] +#![expect(incomplete_features)] + +// Regression test for https://github.com/rust-lang/rust/issues/153539: + +struct S<'a>(&'a ()); + +impl S<'_> { + //~^ ERROR the type parameter `X` is not constrained by the impl trait, self type, or predicates + type P = (); +} + +fn ret_ref_local<'e>() -> &'e i32 { + let f: for<'a> fn(&'a i32) -> S<'a>::P = |x| _ = x; + + f(&1) + //~^ ERROR mismatched types +} + +fn main() {} diff --git a/tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.stderr b/tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.stderr new file mode 100644 index 0000000000000..74d88889223f3 --- /dev/null +++ b/tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.stderr @@ -0,0 +1,22 @@ +error[E0207]: the type parameter `X` is not constrained by the impl trait, self type, or predicates + --> $DIR/inherent-assoc-ty-mismatch-issue-153539.rs:9:6 + | +LL | impl S<'_> { + | ^ unconstrained type parameter + +error[E0308]: mismatched types + --> $DIR/inherent-assoc-ty-mismatch-issue-153539.rs:17:5 + | +LL | fn ret_ref_local<'e>() -> &'e i32 { + | ------- expected `&'e i32` because of return type +... +LL | f(&1) + | ^^^^^ expected `&i32`, found associated type + | + = help: consider constraining the associated type `S<'_>::P` to `&'e i32` + = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0207, E0308. +For more information about an error, try `rustc --explain E0207`.