Skip to content

Commit b7bd207

Browse files
committed
Avoid projection-only constraint suggestions for inherent associated type mismatches
1 parent 14196db commit b7bd207

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,9 @@ impl<T> Trait<T> for X {
606606
ty: Ty<'tcx>,
607607
) -> bool {
608608
let tcx = self.tcx;
609+
if !matches!(proj_ty.kind, ty::AliasTyKind::Projection { .. }) {
610+
return false;
611+
}
609612
let Some(body_owner_def_id) = body_owner_def_id else {
610613
return false;
611614
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//! Regression test for <https://github.com/rust-lang/rust/issues/154333>
2+
//@compile-flags: -Znext-solver=globally
3+
#![feature(inherent_associated_types)]
4+
5+
struct Foo;
6+
impl<const X: y> Foo {
7+
//~^ ERROR the const parameter `X` is not constrained by the impl trait, self type, or predicates
8+
//~| ERROR cannot find type `y` in this scope
9+
type ImplTrait = impl Clone;
10+
//~^ ERROR `impl Trait` in associated types is unstable
11+
//~| ERROR unconstrained opaque type
12+
fn f() -> Self::ImplTrait {
13+
()
14+
//~^ ERROR mismatched types
15+
}
16+
}
17+
//~^ ERROR `main` function not found
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
error[E0425]: cannot find type `y` in this scope
2+
--> $DIR/next-solver-opaque-inherent-no-ice.rs:6:15
3+
|
4+
LL | impl<const X: y> Foo {
5+
| ^ not found in this scope
6+
7+
error[E0658]: `impl Trait` in associated types is unstable
8+
--> $DIR/next-solver-opaque-inherent-no-ice.rs:9:22
9+
|
10+
LL | type ImplTrait = impl Clone;
11+
| ^^^^^^^^^^
12+
|
13+
= note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
14+
= help: add `#![feature(impl_trait_in_assoc_type)]` to the crate attributes to enable
15+
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
16+
17+
error[E0601]: `main` function not found in crate `next_solver_opaque_inherent_no_ice`
18+
--> $DIR/next-solver-opaque-inherent-no-ice.rs:16:2
19+
|
20+
LL | }
21+
| ^ consider adding a `main` function to `$DIR/next-solver-opaque-inherent-no-ice.rs`
22+
23+
error[E0207]: the const parameter `X` is not constrained by the impl trait, self type, or predicates
24+
--> $DIR/next-solver-opaque-inherent-no-ice.rs:6:6
25+
|
26+
LL | impl<const X: y> Foo {
27+
| ^^^^^^^^^^ unconstrained const parameter
28+
|
29+
= note: expressions using a const parameter must map each value to a distinct output value
30+
= note: proving the result of expressions other than the parameter are unique is not supported
31+
32+
error: unconstrained opaque type
33+
--> $DIR/next-solver-opaque-inherent-no-ice.rs:9:22
34+
|
35+
LL | type ImplTrait = impl Clone;
36+
| ^^^^^^^^^^
37+
|
38+
= note: `ImplTrait` must be used in combination with a concrete type within the same impl
39+
40+
error[E0308]: mismatched types
41+
--> $DIR/next-solver-opaque-inherent-no-ice.rs:13:9
42+
|
43+
LL | fn f() -> Self::ImplTrait {
44+
| --------------- expected `Foo::ImplTrait` because of return type
45+
LL | ()
46+
| ^^ types differ
47+
|
48+
= note: expected associated type `Foo::ImplTrait`
49+
found unit type `()`
50+
= help: consider constraining the associated type `Foo::ImplTrait` to `()` or calling a method that returns `Foo::ImplTrait`
51+
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
52+
53+
error: aborting due to 6 previous errors
54+
55+
Some errors have detailed explanations: E0207, E0308, E0425, E0601, E0658.
56+
For more information about an error, try `rustc --explain E0207`.

0 commit comments

Comments
 (0)