Skip to content

Commit ba8be06

Browse files
committed
Provide more context on recursive impl evaluation overflow
When an associated type `Self::Assoc` is part of a `where` clause, we end up unable to evaluate the requirement and emit a E0275. We now point at the associated type if specified in the `impl`. If so, we also suggest using that type instead of `Self::Assoc`. Otherwise, we explain that these are not allowed. ``` error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _` --> $DIR/impl-wf-cycle-1.rs:15:1 | LL | / impl<T: Grault> Grault for (T,) LL | | LL | | where LL | | Self::A: Baz, LL | | Self::B: Fiz, | |_________________^ LL | { LL | type A = (); | ------ associated type `<(T,) as Grault>::A` is specified here | note: required for `(T,)` to implement `Grault` --> $DIR/impl-wf-cycle-1.rs:15:17 | LL | impl<T: Grault> Grault for (T,) | ^^^^^^ ^^^^ ... LL | Self::A: Baz, | --- unsatisfied trait bound introduced here = note: 1 redundant requirement hidden = note: required for `(T,)` to implement `Grault` help: associated type for the current `impl` cannot be restricted in `where` clauses, remove this bound | LL - Self::A: Baz, LL + , | ``` ``` error[E0275]: overflow evaluating the requirement `<T as B>::Type == <T as B>::Type` --> $DIR/impl-wf-cycle-3.rs:7:1 | LL | / impl<T> B for T LL | | where LL | | T: A<Self::Type>, | |_____________________^ LL | { LL | type Type = bool; | --------- associated type `<T as B>::Type` is specified here | note: required for `T` to implement `B` --> $DIR/impl-wf-cycle-3.rs:7:9 | LL | impl<T> B for T | ^ ^ LL | where LL | T: A<Self::Type>, | ------------- unsatisfied trait bound introduced here help: replace the associated type with the type specified in this `impl` | LL | T: A<bool>, | ~~~~ ``` ``` error[E0275]: overflow evaluating the requirement `<T as Filter>::ToMatch == <T as Filter>::ToMatch` --> $DIR/impl-wf-cycle-4.rs:5:1 | LL | / impl<T> Filter for T LL | | where LL | | T: Fn(Self::ToMatch), | |_________________________^ | note: required for `T` to implement `Filter` --> $DIR/impl-wf-cycle-4.rs:5:9 | LL | impl<T> Filter for T | ^^^^^^ ^ LL | where LL | T: Fn(Self::ToMatch), | ----------------- unsatisfied trait bound introduced here note: associated types for the current `impl` cannot be restricted in `where` clauses --> $DIR/impl-wf-cycle-4.rs:7:11 | LL | T: Fn(Self::ToMatch), | ^^^^^^^^^^^^^ ``` Fix rust-lang#116925
1 parent 1a47f5b commit ba8be06

File tree

7 files changed

+253
-47
lines changed

7 files changed

+253
-47
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+157-47
Large diffs are not rendered by default.

tests/ui/associated-types/impl-wf-cycle-1.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ LL | | where
77
LL | | Self::A: Baz,
88
LL | | Self::B: Fiz,
99
| |_________________^
10+
LL | {
11+
LL | type A = ();
12+
| ------ associated type `<(T,) as Grault>::A` is specified here
1013
|
1114
note: required for `(T,)` to implement `Grault`
1215
--> $DIR/impl-wf-cycle-1.rs:15:17
@@ -18,6 +21,11 @@ LL | Self::A: Baz,
1821
| --- unsatisfied trait bound introduced here
1922
= note: 1 redundant requirement hidden
2023
= note: required for `(T,)` to implement `Grault`
24+
help: associated type for the current `impl` cannot be restricted in `where` clauses, remove this bound
25+
|
26+
LL - Self::A: Baz,
27+
LL + ,
28+
|
2129

2230
error: aborting due to 1 previous error
2331

tests/ui/associated-types/impl-wf-cycle-2.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ LL | |
66
LL | | where
77
LL | | Self::A: Copy,
88
| |__________________^
9+
LL | {
10+
LL | type A = ();
11+
| ------ associated type `<(T,) as Grault>::A` is specified here
912
|
1013
note: required for `(T,)` to implement `Grault`
1114
--> $DIR/impl-wf-cycle-2.rs:7:17
@@ -15,6 +18,11 @@ LL | impl<T: Grault> Grault for (T,)
1518
...
1619
LL | Self::A: Copy,
1720
| ---- unsatisfied trait bound introduced here
21+
help: associated type for the current `impl` cannot be restricted in `where` clauses, remove this bound
22+
|
23+
LL - Self::A: Copy,
24+
LL + ,
25+
|
1826

1927
error: aborting due to 1 previous error
2028

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait A<T> {}
2+
3+
trait B {
4+
type Type;
5+
}
6+
7+
impl<T> B for T //~ ERROR overflow evaluating the requirement
8+
where
9+
T: A<Self::Type>,
10+
{
11+
type Type = bool;
12+
}
13+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0275]: overflow evaluating the requirement `<T as B>::Type == <T as B>::Type`
2+
--> $DIR/impl-wf-cycle-3.rs:7:1
3+
|
4+
LL | / impl<T> B for T
5+
LL | | where
6+
LL | | T: A<Self::Type>,
7+
| |_____________________^
8+
LL | {
9+
LL | type Type = bool;
10+
| --------- associated type `<T as B>::Type` is specified here
11+
|
12+
note: required for `T` to implement `B`
13+
--> $DIR/impl-wf-cycle-3.rs:7:9
14+
|
15+
LL | impl<T> B for T
16+
| ^ ^
17+
LL | where
18+
LL | T: A<Self::Type>,
19+
| ------------- unsatisfied trait bound introduced here
20+
help: replace the associated type with the type specified in this `impl`
21+
|
22+
LL | T: A<bool>,
23+
| ~~~~
24+
25+
error: aborting due to 1 previous error
26+
27+
For more information about this error, try `rustc --explain E0275`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
trait Filter {
2+
type ToMatch;
3+
}
4+
5+
impl<T> Filter for T //~ ERROR overflow evaluating the requirement
6+
where
7+
T: Fn(Self::ToMatch),
8+
{
9+
}
10+
11+
struct JustFilter<F: Filter> {
12+
filter: F,
13+
}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0275]: overflow evaluating the requirement `<T as Filter>::ToMatch == <T as Filter>::ToMatch`
2+
--> $DIR/impl-wf-cycle-4.rs:5:1
3+
|
4+
LL | / impl<T> Filter for T
5+
LL | | where
6+
LL | | T: Fn(Self::ToMatch),
7+
| |_________________________^
8+
|
9+
note: required for `T` to implement `Filter`
10+
--> $DIR/impl-wf-cycle-4.rs:5:9
11+
|
12+
LL | impl<T> Filter for T
13+
| ^^^^^^ ^
14+
LL | where
15+
LL | T: Fn(Self::ToMatch),
16+
| ----------------- unsatisfied trait bound introduced here
17+
note: associated types for the current `impl` cannot be restricted in `where` clauses
18+
--> $DIR/impl-wf-cycle-4.rs:7:11
19+
|
20+
LL | T: Fn(Self::ToMatch),
21+
| ^^^^^^^^^^^^^
22+
23+
error: aborting due to 1 previous error
24+
25+
For more information about this error, try `rustc --explain E0275`.

0 commit comments

Comments
 (0)