Skip to content

Commit ecd9898

Browse files
authored
Rollup merge of rust-lang#49423 - gavento:gavento-dev, r=nikomatsakis
Extend tests for RFC1598 (GAT) More GAT tests, namely some usage for `Iterable` and `StreamingIterator`, shadowing (lifetimes and type params), `Collection<T>` and `CollectionFamily` from [the series](http://smallcultfollowing.com/babysteps/blog/2016/11/03/associated-type-constructors-part-2-family-traits/) with default associated types. Tracking issue: rust-lang#44265 r? @nikomatsakis Wrong GAT argument numbers / kinds and default values are next.
2 parents 0a223d1 + 9073c89 commit ecd9898

12 files changed

+401
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(generic_associated_types)]
12+
#![feature(associated_type_defaults)]
13+
14+
//FIXME(#44265): "lifetime parameters are not allowed on this type" errors will be addressed in a
15+
//follow-up PR
16+
17+
// A Collection trait and collection families. Based on
18+
// http://smallcultfollowing.com/babysteps/blog/2016/11/03/
19+
// associated-type-constructors-part-2-family-traits/
20+
21+
trait Collection<T> {
22+
type Iter<'iter>: Iterator<Item=&'iter T>;
23+
type Family: CollectionFamily;
24+
// Test associated type defaults with parameters
25+
type Sibling<U>: Collection<U> =
26+
<<Self as Collection<T>>::Family as CollectionFamily>::Member<U>;
27+
//~^ ERROR type parameters are not allowed on this type [E0109]
28+
29+
fn empty() -> Self;
30+
31+
fn add(&mut self, value: T);
32+
33+
fn iterate<'iter>(&'iter self) -> Self::Iter<'iter>;
34+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
35+
}
36+
37+
trait CollectionFamily {
38+
type Member<T>: Collection<T, Family = Self>;
39+
}
40+
41+
struct VecFamily;
42+
43+
impl CollectionFamily for VecFamily {
44+
type Member<T> = Vec<T>;
45+
}
46+
47+
impl<T> Collection<T> for Vec<T> {
48+
type Iter<'iter> = std::slice::Iter<'iter, T>;
49+
type Family = VecFamily;
50+
51+
fn empty() -> Self {
52+
Vec::new()
53+
}
54+
55+
fn add(&mut self, value: T) {
56+
self.push(value)
57+
}
58+
59+
fn iterate<'iter>(&'iter self) -> Self::Iter<'iter> {
60+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
61+
self.iter()
62+
}
63+
}
64+
65+
fn floatify<C>(ints: &C) -> <<C as Collection<i32>>::Family as CollectionFamily>::Member<f32>
66+
//~^ ERROR type parameters are not allowed on this type [E0109]
67+
where
68+
C: Collection<i32>,
69+
{
70+
let mut res = C::Family::Member::<f32>::empty();
71+
for &v in ints.iterate() {
72+
res.add(v as f32);
73+
}
74+
res
75+
}
76+
77+
fn floatify_sibling<C>(ints: &C) -> <C as Collection<i32>>::Sibling<f32>
78+
//~^ ERROR type parameters are not allowed on this type [E0109]
79+
where
80+
C: Collection<i32>,
81+
{
82+
let mut res = C::Family::Member::<f32>::empty();
83+
for &v in ints.iterate() {
84+
res.add(v as f32);
85+
}
86+
res
87+
}
88+
89+
fn use_floatify() {
90+
let a = vec![1i32, 2, 3];
91+
let b = floatify(a);
92+
println!("{}", b.iterate().next());
93+
let c = floatify_sibling(a);
94+
println!("{}", c.iterate().next());
95+
}
96+
97+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error[E0109]: type parameters are not allowed on this type
2+
--> $DIR/collections.rs:65:90
3+
|
4+
LL | fn floatify<C>(ints: &C) -> <<C as Collection<i32>>::Family as CollectionFamily>::Member<f32>
5+
| ^^^ type parameter not allowed
6+
7+
error[E0109]: type parameters are not allowed on this type
8+
--> $DIR/collections.rs:77:69
9+
|
10+
LL | fn floatify_sibling<C>(ints: &C) -> <C as Collection<i32>>::Sibling<f32>
11+
| ^^^ type parameter not allowed
12+
13+
error[E0109]: type parameters are not allowed on this type
14+
--> $DIR/collections.rs:26:71
15+
|
16+
LL | <<Self as Collection<T>>::Family as CollectionFamily>::Member<U>;
17+
| ^ type parameter not allowed
18+
19+
error[E0110]: lifetime parameters are not allowed on this type
20+
--> $DIR/collections.rs:33:50
21+
|
22+
LL | fn iterate<'iter>(&'iter self) -> Self::Iter<'iter>;
23+
| ^^^^^ lifetime parameter not allowed on this type
24+
25+
error[E0110]: lifetime parameters are not allowed on this type
26+
--> $DIR/collections.rs:59:50
27+
|
28+
LL | fn iterate<'iter>(&'iter self) -> Self::Iter<'iter> {
29+
| ^^^^^ lifetime parameter not allowed on this type
30+
31+
error: aborting due to 5 previous errors
32+
33+
Some errors occurred: E0109, E0110.
34+
For more information about an error, try `rustc --explain E0109`.

