Skip to content

Commit 9c2ef79

Browse files
committed
Fix ICE for inherent associated type mismatches
Avoid projection-only suggestions for inherent associated types.
1 parent 2972b5e commit 9c2ef79

3 files changed

Lines changed: 81 additions & 4 deletions

File tree

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,13 @@ impl<T> Trait<T> for X {
666666
)
667667
);
668668
let impl_comparison = matches!(cause_code, ObligationCauseCode::CompareImplItem { .. });
669+
let is_projection = proj_ty.kind(tcx) == ty::AliasTyKind::Projection;
669670
if impl_comparison {
670671
// We do not want to suggest calling functions when the reason of the
671672
// type error is a comparison of an `impl` with its `trait`.
672673
} else {
673-
let point_at_assoc_fn = if callable_scope
674+
let point_at_assoc_fn = if is_projection
675+
&& callable_scope
674676
&& self.point_at_methods_that_satisfy_associated_type(
675677
diag,
676678
tcx.parent(proj_ty.def_id),
@@ -688,14 +690,17 @@ impl<T> Trait<T> for X {
688690
};
689691
// Possibly suggest constraining the associated type to conform to the
690692
// found type.
691-
if self.suggest_constraint(diag, &msg, body_owner_def_id, proj_ty, values.found)
692-
|| point_at_assoc_fn
693+
if is_projection
694+
&& (self.suggest_constraint(diag, &msg, body_owner_def_id, proj_ty, values.found)
695+
|| point_at_assoc_fn)
693696
{
694697
return;
695698
}
696699
}
697700

698-
self.suggest_constraining_opaque_associated_type(diag, &msg, proj_ty, values.found);
701+
if is_projection {
702+
self.suggest_constraining_opaque_associated_type(diag, &msg, proj_ty, values.found);
703+
}
699704

700705
if self.point_at_associated_type(diag, body_owner_def_id, values.found) {
701706
return;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//@compile-flags: -Znext-solver=globally
2+
#![feature(inherent_associated_types)]
3+
4+
struct Foo;
5+
impl<const X: y> Foo {
6+
//~^ ERROR the const parameter `X` is not constrained by the impl trait, self type, or predicates
7+
//~| ERROR cannot find type `y` in this scope
8+
type ImplTrait = impl Clone;
9+
//~^ ERROR `impl Trait` in associated types is unstable
10+
//~| ERROR unconstrained opaque type
11+
fn f() -> Self::ImplTrait {
12+
()
13+
//~^ ERROR mismatched types
14+
}
15+
}
16+
//~^ 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:5: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:8: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:15: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:5: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:8: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:12: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)