Skip to content

Commit

Permalink
Rollup merge of #79639 - sasurau4:feature/add-long-explanation-E0212,…
Browse files Browse the repository at this point in the history
… r=GuillaumeGomez

Add long explanation for E0212

Helps with #61137
  • Loading branch information
tmandry authored Dec 11, 2020
2 parents a8c19e1 + 87c6216 commit f3a3fc9
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 20 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_error_codes/src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ E0206: include_str!("./error_codes/E0206.md"),
E0207: include_str!("./error_codes/E0207.md"),
E0210: include_str!("./error_codes/E0210.md"),
E0211: include_str!("./error_codes/E0211.md"),
E0212: include_str!("./error_codes/E0212.md"),
E0214: include_str!("./error_codes/E0214.md"),
E0220: include_str!("./error_codes/E0220.md"),
E0221: include_str!("./error_codes/E0221.md"),
Expand Down Expand Up @@ -503,7 +504,6 @@ E0779: include_str!("./error_codes/E0779.md"),
// E0196, // cannot determine a type for this closure
E0208,
// E0209, // builtin traits can only be implemented on structs or enums
E0212, // cannot extract an associated type from a higher-ranked trait bound
// E0213, // associated types are not accepted in this context
// E0215, // angle-bracket notation is not stable with `Fn`
// E0216, // parenthetical notation is only stable with `Fn`
Expand Down
35 changes: 35 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0212.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Cannot use the associated type of
a trait with uninferred generic parameters.

Erroneous code example:

```compile_fail,E0212
pub trait Foo<T> {
type A;
fn get(&self, t: T) -> Self::A;
}
fn foo2<I : for<'x> Foo<&'x isize>>(
field: I::A) {} // error!
```

In this example, we have to instantiate `'x`, and
we don't know what lifetime to instantiate it with.
To fix this, spell out the precise lifetimes involved.
Example:

```
pub trait Foo<T> {
type A;
fn get(&self, t: T) -> Self::A;
}
fn foo3<I : for<'x> Foo<&'x isize>>(
x: <I as Foo<&isize>>::A) {} // ok!
fn foo4<'a, I : for<'x> Foo<&'x isize>>(
x: <I as Foo<&'a isize>>::A) {} // ok!
```
4 changes: 2 additions & 2 deletions compiler/rustc_typeck/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ impl AstConv<'tcx> for ItemCtxt<'tcx> {
self.tcx().sess,
span,
E0212,
"cannot extract an associated type from a higher-ranked trait bound \
in this context"
"cannot use the associated type of a trait \
with uninferred generic parameters"
);

match self.node() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub trait Foo<T> {

fn foo2<I : for<'x> Foo<&'x isize>>(
x: <I as Foo<&isize>>::A)
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
//~^ ERROR cannot use the associated type of a trait with uninferred generic parameters
{
// This case is illegal because we have to instantiate `'x`, and
// we don't know what region to instantiate it with.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub trait Foo<T> {

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

error: aborting due to previous error

For more information about this error, try `rustc --explain E0212`.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ pub trait Foo<T> {

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

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

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

pub fn main() {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context
error[E0212]: cannot use the associated type of a trait with uninferred generic parameters
--> $DIR/associated-types-project-from-hrtb-in-struct.rs:11:12
|
LL | field: I::A
Expand All @@ -10,7 +10,7 @@ LL | struct SomeStruct<'a, I: for<'x> Foo<&'x isize>> {
LL | field: <I as Foo<&'a isize>>::A
|

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

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

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

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0212`.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub trait Foo<T> {

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

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

impl<X: for<'a> Banana<'a>> Peach<X> {
fn mango(&self) -> <X as Banana<'_>>::Assoc {
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
//~^ ERROR cannot use the associated type of a trait with uninferred generic parameters
Default::default()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub trait Foo<T> {

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

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

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

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

error: aborting due to 2 previous errors

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

0 comments on commit f3a3fc9

Please sign in to comment.