Rollup merge of #69048 - estebank:hrlt-assoc, r=nagisa
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. Fix #69000.
This commit is contained in:
commit
8d00adf289
9 changed files with 226 additions and 20 deletions
|
|
@ -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() {}
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
#![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.
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -7,18 +7,25 @@ pub trait Foo<T> {
|
|||
fn get(&self, t: T) -> Self::A;
|
||||
}
|
||||
|
||||
struct SomeStruct<I : for<'x> Foo<&'x isize>> {
|
||||
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
|
||||
}
|
||||
|
||||
enum SomeEnum<I: for<'x> Foo<&'x isize>> {
|
||||
TupleVariant(I::A),
|
||||
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
|
||||
StructVariant { field: I::A },
|
||||
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
|
||||
}
|
||||
|
||||
// FIXME(eddyb) This one doesn't even compile because of the unsupported syntax.
|
||||
|
||||
// struct AnotherStruct<I : for<'x> Foo<&'x isize>> {
|
||||
// field: <I as for<'y> Foo<&'y isize>>::A
|
||||
// }
|
||||
|
||||
struct YetAnotherStruct<'a, I : for<'x> Foo<&'x isize>> {
|
||||
struct YetAnotherStruct<'a, I: for<'x> Foo<&'x isize>> {
|
||||
field: <I as Foo<&'a isize>>::A
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,6 +3,38 @@ error[E0212]: cannot extract an associated type from a higher-ranked trait bound
|
|||
|
|
||||
LL | field: I::A
|
||||
| ^^^^
|
||||
|
|
||||
help: use a fully qualified path with explicit lifetimes
|
||||
|
|
||||
LL | struct SomeStruct<'a, I: for<'x> Foo<&'x isize>> {
|
||||
LL | field: <I as Foo<&'a 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-struct.rs:16:18
|
||||
|
|
||||
LL | TupleVariant(I::A),
|
||||
| ^^^^
|
||||
|
|
||||
help: use a fully qualified path with explicit lifetimes
|
||||
|
|
||||
LL | enum SomeEnum<'a, I: for<'x> Foo<&'x isize>> {
|
||||
LL | TupleVariant(<I as Foo<&'a isize>>::A),
|
||||
|
|
||||
|
||||
error[E0212]: cannot extract an associated type from a higher-ranked trait bound in this context
|
||||
--> $DIR/associated-types-project-from-hrtb-in-struct.rs:18:28
|
||||
|
|
||||
LL | StructVariant { field: I::A },
|
||||
| ^^^^
|
||||
|
|
||||
help: use a fully qualified path with explicit lifetimes
|
||||
|
|
||||
LL | enum SomeEnum<'a, I: for<'x> Foo<&'x isize>> {
|
||||
LL | TupleVariant(I::A),
|
||||
LL |
|
||||
LL | StructVariant { field: <I as Foo<&'a isize>>::A },
|
||||
|
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
|
|
|
|||
|
|
@ -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() {}
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
#![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.
|
||||
|
||||
|
|
@ -20,4 +22,17 @@ 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::Assoc {
|
||||
//~^ ERROR cannot extract an associated type from a higher-ranked trait bound in this context
|
||||
Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn main() {}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue