From c9fc358c466ed26aac060b3fe711637be55b52dc Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Tue, 10 Mar 2026 15:28:04 +0900 Subject: [PATCH 1/3] add regression test for inherent assoc type mismatch ICE --- ...inherent-assoc-ty-mismatch-issue-153539.rs | 21 +++++++++++++ ...rent-assoc-ty-mismatch-issue-153539.stderr | 31 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.rs create mode 100644 tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.stderr 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..019765a1909f3 --- /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)] +//~^ WARN the feature `inherent_associated_types` is incomplete + +// 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..66ca01e845f31 --- /dev/null +++ b/tests/ui/associated-inherent-types/inherent-assoc-ty-mismatch-issue-153539.stderr @@ -0,0 +1,31 @@ +warning: the feature `inherent_associated_types` is incomplete and may not be safe to use and/or cause compiler crashes + --> $DIR/inherent-assoc-ty-mismatch-issue-153539.rs:2:12 + | +LL | #![feature(inherent_associated_types)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: see issue #8995 for more information + = note: `#[warn(incomplete_features)]` on by default + +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; 1 warning emitted + +Some errors have detailed explanations: E0207, E0308. +For more information about an error, try `rustc --explain E0207`. From a92006f5c99f551fe7367e35532b398aa462474a Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Tue, 10 Mar 2026 15:28:29 +0900 Subject: [PATCH 2/3] avoid projection-only suggestions for inherent assoc types --- .../error_reporting/infer/note_and_explain.rs | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) 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 \ From db1060e5bbf07a1203a31f9d87474dd3d3ff1e5e Mon Sep 17 00:00:00 2001 From: Takayuki Maeda Date: Tue, 10 Mar 2026 18:32:20 +0900 Subject: [PATCH 3/3] use `#![expect(incomplete_features)]` --- .../inherent-assoc-ty-mismatch-issue-153539.rs | 2 +- .../inherent-assoc-ty-mismatch-issue-153539.stderr | 11 +---------- 2 files changed, 2 insertions(+), 11 deletions(-) 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 index 019765a1909f3..960e19c21a2c5 100644 --- 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 @@ -1,6 +1,6 @@ //@ compile-flags: -Znext-solver=globally #![feature(inherent_associated_types)] -//~^ WARN the feature `inherent_associated_types` is incomplete +#![expect(incomplete_features)] // Regression test for https://github.com/rust-lang/rust/issues/153539: 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 index 66ca01e845f31..74d88889223f3 100644 --- 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 @@ -1,12 +1,3 @@ -warning: the feature `inherent_associated_types` is incomplete and may not be safe to use and/or cause compiler crashes - --> $DIR/inherent-assoc-ty-mismatch-issue-153539.rs:2:12 - | -LL | #![feature(inherent_associated_types)] - | ^^^^^^^^^^^^^^^^^^^^^^^^^ - | - = note: see issue #8995 for more information - = note: `#[warn(incomplete_features)]` on by default - 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 | @@ -25,7 +16,7 @@ LL | f(&1) = 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; 1 warning emitted +error: aborting due to 2 previous errors Some errors have detailed explanations: E0207, E0308. For more information about an error, try `rustc --explain E0207`.