Skip to content

Commit 17a6b83

Browse files
Rollup merge of #153643 - TaKO8Ki:issue-153539, r=Kivooeo
Avoid projection-only suggestions for inherent assoc types Fixes #153539. Type mismatch diagnostics in `note_and_explain_type_err` currently route both `ty::Projectio`n and `ty::Inherent` through the same associated-type suggestion path. For inherent associated types, that path can reach helpers that are only valid for trait projections.
2 parents f089301 + db1060e commit 17a6b83

3 files changed

Lines changed: 58 additions & 12 deletions

File tree

compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,18 +272,21 @@ impl<T> Trait<T> for X {
272272
values.found, values.expected,
273273
)
274274
};
275-
if !(self.suggest_constraining_opaque_associated_type(
276-
diag,
277-
msg,
278-
proj_ty,
279-
values.expected,
280-
) || self.suggest_constraint(
281-
diag,
282-
&msg,
283-
body_owner_def_id,
284-
proj_ty,
285-
values.expected,
286-
)) {
275+
let suggested_projection_constraint = proj_ty.kind(tcx)
276+
== ty::AliasTyKind::Projection
277+
&& (self.suggest_constraining_opaque_associated_type(
278+
diag,
279+
msg,
280+
proj_ty,
281+
values.expected,
282+
) || self.suggest_constraint(
283+
diag,
284+
&msg,
285+
body_owner_def_id,
286+
proj_ty,
287+
values.expected,
288+
));
289+
if !suggested_projection_constraint {
287290
diag.help(msg());
288291
diag.note(
289292
"for more information, visit \
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ compile-flags: -Znext-solver=globally
2+
#![feature(inherent_associated_types)]
3+
#![expect(incomplete_features)]
4+
5+
// Regression test for https://github.com/rust-lang/rust/issues/153539:
6+
7+
struct S<'a>(&'a ());
8+
9+
impl<X> S<'_> {
10+
//~^ ERROR the type parameter `X` is not constrained by the impl trait, self type, or predicates
11+
type P = ();
12+
}
13+
14+
fn ret_ref_local<'e>() -> &'e i32 {
15+
let f: for<'a> fn(&'a i32) -> S<'a>::P = |x| _ = x;
16+
17+
f(&1)
18+
//~^ ERROR mismatched types
19+
}
20+
21+
fn main() {}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0207]: the type parameter `X` is not constrained by the impl trait, self type, or predicates
2+
--> $DIR/inherent-assoc-ty-mismatch-issue-153539.rs:9:6
3+
|
4+
LL | impl<X> S<'_> {
5+
| ^ unconstrained type parameter
6+
7+
error[E0308]: mismatched types
8+
--> $DIR/inherent-assoc-ty-mismatch-issue-153539.rs:17:5
9+
|
10+
LL | fn ret_ref_local<'e>() -> &'e i32 {
11+
| ------- expected `&'e i32` because of return type
12+
...
13+
LL | f(&1)
14+
| ^^^^^ expected `&i32`, found associated type
15+
|
16+
= help: consider constraining the associated type `S<'_>::P` to `&'e i32`
17+
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
18+
19+
error: aborting due to 2 previous errors
20+
21+
Some errors have detailed explanations: E0207, E0308.
22+
For more information about an error, try `rustc --explain E0207`.

0 commit comments

Comments
 (0)