Skip to content

Commit

Permalink
Add more tests for generic associated type bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Feb 13, 2021
1 parent d785c8c commit eeb82e4
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// run-pass

#![allow(incomplete_features)]
#![feature(generic_associated_types)]

pub trait X {
type Y<'a>;
fn m(&self) -> Self::Y<'_>;
}

impl X for () {
type Y<'a> = &'a ();

fn m(&self) -> Self::Y<'_> {
self
}
}

fn f(x: &impl for<'a> X<Y<'a> = &'a ()>) -> &() {
x.m()
}

fn g<T: for<'a> X<Y<'a> = &'a ()>>(x: &T) -> &() {
x.m()
}

fn h(x: &()) -> &() {
x.m()
}

fn main() {
f(&());
g(&());
h(&());
}
6 changes: 2 additions & 4 deletions src/test/ui/generic-associated-types/issue-76535.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#![feature(generic_associated_types)]
//~^ WARNING the feature
//~^ WARNING the feature

pub trait SubTrait {}

pub trait SuperTrait {
type SubType<'a>: SubTrait;
//~^ ERROR missing generics for associated
//~^ ERROR missing generics for associated

fn get_sub<'a>(&'a mut self) -> Self::SubType<'a>;
}
Expand Down Expand Up @@ -36,6 +36,4 @@ impl SuperTrait for SuperStruct {

fn main() {
let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
//~^ ERROR the trait
//~| ERROR the trait
}
39 changes: 2 additions & 37 deletions src/test/ui/generic-associated-types/issue-76535.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,6 @@ help: use angle brackets to add missing lifetime argument
LL | type SubType<'a><'a>: SubTrait;
| ^^^^

error[E0038]: the trait `SuperTrait` cannot be made into an object
--> $DIR/issue-76535.rs:38:14
|
LL | let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `SuperTrait` cannot be made into an object
|
= help: consider moving `get_sub` to another trait
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/issue-76535.rs:10:37
|
LL | pub trait SuperTrait {
| ---------- this trait cannot be made into an object...
...
LL | fn get_sub<'a>(&'a mut self) -> Self::SubType<'a>;
| ^^^^^^^^^^^^^^^^^ ...because method `get_sub` references the `Self` type in its return type

error[E0038]: the trait `SuperTrait` cannot be made into an object
--> $DIR/issue-76535.rs:38:57
|
LL | let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `SuperTrait` cannot be made into an object
|
= help: consider moving `get_sub` to another trait
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/issue-76535.rs:10:37
|
LL | pub trait SuperTrait {
| ---------- this trait cannot be made into an object...
...
LL | fn get_sub<'a>(&'a mut self) -> Self::SubType<'a>;
| ^^^^^^^^^^^^^^^^^ ...because method `get_sub` references the `Self` type in its return type
= note: required because of the requirements on the impl of `CoerceUnsized<Box<dyn SuperTrait<SubType = SubStruct<'_>>>>` for `Box<SuperStruct>`
= note: required by cast to type `Box<dyn SuperTrait<SubType = SubStruct<'_>>>`

error: aborting due to 3 previous errors; 1 warning emitted
error: aborting due to previous error; 1 warning emitted

Some errors have detailed explanations: E0038, E0107.
For more information about an error, try `rustc --explain E0038`.
For more information about this error, try `rustc --explain E0107`.
5 changes: 2 additions & 3 deletions src/test/ui/generic-associated-types/issue-79422.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<'a, T> RefCont<'a, T> for Box<T> {

trait MapLike<K, V> {
type VRefCont<'a>: RefCont<'a, V>;
//~^ ERROR missing generics
//~^ ERROR missing generics
fn get<'a>(&'a self, key: &K) -> Option<Self::VRefCont<'a>>;
}

Expand All @@ -42,6 +42,5 @@ impl<K, V: Default> MapLike<K, V> for Source {
fn main() {
let m = Box::new(std::collections::BTreeMap::<u8, u8>::new())
as Box<dyn MapLike<u8, u8, VRefCont = dyn RefCont<'_, u8>>>;
//~^ ERROR the trait
//~^^^ ERROR the trait
//~^^ ERROR type mismatch resolving
}
40 changes: 8 additions & 32 deletions src/test/ui/generic-associated-types/issue-79422.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,17 @@ help: use angle brackets to add missing lifetime argument
LL | type VRefCont<'a><'a>: RefCont<'a, V>;
| ^^^^

error[E0038]: the trait `MapLike` cannot be made into an object
--> $DIR/issue-79422.rs:44:12
|
LL | as Box<dyn MapLike<u8, u8, VRefCont = dyn RefCont<'_, u8>>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `MapLike` cannot be made into an object
|
= help: consider moving `get` to another trait
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/issue-79422.rs:23:38
|
LL | trait MapLike<K, V> {
| ------- this trait cannot be made into an object...
...
LL | fn get<'a>(&'a self, key: &K) -> Option<Self::VRefCont<'a>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ ...because method `get` references the `Self` type in its return type

error[E0038]: the trait `MapLike` cannot be made into an object
error[E0271]: type mismatch resolving `<BTreeMap<u8, u8> as MapLike<u8, u8>>::VRefCont<'static> == (dyn RefCont<'_, u8> + 'static)`
--> $DIR/issue-79422.rs:43:13
|
LL | let m = Box::new(std::collections::BTreeMap::<u8, u8>::new())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `MapLike` cannot be made into an object
|
= help: consider moving `get` to another trait
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $DIR/issue-79422.rs:23:38
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn RefCont`, found reference
|
LL | trait MapLike<K, V> {
| ------- this trait cannot be made into an object...
...
LL | fn get<'a>(&'a self, key: &K) -> Option<Self::VRefCont<'a>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ ...because method `get` references the `Self` type in its return type
= note: required because of the requirements on the impl of `CoerceUnsized<Box<dyn MapLike<u8, u8, VRefCont = (dyn RefCont<'_, u8> + 'static)>>>` for `Box<BTreeMap<u8, u8>>`
= note: required by cast to type `Box<dyn MapLike<u8, u8, VRefCont = (dyn RefCont<'_, u8> + 'static)>>`
= note: expected trait object `(dyn RefCont<'_, u8> + 'static)`
found reference `&'static u8`
= note: required for the cast to the object type `dyn MapLike<u8, u8, VRefCont = (dyn RefCont<'_, u8> + 'static)>`

error: aborting due to 3 previous errors
error: aborting due to 2 previous errors

Some errors have detailed explanations: E0038, E0107.
For more information about an error, try `rustc --explain E0038`.
Some errors have detailed explanations: E0107, E0271.
For more information about an error, try `rustc --explain E0107`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![allow(incomplete_features)]
#![feature(generic_associated_types)]

pub trait X {
type Y<'a>;
fn m(&self) -> Self::Y<'_>;
}

impl X for () {
type Y<'a> = &'a ();

fn m(&self) -> Self::Y<'_> {
self
}
}

fn f(x: &impl for<'a> X<Y<'a> = &'a ()>) -> &'static () {
x.m()
//~^ ERROR explicit lifetime required
}

fn g<T: for<'a> X<Y<'a> = &'a ()>>(x: &T) -> &'static () {
x.m()
//~^ ERROR explicit lifetime required
}

fn h(x: &()) -> &'static () {
x.m()
//~^ ERROR explicit lifetime required
}

fn main() {
f(&());
g(&());
h(&());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0621]: explicit lifetime required in the type of `x`
--> $DIR/projection-type-lifetime-mismatch.rs:18:5
|
LL | fn f(x: &impl for<'a> X<Y<'a> = &'a ()>) -> &'static () {
| ------------------------------- help: add explicit lifetime `'static` to the type of `x`: `&'static impl for<'a> X<Y<'a> = &'a ()>`
LL | x.m()
| ^^^^^ lifetime `'static` required

error[E0621]: explicit lifetime required in the type of `x`
--> $DIR/projection-type-lifetime-mismatch.rs:23:5
|
LL | fn g<T: for<'a> X<Y<'a> = &'a ()>>(x: &T) -> &'static () {
| -- help: add explicit lifetime `'static` to the type of `x`: `&'static T`
LL | x.m()
| ^^^^^ lifetime `'static` required

error[E0621]: explicit lifetime required in the type of `x`
--> $DIR/projection-type-lifetime-mismatch.rs:28:5
|
LL | fn h(x: &()) -> &'static () {
| --- help: add explicit lifetime `'static` to the type of `x`: `&'static ()`
LL | x.m()
| ^^^^^ lifetime `'static` required

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0621`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#![allow(incomplete_features)]
#![feature(generic_associated_types)]

pub trait X {
type Y<'a: 'static>;
//~^ WARNING unnecessary lifetime parameter
}

impl X for () {
type Y<'a> = &'a ();
}

struct B<'a, T: for<'r> X<Y<'r> = &'r ()>> {
f: <T as X>::Y<'a>,
//~^ ERROR lifetime bound not satisfied
}

struct C<'a, T: X> {
f: <T as X>::Y<'a>,
//~^ ERROR lifetime bound not satisfied
}

struct D<'a> {
f: <() as X>::Y<'a>,
//~^ ERROR lifetime bound not satisfied
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
warning: unnecessary lifetime parameter `'a`
--> $DIR/unsatified-item-lifetime-bound.rs:5:12
|
LL | type Y<'a: 'static>;
| ^^^^^^^^^^^
|
= help: you can use the `'static` lifetime directly, in place of `'a`

error[E0478]: lifetime bound not satisfied
--> $DIR/unsatified-item-lifetime-bound.rs:14:8
|
LL | f: <T as X>::Y<'a>,
| ^^^^^^^^^^^^^^^
|
note: lifetime parameter instantiated with the lifetime `'a` as defined on the struct at 13:10
--> $DIR/unsatified-item-lifetime-bound.rs:13:10
|
LL | struct B<'a, T: for<'r> X<Y<'r> = &'r ()>> {
| ^^
= note: but lifetime parameter must outlive the static lifetime

error[E0478]: lifetime bound not satisfied
--> $DIR/unsatified-item-lifetime-bound.rs:19:8
|
LL | f: <T as X>::Y<'a>,
| ^^^^^^^^^^^^^^^
|
note: lifetime parameter instantiated with the lifetime `'a` as defined on the struct at 18:10
--> $DIR/unsatified-item-lifetime-bound.rs:18:10
|
LL | struct C<'a, T: X> {
| ^^
= note: but lifetime parameter must outlive the static lifetime

error[E0478]: lifetime bound not satisfied
--> $DIR/unsatified-item-lifetime-bound.rs:24:8
|
LL | f: <() as X>::Y<'a>,
| ^^^^^^^^^^^^^^^^
|
note: lifetime parameter instantiated with the lifetime `'a` as defined on the struct at 23:10
--> $DIR/unsatified-item-lifetime-bound.rs:23:10
|
LL | struct D<'a> {
| ^^
= note: but lifetime parameter must outlive the static lifetime

error: aborting due to 3 previous errors; 1 warning emitted

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

0 comments on commit eeb82e4

Please sign in to comment.