src/test/ui/rfc1598-generic-associated-types/construct_with_other_type.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#![feature(generic_associated_types)]
1212

13+
use std::ops::Deref;
14+
1315
//FIXME(#44265): "lifetime parameters are not allowed on this type" errors will be addressed in a
1416
//follow-up PR
1517

@@ -18,11 +20,18 @@ trait Foo {
1820
}
1921

2022
trait Baz {
21-
type Quux<'a>;
23+
type Quux<'a>: Foo;
24+
25+
// This weird type tests that we can use universal function call syntax to access the Item on
26+
type Baa<'a>: Deref<Target = <Self::Quux<'a> as Foo>::Bar<'a, 'static>>;
27+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
28+
//~| ERROR lifetime parameters are not allowed on this type [E0110]
2229
}
2330

2431
impl<T> Baz for T where T: Foo {
25-
type Quux<'a> = <T as Foo>::Bar<'a, 'static>;
32+
type Quux<'a> = T;
33+
34+
type Baa<'a> = &'a <T as Foo>::Bar<'a, 'static>;
2635
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
2736
}
2837

Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
error[E0110]: lifetime parameters are not allowed on this type
2-
--> $DIR/construct_with_other_type.rs:25:37
2+
--> $DIR/construct_with_other_type.rs:26:46
33
|
4-
LL | type Quux<'a> = <T as Foo>::Bar<'a, 'static>;
5-
| ^^ lifetime parameter not allowed on this type
4+
LL | type Baa<'a>: Deref<Target = <Self::Quux<'a> as Foo>::Bar<'a, 'static>>;
5+
| ^^ lifetime parameter not allowed on this type
66

7-
error: aborting due to previous error
7+
error[E0110]: lifetime parameters are not allowed on this type
8+
--> $DIR/construct_with_other_type.rs:26:63
9+
|
10+
LL | type Baa<'a>: Deref<Target = <Self::Quux<'a> as Foo>::Bar<'a, 'static>>;
11+
| ^^ lifetime parameter not allowed on this type
12+
13+
error[E0110]: lifetime parameters are not allowed on this type
14+
--> $DIR/construct_with_other_type.rs:34:40
15+
|
16+
LL | type Baa<'a> = &'a <T as Foo>::Bar<'a, 'static>;
17+
| ^^ lifetime parameter not allowed on this type
18+
19+
error: aborting due to 3 previous errors
820

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

src/test/ui/rfc1598-generic-associated-types/iterable.rs

+31-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,40 @@ trait Iterable {
2020
type Iter<'a>: Iterator<Item = Self::Item<'a>>;
2121
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
2222

23-
// This weird type tests that we can use universal function call syntax to access the Item on
24-
// Self::Iter which we have declared to be an Iterator
25-
type Iter2<'a>: Deref<Target = <Self::Iter<'a> as Iterator>::Item>;
23+
fn iter<'a>(&'a self) -> Self::Iter<'a>;
2624
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
25+
}
2726

28-
fn iter<'a>(&'a self) -> Self::Iter<'a>;
27+
// Impl for struct type
28+
impl<T> Iterable for Vec<T> {
29+
type Item<'a> = &'a T;
30+
type Iter<'a> = std::slice::Iter<'a, T>;
31+
32+
fn iter<'a>(&'a self) -> Self::Iter<'a> {
33+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
34+
self.iter()
35+
}
36+
}
37+
38+
// Impl for a primitive type
39+
impl<T> Iterable for [T] {
40+
type Item<'a> = &'a T;
41+
type Iter<'a> = std::slice::Iter<'a, T>;
42+
43+
fn iter<'a>(&'a self) -> Self::Iter<'a> {
44+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
45+
self.iter()
46+
}
47+
}
48+
49+
fn make_iter<'a, I: Iterable>(it: &'a I) -> I::Iter<'a> {
50+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
51+
it.iter()
52+
}
53+
54+
fn get_first<'a, I: Iterable>(it: &'a I) -> Option<I::Item<'a>> {
2955
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
56+
it.iter().next()
3057
}
3158

3259
fn main() {}

src/test/ui/rfc1598-generic-associated-types/iterable.stderr

+23-5
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,35 @@ LL | type Iter<'a>: Iterator<Item = Self::Item<'a>>;
55
| ^^ lifetime parameter not allowed on this type
66

77
error[E0110]: lifetime parameters are not allowed on this type
8-
--> $DIR/iterable.rs:25:48
8+
--> $DIR/iterable.rs:49:53
99
|
10-
LL | type Iter2<'a>: Deref<Target = <Self::Iter<'a> as Iterator>::Item>;
11-
| ^^ lifetime parameter not allowed on this type
10+
LL | fn make_iter<'a, I: Iterable>(it: &'a I) -> I::Iter<'a> {
11+
| ^^ lifetime parameter not allowed on this type
1212

