Skip to content

Commit f3a3fc9

Browse files
authored
Rollup merge of #79639 - sasurau4:feature/add-long-explanation-E0212, r=GuillaumeGomez
Add long explanation for E0212 Helps with #61137
2 parents a8c19e1 + 87c6216 commit f3a3fc9

11 files changed

+58
-20
lines changed

compiler/rustc_error_codes/src/error_codes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ E0206: include_str!("./error_codes/E0206.md"),
111111
E0207: include_str!("./error_codes/E0207.md"),
112112
E0210: include_str!("./error_codes/E0210.md"),
113113
E0211: include_str!("./error_codes/E0211.md"),
114+
E0212: include_str!("./error_codes/E0212.md"),
114115
E0214: include_str!("./error_codes/E0214.md"),
115116
E0220: include_str!("./error_codes/E0220.md"),
116117
E0221: include_str!("./error_codes/E0221.md"),
@@ -503,7 +504,6 @@ E0779: include_str!("./error_codes/E0779.md"),
503504
// E0196, // cannot determine a type for this closure
504505
E0208,
505506
// E0209, // builtin traits can only be implemented on structs or enums
506-
E0212, // cannot extract an associated type from a higher-ranked trait bound
507507
// E0213, // associated types are not accepted in this context
508508
// E0215, // angle-bracket notation is not stable with `Fn`
509509
// E0216, // parenthetical notation is only stable with `Fn`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Cannot use the associated type of
2+
a trait with uninferred generic parameters.
3+
4+
Erroneous code example:
5+
6+
```compile_fail,E0212
7+
pub trait Foo<T> {
8+
type A;
9+
10+
fn get(&self, t: T) -> Self::A;
11+
}
12+
13+
fn foo2<I : for<'x> Foo<&'x isize>>(
14+
field: I::A) {} // error!
15+
```
16+
17+
In this example, we have to instantiate `'x`, and
18+
we don't know what lifetime to instantiate it with.
19+
To fix this, spell out the precise lifetimes involved.
20+
Example:
21+
22+
```
23+
pub trait Foo<T> {
24+
type A;
25+
26+
fn get(&self, t: T) -> Self::A;
27+
}
28+
29+
fn foo3<I : for<'x> Foo<&'x isize>>(
30+
x: <I as Foo<&isize>>::A) {} // ok!
31+
32+
33+
fn foo4<'a, I : for<'x> Foo<&'x isize>>(
34+
x: <I as Foo<&'a isize>>::A) {} // ok!
35+
```

compiler/rustc_typeck/src/collect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ impl AstConv<'tcx> for ItemCtxt<'tcx> {
359359
self.tcx().sess,
360360
span,
361361
E0212,
362-
"cannot extract an associated type from a higher-ranked trait bound \
363-
in this context"
362+
"cannot use the associated type of a trait \
363+
with uninferred generic parameters"
364364
);
365365

