-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suggestion when encountering assoc types from hrtb
When encountering E0212, detect whether this is a representable case or not, i.e. if it's happening on an `fn` or on an ADT. If the former, provide a structured suggestion, otherwise note that this can't be represented in Rust.
- Loading branch information
Showing
7 changed files
with
146 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/test/ui/associated-types/associated-types-project-from-hrtb-in-fn.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#![allow(dead_code, unused_variables)] | ||
// run-rustfix | ||
// Check projection of an associated type out of a higher-ranked trait-bound | ||
// in the context of a function signature. | ||
|
||
pub trait Foo<T> { | ||
type A; | ||
|
||
fn get(&self, t: T) -> Self::A; | ||
} | ||
|
||
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 | ||
{ | ||
// This case is illegal because we have to instantiate `'x`, and | ||
// we don't know what region to instantiate it with. | ||
// | ||
// This could perhaps be made equivalent to the examples below, | ||
// specifically for fn signatures. | ||
} | ||
|
||
fn foo3<I : for<'x> Foo<&'x isize>>( | ||
x: <I as Foo<&isize>>::A) | ||
{ | ||
// OK, in this case we spelled out the precise regions involved, though we left one of | ||
// them anonymous. | ||
} | ||
|
||
fn foo4<'a, I : for<'x> Foo<&'x isize>>( | ||
x: <I as Foo<&'a isize>>::A) | ||
{ | ||
// OK, in this case we spelled out the precise regions involved. | ||
} | ||
|
||
|
||
pub fn main() {} |
2 changes: 2 additions & 0 deletions
2
src/test/ui/associated-types/associated-types-project-from-hrtb-in-fn.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/test/ui/associated-types/associated-types-project-from-hrtb-in-fn.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context | ||
--> $DIR/associated-types-project-from-hrtb-in-fn.rs:11:8 | ||
--> $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 | ||
|
38 changes: 38 additions & 0 deletions
38
src/test/ui/associated-types/associated-types-project-from-hrtb-in-trait-method.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#![allow(dead_code)] | ||
// run-rustfix | ||
// Check projection of an associated type out of a higher-ranked trait-bound | ||
// in the context of a method definition in a trait. | ||
|
||
pub trait Foo<T> { | ||
type A; | ||
|
||
fn get(&self, t: T) -> Self::A; | ||
} | ||
|
||
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 | ||
} | ||
|
||
trait AnotherTrait<I : for<'x> Foo<&'x isize>> { | ||
fn some_method(&self, arg: <I as Foo<&isize>>::A); | ||
} | ||
|
||
trait YetAnotherTrait<I : for<'x> Foo<&'x isize>> { | ||
fn some_method<'a>(&self, arg: <I as Foo<&'a isize>>::A); | ||
} | ||
|
||
trait Banana<'a> { | ||
type Assoc: Default; | ||
} | ||
|
||
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 | ||
Default::default() | ||
} | ||
} | ||
|
||
pub fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 9 additions & 3 deletions
12
src/test/ui/associated-types/associated-types-project-from-hrtb-in-trait-method.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context | ||
--> $DIR/associated-types-project-from-hrtb-in-trait-method.rs:11:32 | ||
--> $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: aborting due to previous error | ||
error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context | ||
--> $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 | ||
|