1313
error[E0110]: lifetime parameters are not allowed on this type
14-
--> $DIR/iterable.rs:28:41
14+
--> $DIR/iterable.rs:54:60
15+
|
16+
LL | fn get_first<'a, I: Iterable>(it: &'a I) -> Option<I::Item<'a>> {
17+
| ^^ lifetime parameter not allowed on this type
18+
19+
error[E0110]: lifetime parameters are not allowed on this type
20+
--> $DIR/iterable.rs:23:41
1521
|
1622
LL | fn iter<'a>(&'a self) -> Self::Iter<'a>;
1723
| ^^ lifetime parameter not allowed on this type
1824

19-
error: aborting due to 3 previous errors
25+
error[E0110]: lifetime parameters are not allowed on this type
26+
--> $DIR/iterable.rs:32:41
27+
|
28+
LL | fn iter<'a>(&'a self) -> Self::Iter<'a> {
29+
| ^^ lifetime parameter not allowed on this type
30+
31+
error[E0110]: lifetime parameters are not allowed on this type
32+
--> $DIR/iterable.rs:43:41
33+
|
34+
LL | fn iter<'a>(&'a self) -> Self::Iter<'a> {
35+
| ^^ lifetime parameter not allowed on this type
36+
37+
error: aborting due to 6 previous errors
2038

2139
For more information about this error, try `rustc --explain E0110`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(generic_associated_types)]
12+
#![feature(associated_type_defaults)]
13+
14+
//FIXME(#44265): "lifetime parameters are not allowed on this type" errors will be addressed in a
15+
//follow-up PR
16+
17+
//FIXME(#44265): Update expected errors once E110 is resolved, now does not get past `trait Foo`
18+
19+
trait Foo {
20+
type A<'a>;
21+
type B<'a, 'b>;
22+
type C;
23+
type D<T>;
24+
type E<'a, T>;
25+
// Test parameters in default values
26+
type FOk<T> = Self::E<'static, T>;
27+
//~^ ERROR type parameters are not allowed on this type [E0109]
28+
//~| ERROR lifetime parameters are not allowed on this type [E0110]
29+
type FErr1 = Self::E<'static, 'static>; // Error
30+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
31+
type FErr2<T> = Self::E<'static, T, u32>; // Error
32+
//~^ ERROR type parameters are not allowed on this type [E0109]
33+
//~| ERROR lifetime parameters are not allowed on this type [E0110]
34+
}
35+
36+
struct Fooy;
37+
38+
impl Foo for Fooy {
39+
type A = u32; // Error: parameter expected
40+
type B<'a, T> = Vec<T>; // Error: lifetime param expected
41+
type C<'a> = u32; // Error: no param expected
42+
type D<'a> = u32; // Error: type param expected
43+
type E<T, U> = u32; // Error: lifetime expected as the first param
44+
}
45+
46+
struct Fooer;
47+
48+
impl Foo for Fooer {
49+
type A<T> = u32; // Error: lifetime parameter expected
50+
type B<'a> = u32; // Error: another lifetime param expected
51+
type C<T> = T; // Error: no param expected
52+
type D<'b, T> = u32; // Error: unexpected lifetime param
53+
type E<'a, 'b> = u32; // Error: type expected as the second param
54+
}
55+
56+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error[E0109]: type parameters are not allowed on this type
2+
--> $DIR/parameter_number_and_kind.rs:26:36
3+
|
4+
LL | type FOk<T> = Self::E<'static, T>;
5+
| ^ type parameter not allowed
6+
7+
error[E0110]: lifetime parameters are not allowed on this type
8+
--> $DIR/parameter_number_and_kind.rs:26:27
9+
|
10+
LL | type FOk<T> = Self::E<'static, T>;
11+
| ^^^^^^^ lifetime parameter not allowed on this type
12+
13+
error[E0110]: lifetime parameters are not allowed on this type
14+
--> $DIR/parameter_number_and_kind.rs:29:26
15+
|
16+
LL | type FErr1 = Self::E<'static, 'static>; // Error
17+
| ^^^^^^^ lifetime parameter not allowed on this type
18+
19+
error[E0109]: type parameters are not allowed on this type
20+
--> $DIR/parameter_number_and_kind.rs:31:38
21+
|
22+
LL | type FErr2<T> = Self::E<'static, T, u32>; // Error
23+
| ^ type parameter not allowed
24+
25+
error[E0110]: lifetime parameters are not allowed on this type
26+
--> $DIR/parameter_number_and_kind.rs:31:29
27+
|
28+
LL | type FErr2<T> = Self::E<'static, T, u32>; // Error
29+
| ^^^^^^^ lifetime parameter not allowed on this type
30+
31+
error: aborting due to 5 previous errors
32+
33+
Some errors occurred: E0109, E0110.
34+
For more information about an error, try `rustc --explain E0109`.

0 commit comments

Comments
 (0)