Skip to content

Commit eeb82e4

Browse files
committed
Add more tests for generic associated type bounds
1 parent d785c8c commit eeb82e4

9 files changed

+190
-76
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// run-pass
2+
3+
#![allow(incomplete_features)]
4+
#![feature(generic_associated_types)]
5+
6+
pub trait X {
7+
type Y<'a>;
8+
fn m(&self) -> Self::Y<'_>;
9+
}
10+
11+
impl X for () {
12+
type Y<'a> = &'a ();
13+
14+
fn m(&self) -> Self::Y<'_> {
15+
self
16+
}
17+
}
18+
19+
fn f(x: &impl for<'a> X<Y<'a> = &'a ()>) -> &() {
20+
x.m()
21+
}
22+
23+
fn g<T: for<'a> X<Y<'a> = &'a ()>>(x: &T) -> &() {
24+
x.m()
25+
}
26+
27+
fn h(x: &()) -> &() {
28+
x.m()
29+
}
30+
31+
fn main() {
32+
f(&());
33+
g(&());
34+
h(&());
35+
}

src/test/ui/generic-associated-types/issue-76535.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#![feature(generic_associated_types)]
2-
//~^ WARNING the feature
2+
//~^ WARNING the feature
33

44
pub trait SubTrait {}
55

66
pub trait SuperTrait {
77
type SubType<'a>: SubTrait;
8-
//~^ ERROR missing generics for associated
8+
//~^ ERROR missing generics for associated
99

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

3737
fn main() {
3838
let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
39-
//~^ ERROR the trait
40-
//~| ERROR the trait
4139
}

src/test/ui/generic-associated-types/issue-76535.stderr

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,41 +23,6 @@ help: use angle brackets to add missing lifetime argument
2323
LL | type SubType<'a><'a>: SubTrait;
2424
| ^^^^
2525

26-
error[E0038]: the trait `SuperTrait` cannot be made into an object
27-
--> $DIR/issue-76535.rs:38:14
28-
|
29-
LL | let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
30-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `SuperTrait` cannot be made into an object
31-
|
32-
= help: consider moving `get_sub` to another trait
33-
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>
34-
--> $DIR/issue-76535.rs:10:37
35-
|
36-
LL | pub trait SuperTrait {
37-
| ---------- this trait cannot be made into an object...
38-
...
39-
LL | fn get_sub<'a>(&'a mut self) -> Self::SubType<'a>;
40-
| ^^^^^^^^^^^^^^^^^ ...because method `get_sub` references the `Self` type in its return type
41-
42-
error[E0038]: the trait `SuperTrait` cannot be made into an object
43-
--> $DIR/issue-76535.rs:38:57
44-
|
45-
LL | let sub: Box<dyn SuperTrait<SubType = SubStruct>> = Box::new(SuperStruct::new(0));
46-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `SuperTrait` cannot be made into an object
47-
|
48-
= help: consider moving `get_sub` to another trait
49-
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>
50-
--> $DIR/issue-76535.rs:10:37
51-
|
52-
LL | pub trait SuperTrait {
53-
| ---------- this trait cannot be made into an object...
54-
...
55-
LL | fn get_sub<'a>(&'a mut self) -> Self::SubType<'a>;
56-
| ^^^^^^^^^^^^^^^^^ ...because method `get_sub` references the `Self` type in its return type
57-
= note: required because of the requirements on the impl of `CoerceUnsized<Box<dyn SuperTrait<SubType = SubStruct<'_>>>>` for `Box<SuperStruct>`
58-
= note: required by cast to type `Box<dyn SuperTrait<SubType = SubStruct<'_>>>`
59-
60-
error: aborting due to 3 previous errors; 1 warning emitted
26+
error: aborting due to previous error; 1 warning emitted
6127

62-
Some errors have detailed explanations: E0038, E0107.
63-
For more information about an error, try `rustc --explain E0038`.
28+
For more information about this error, try `rustc --explain E0107`.

src/test/ui/generic-associated-types/issue-79422.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl<'a, T> RefCont<'a, T> for Box<T> {
1919

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

@@ -42,6 +42,5 @@ impl<K, V: Default> MapLike<K, V> for Source {
4242
fn main() {
4343
let m = Box::new(std::collections::BTreeMap::<u8, u8>::new())
4444
as Box<dyn MapLike<u8, u8, VRefCont = dyn RefCont<'_, u8>>>;
45-
//~^ ERROR the trait
46-
//~^^^ ERROR the trait
45+
//~^^ ERROR type mismatch resolving
4746
}

src/test/ui/generic-associated-types/issue-79422.stderr

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,17 @@ help: use angle brackets to add missing lifetime argument
1414
LL | type VRefCont<'a><'a>: RefCont<'a, V>;
1515
| ^^^^
1616

17-
error[E0038]: the trait `MapLike` cannot be made into an object
18-
--> $DIR/issue-79422.rs:44:12
19-
|
20-
LL | as Box<dyn MapLike<u8, u8, VRefCont = dyn RefCont<'_, u8>>>;
21-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `MapLike` cannot be made into an object
22-
|
23-
= help: consider moving `get` to another trait
24-
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>
25-
--> $DIR/issue-79422.rs:23:38
26-
|
27-
LL | trait MapLike<K, V> {
28-
| ------- this trait cannot be made into an object...
29-
...
30-
LL | fn get<'a>(&'a self, key: &K) -> Option<Self::VRefCont<'a>>;
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ ...because method `get` references the `Self` type in its return type
32-
33-
error[E0038]: the trait `MapLike` cannot be made into an object
17+
error[E0271]: type mismatch resolving `<BTreeMap<u8, u8> as MapLike<u8, u8>>::VRefCont<'static> == (dyn RefCont<'_, u8> + 'static)`
3418
--> $DIR/issue-79422.rs:43:13
3519
|
3620
LL | let m = Box::new(std::collections::BTreeMap::<u8, u8>::new())
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `MapLike` cannot be made into an object
38-
|
39-
= help: consider moving `get` to another trait
40-
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>
41-
--> $DIR/issue-79422.rs:23:38
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected trait object `dyn RefCont`, found reference
4222
|
43-
LL | trait MapLike<K, V> {
44-
| ------- this trait cannot be made into an object...
45-
...
46-
LL | fn get<'a>(&'a self, key: &K) -> Option<Self::VRefCont<'a>>;
47-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ ...because method `get` references the `Self` type in its return type
48-
= 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>>`
49-
= note: required by cast to type `Box<dyn MapLike<u8, u8, VRefCont = (dyn RefCont<'_, u8> + 'static)>>`
23+
= note: expected trait object `(dyn RefCont<'_, u8> + 'static)`
24+
found reference `&'static u8`
25+
= note: required for the cast to the object type `dyn MapLike<u8, u8, VRefCont = (dyn RefCont<'_, u8> + 'static)>`
5026

51-
error: aborting due to 3 previous errors
27+
error: aborting due to 2 previous errors
5228

53-
Some errors have detailed explanations: E0038, E0107.
54-
For more information about an error, try `rustc --explain E0038`.
29+
Some errors have detailed explanations: E0107, E0271.
30+
For more information about an error, try `rustc --explain E0107`.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![allow(incomplete_features)]
2+
#![feature(generic_associated_types)]
3+
4+
pub trait X {
5+
type Y<'a>;
6+
fn m(&self) -> Self::Y<'_>;
7+
}
8+
9+
impl X for () {
10+
type Y<'a> = &'a ();
11+
12+
fn m(&self) -> Self::Y<'_> {
13+
self
14+
}
15+
}
16+
17+
fn f(x: &impl for<'a> X<Y<'a> = &'a ()>) -> &'static () {
18+
x.m()
19+
//~^ ERROR explicit lifetime required
20+
}
21+
22+
fn g<T: for<'a> X<Y<'a> = &'a ()>>(x: &T) -> &'static () {
23+
x.m()
24+
//~^ ERROR explicit lifetime required
25+
}
26+
27+
fn h(x: &()) -> &'static () {
28+
x.m()
29+
//~^ ERROR explicit lifetime required
30+
}
31+
32+
fn main() {
33+
f(&());
34+
g(&());
35+
h(&());
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0621]: explicit lifetime required in the type of `x`
2+
--> $DIR/projection-type-lifetime-mismatch.rs:18:5
3+
|
4+
LL | fn f(x: &impl for<'a> X<Y<'a> = &'a ()>) -> &'static () {
5+
| ------------------------------- help: add explicit lifetime `'static` to the type of `x`: `&'static impl for<'a> X<Y<'a> = &'a ()>`
6+
LL | x.m()
7+
| ^^^^^ lifetime `'static` required
8+
9+
error[E0621]: explicit lifetime required in the type of `x`
10+
--> $DIR/projection-type-lifetime-mismatch.rs:23:5
11+
|
12+
LL | fn g<T: for<'a> X<Y<'a> = &'a ()>>(x: &T) -> &'static () {
13+
| -- help: add explicit lifetime `'static` to the type of `x`: `&'static T`
14+
LL | x.m()
15+
| ^^^^^ lifetime `'static` required
16+
17+
error[E0621]: explicit lifetime required in the type of `x`
18+
--> $DIR/projection-type-lifetime-mismatch.rs:28:5
19+
|
20+
LL | fn h(x: &()) -> &'static () {
21+
| --- help: add explicit lifetime `'static` to the type of `x`: `&'static ()`
22+
LL | x.m()
23+
| ^^^^^ lifetime `'static` required
24+
25+
error: aborting due to 3 previous errors
26+
27+
For more information about this error, try `rustc --explain E0621`.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![allow(incomplete_features)]
2+
#![feature(generic_associated_types)]
3+
4+
pub trait X {
5+
type Y<'a: 'static>;
6+
//~^ WARNING unnecessary lifetime parameter
7+
}
8+
9+
impl X for () {
10+
type Y<'a> = &'a ();
11+
}
12+
13+
struct B<'a, T: for<'r> X<Y<'r> = &'r ()>> {
14+
f: <T as X>::Y<'a>,
15+
//~^ ERROR lifetime bound not satisfied
16+
}
17+
18+
struct C<'a, T: X> {
19+
f: <T as X>::Y<'a>,
20+
//~^ ERROR lifetime bound not satisfied
21+
}
22+
23+
struct D<'a> {
24+
f: <() as X>::Y<'a>,
25+
//~^ ERROR lifetime bound not satisfied
26+
}
27+
28+
fn main() {}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
warning: unnecessary lifetime parameter `'a`
2+
--> $DIR/unsatified-item-lifetime-bound.rs:5:12
3+
|
4+
LL | type Y<'a: 'static>;
5+
| ^^^^^^^^^^^
6+
|
7+
= help: you can use the `'static` lifetime directly, in place of `'a`
8+
9+
error[E0478]: lifetime bound not satisfied
10+
--> $DIR/unsatified-item-lifetime-bound.rs:14:8
11+
|
12+
LL | f: <T as X>::Y<'a>,
13+
| ^^^^^^^^^^^^^^^
14+
|
15+
note: lifetime parameter instantiated with the lifetime `'a` as defined on the struct at 13:10
16+
--> $DIR/unsatified-item-lifetime-bound.rs:13:10
17+
|
18+
LL | struct B<'a, T: for<'r> X<Y<'r> = &'r ()>> {
19+
| ^^
20+
= note: but lifetime parameter must outlive the static lifetime
21+
22+
error[E0478]: lifetime bound not satisfied
23+
--> $DIR/unsatified-item-lifetime-bound.rs:19:8
24+
|
25+
LL | f: <T as X>::Y<'a>,
26+
| ^^^^^^^^^^^^^^^
27+
|
28+
note: lifetime parameter instantiated with the lifetime `'a` as defined on the struct at 18:10
29+
--> $DIR/unsatified-item-lifetime-bound.rs:18:10
30+
|
31+
LL | struct C<'a, T: X> {
32+
| ^^
33+
= note: but lifetime parameter must outlive the static lifetime
34+
35+
error[E0478]: lifetime bound not satisfied
36+
--> $DIR/unsatified-item-lifetime-bound.rs:24:8
37+
|
38+
LL | f: <() as X>::Y<'a>,
39+
| ^^^^^^^^^^^^^^^^
40+
|
41+
note: lifetime parameter instantiated with the lifetime `'a` as defined on the struct at 23:10
42+
--> $DIR/unsatified-item-lifetime-bound.rs:23:10
43+
|
44+
LL | struct D<'a> {
45+
| ^^
46+
= note: but lifetime parameter must outlive the static lifetime
47+
48+
error: aborting due to 3 previous errors; 1 warning emitted
49+
50+
For more information about this error, try `rustc --explain E0478`.

0 commit comments

Comments
 (0)