366366
match self.node() {

src/test/ui/associated-types/associated-types-project-from-hrtb-in-fn.fixed

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub trait Foo<T> {
1111

1212
fn foo2<I : for<'x> Foo<&'x isize>>(
1313
x: <I as Foo<&isize>>::A)
14-
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
14+
//~^ ERROR cannot use the associated type of a trait with uninferred generic parameters
1515
{
1616
// This case is illegal because we have to instantiate `'x`, and
1717
// we don't know what region to instantiate it with.

src/test/ui/associated-types/associated-types-project-from-hrtb-in-fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub trait Foo<T> {
1111

1212
fn foo2<I : for<'x> Foo<&'x isize>>(
1313
x: I::A)
14-
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
14+
//~^ ERROR cannot use the associated type of a trait with uninferred generic parameters
1515
{
1616
// This case is illegal because we have to instantiate `'x`, and
1717
// we don't know what region to instantiate it with.
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context
1+
error[E0212]: cannot use the associated type of a trait with uninferred generic parameters
22
--> $DIR/associated-types-project-from-hrtb-in-fn.rs:13:8
33
|
44
LL | x: I::A)
55
| ^^^^ help: use a fully qualified path with inferred lifetimes: `<I as Foo<&isize>>::A`
66

77
error: aborting due to previous error
88

9+
For more information about this error, try `rustc --explain E0212`.

src/test/ui/associated-types/associated-types-project-from-hrtb-in-struct.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ pub trait Foo<T> {
99

1010
struct SomeStruct<I: for<'x> Foo<&'x isize>> {
1111
field: I::A
12-
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
12+
//~^ ERROR cannot use the associated type of a trait with uninferred generic parameters
1313
}
1414

1515
enum SomeEnum<'b, I: for<'a> Foo<&'a isize>> {
1616
TupleVariant(I::A),
17-
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
17+
//~^ ERROR cannot use the associated type of a trait with uninferred generic parameters
1818
StructVariant { field: I::A },
19-
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
19+
//~^ ERROR cannot use the associated type of a trait with uninferred generic parameters
2020
OkVariant(&'b usize),
2121
}
2222

@@ -33,7 +33,7 @@ struct YetAnotherStruct<'a, I: for<'x> Foo<&'x isize>> {
3333
struct Why<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k, 'n, 'o, 'p, 'q, 'r, 's, 't, 'u, 'v, 'w, 'x,
3434
'y, 'z, 'aa, I: for<'l, 'm> Foo<&'l &'m isize>> {
3535
field: I::A,
36-
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
36+
//~^ ERROR cannot use the associated type of a trait with uninferred generic parameters
3737
}
3838

3939
pub fn main() {}

src/test/ui/associated-types/associated-types-project-from-hrtb-in-struct.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context
1+
error[E0212]: cannot use the associated type of a trait with uninferred generic parameters
22
--> $DIR/associated-types-project-from-hrtb-in-struct.rs:11:12
33
|
44
LL | field: I::A
@@ -10,7 +10,7 @@ LL | struct SomeStruct<'a, I: for<'x> Foo<&'x isize>> {
1010
LL | field: <I as Foo<&'a isize>>::A
1111
|
1212

13-
error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context
13+
error[E0212]: cannot use the associated type of a trait with uninferred generic parameters
1414
--> $DIR/associated-types-project-from-hrtb-in-struct.rs:16:18
1515
|
1616
LL | TupleVariant(I::A),
@@ -22,7 +22,7 @@ LL | enum SomeEnum<'c, 'b, I: for<'a> Foo<&'a isize>> {
2222
LL | TupleVariant(<I as Foo<&'c isize>>::A),
2323
|
2424

25-
error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context
25+
error[E0212]: cannot use the associated type of a trait with uninferred generic parameters
2626
--> $DIR/associated-types-project-from-hrtb-in-struct.rs:18:28
2727
|
2828
LL | StructVariant { field: I::A },
@@ -36,7 +36,7 @@ LL |
3636
LL | StructVariant { field: <I as Foo<&'c isize>>::A },
3737
|
3838

39-
error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context
39+
error[E0212]: cannot use the associated type of a trait with uninferred generic parameters
4040
--> $DIR/associated-types-project-from-hrtb-in-struct.rs:35:12
4141
|
4242
LL | field: I::A,
@@ -51,3 +51,4 @@ LL | field: <I as Foo<&'bb &'bb isize>>::A,
5151

5252
error: aborting due to 4 previous errors
5353

54+
For more information about this error, try `rustc --explain E0212`.

src/test/ui/associated-types/associated-types-project-from-hrtb-in-trait-method.fixed

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub trait Foo<T> {
1111

1212
trait SomeTrait<I : for<'x> Foo<&'x isize>> {
1313
fn some_method(&self, arg: <I as Foo<&isize>>::A);
14-
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
14+
//~^ ERROR cannot use the associated type of a trait with uninferred generic parameters
1515
}
1616

1717
trait AnotherTrait<I : for<'x> Foo<&'x isize>> {
@@ -30,7 +30,7 @@ struct Peach<X>(std::marker::PhantomData<X>);
3030

3131
impl<X: for<'a> Banana<'a>> Peach<X> {
3232
fn mango(&self) -> <X as Banana<'_>>::Assoc {
33-
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
33+
//~^ ERROR cannot use the associated type of a trait with uninferred generic parameters
3434
Default::default()
3535
}
3636
}

src/test/ui/associated-types/associated-types-project-from-hrtb-in-trait-method.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub trait Foo<T> {
1111

1212
trait SomeTrait<I : for<'x> Foo<&'x isize>> {
1313
fn some_method(&self, arg: I::A);
14-
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
14+
//~^ ERROR cannot use the associated type of a trait with uninferred generic parameters
1515
}
1616

1717
trait AnotherTrait<I : for<'x> Foo<&'x isize>> {
@@ -30,7 +30,7 @@ struct Peach<X>(std::marker::PhantomData<X>);
3030

3131
impl<X: for<'a> Banana<'a>> Peach<X> {
3232
fn mango(&self) -> X::Assoc {
33-
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
33+
//~^ ERROR cannot use the associated type of a trait with uninferred generic parameters
3434
Default::default()
3535
}
3636
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context
1+
error[E0212]: cannot use the associated type of a trait with uninferred generic parameters
22
--> $DIR/associated-types-project-from-hrtb-in-trait-method.rs:13:32
33
|
44
LL | fn some_method(&self, arg: I::A);
55
| ^^^^ help: use a fully qualified path with inferred lifetimes: `<I as Foo<&isize>>::A`
66

7-
error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context
7+
error[E0212]: cannot use the associated type of a trait with uninferred generic parameters
88
--> $DIR/associated-types-project-from-hrtb-in-trait-method.rs:32:24
99
|
1010
LL | fn mango(&self) -> X::Assoc {
1111
| ^^^^^^^^ help: use a fully qualified path with inferred lifetimes: `<X as Banana<'_>>::Assoc`
1212

1313
error: aborting due to 2 previous errors
1414

15+
For more information about this error, try `rustc --explain E0212`.

0 commit comments

Comments
 (0)