Skip to content

Commit fd92d88

Browse files
authored
Rollup merge of rust-lang#119389 - estebank:issue-116925, r=TaKO8Ki
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, | ``` ``` 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
2 parents 8c6cf3c + 29bdf9e commit fd92d88

13 files changed

+474
-45
lines changed

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

+195-45
Large diffs are not rendered by default.

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

+7
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,10 @@ 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+
|
2128

2229
error: aborting due to 1 previous error
2330

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 - where
24+
LL - Self::A: Copy,
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`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// run-rustfix
2+
3+
trait Baz {}
4+
impl Baz for () {}
5+
impl<T> Baz for (T,) {}
6+
7+
trait Fiz {}
8+
impl Fiz for bool {}
9+
10+
trait Grault {
11+
type A;
12+
type B;
13+
}
14+
15+
impl Grault for () {
16+
type A = ();
17+
type B = bool;
18+
}
19+
20+
impl<T> Grault for (T,)
21+
//~^ ERROR overflow evaluating the requirement `<(T,) as Grault>::A == _`
22+
where
23+
T: Grault,
24+
{
25+
type A = ();
26+
type B = bool;
27+
}
28+
29+
fn main() {
30+
let _: <((),) as Grault>::A = ();
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// run-rustfix
2+
3+
trait Baz {}
4+
impl Baz for () {}
5+
impl<T> Baz for (T,) {}
6+
7+
trait Fiz {}
8+
impl Fiz for bool {}
9+
10+
trait Grault {
11+
type A;
12+
type B;
13+
}
14+
15+
impl Grault for () {
16+
type A = ();
17+
type B = bool;
18+
}
19+
20+
impl<T> Grault for (T,)
21+
//~^ ERROR overflow evaluating the requirement `<(T,) as Grault>::A == _`
22+
where
23+
T: Grault,
24+
Self::A: Baz,
25+
{
26+
type A = ();
27+
type B = bool;
28+
}
29+
30+
fn main() {
31+
let _: <((),) as Grault>::A = ();
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
2+
--> $DIR/impl-wf-cycle-5.rs:20:1
3+
|
4+
LL | / impl<T> Grault for (T,)
5+
LL | |
6+
LL | | where
7+
LL | | T: Grault,
8+
LL | | Self::A: Baz,
9+
| |_________________^
10+
LL | {
11+
LL | type A = ();
12+
| ------ associated type `<(T,) as Grault>::A` is specified here
13+
|
14+
note: required for `(T,)` to implement `Grault`
15+
--> $DIR/impl-wf-cycle-5.rs:20:9
16+
|
17+
LL | impl<T> Grault for (T,)
18+
| ^^^^^^ ^^^^
19+
...
20+
LL | Self::A: Baz,
21+
| --- unsatisfied trait bound introduced here
22+
help: associated type for the current `impl` cannot be restricted in `where` clauses, remove this bound
23+
|
24+
LL - T: Grault,
25+
LL - Self::A: Baz,
26+
LL + T: Grault,
27+
|
28+
29+
error: aborting due to 1 previous error
30+
31+
For more information about this error, try `rustc --explain E0275`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// run-rustfix
2+
3+
trait Baz {}
4+
impl Baz for () {}
5+
impl<T> Baz for (T,) {}
6+
7+
trait Fiz {}
8+
impl Fiz for bool {}
9+
10+
trait Grault {
11+
type A;
12+
type B;
13+
}
14+
15+
impl Grault for () {
16+
type A = ();
17+
type B = bool;
18+
}
19+
20+
impl<T: Grault> Grault for (T,)
21+
//~^ ERROR overflow evaluating the requirement `<(T,) as Grault>::A == _`
22+
23+
{
24+
type A = ();
25+
type B = bool;
26+
}
27+
28+
fn main() {
29+
let _: <((),) as Grault>::A = ();
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// run-rustfix
2+
3+
trait Baz {}
4+
impl Baz for () {}
5+
impl<T> Baz for (T,) {}
6+
7+
trait Fiz {}
8+
impl Fiz for bool {}
9+
10+
trait Grault {
11+
type A;
12+
type B;
13+
}
14+
15+
impl Grault for () {
16+
type A = ();
17+
type B = bool;
18+
}
19+
20+
impl<T: Grault> Grault for (T,)
21+
//~^ ERROR overflow evaluating the requirement `<(T,) as Grault>::A == _`
22+
where
23+
Self::A: Baz,
24+
{
25+
type A = ();
26+
type B = bool;
27+
}
28+
29+
fn main() {
30+
let _: <((),) as Grault>::A = ();
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
2+
--> $DIR/impl-wf-cycle-6.rs:20:1
3+
|
4+
LL | / impl<T: Grault> Grault for (T,)
5+
LL | |
6+
LL | | where
7+
LL | | Self::A: Baz,
8+
| |_________________^
9+
LL | {
10+
LL | type A = ();
11+
| ------ associated type `<(T,) as Grault>::A` is specified here
12+
|
13+
note: required for `(T,)` to implement `Grault`
14+
--> $DIR/impl-wf-cycle-6.rs:20:17
15+
|
16+
LL | impl<T: Grault> Grault for (T,)
17+
| ^^^^^^ ^^^^
18+
...
19+
LL | Self::A: Baz,
20+
| --- 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 - where
24+
LL - Self::A: Baz,
25+
|
26+
27+
error: aborting due to 1 previous error
28+
29+
For more information about this error, try `rustc --explain E0275`.

0 commit comments

Comments
 (0)