Skip to content

Commit b0fcb5f

Browse files
committed
Extend tests for RFC1598 (GAT)
1 parent 3eadd75 commit b0fcb5f

File tree

8 files changed

+279
-2
lines changed

8 files changed

+279
-2
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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.
18+
// Based on http://smallcultfollowing.com/babysteps/blog/2016/11/03/associated-type-constructors-part-2-family-traits/
19+
20+
trait Collection<T> {
21+
fn empty() -> Self;
22+
fn add(&mut self, value: T);
23+
fn iterate<'iter>(&'iter self) -> Self::Iter<'iter>;
24+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
25+
type Iter<'iter>: Iterator<Item=&'iter T>;
26+
type Family: CollectionFamily;
27+
// Test associated type defaults with parameters
28+
type Sibling<U>: Collection<U> = <<Self as Collection<T>>::Family as CollectionFamily>::Member<U>;
29+
//~^ ERROR type parameters are not allowed on this type [E0109]
30+
}
31+
32+
trait CollectionFamily {
33+
type Member<T>: Collection<T, Family = Self>;
34+
}
35+
36+
struct VecFamily;
37+
38+
impl CollectionFamily for VecFamily {
39+
type Member<T> = Vec<T>;
40+
}
41+
42+
impl<T> Collection<T> for Vec<T> {
43+
fn empty() -> Self {
44+
Vec::new()
45+
}
46+
fn add(&mut self, value: T) {
47+
self.push(value)
48+
}
49+
fn iterate<'iter>(&'iter self) -> Self::Iter<'iter> {
50+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
51+
self.iter()
52+
}
53+
type Iter<'iter> = std::slice::Iter<'iter, T>;
54+
type Family = VecFamily;
55+
}
56+
57+
fn floatify<C>(ints: &C) -> <<C as Collection<i32>>::Family as CollectionFamily>::Member<f32>
58+
//~^ ERROR type parameters are not allowed on this type [E0109]
59+
where C: Collection<i32> {
60+
let mut res = C::Family::Member::<f32>::empty();
61+
for &v in ints.iterate() {
62+
res.add(v as f32);
63+
}
64+
res
65+
}
66+
67+
fn floatify_sibling<C>(ints: &C) -> <C as Collection<i32>>::Sibling<f32>
68+
//~^ ERROR type parameters are not allowed on this type [E0109]
69+
where C: Collection<i32> {
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 use_floatify() {
78+
let a = vec![1i32, 2, 3];
79+
let b = floatify(a);
80+
println!("{}", b.iterate().next());
81+
let c = floatify_sibling(a);
82+
println!("{}", c.iterate().next());
83+
}
84+
85+
fn main() {}
Lines changed: 34 additions & 0 deletions
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:57: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:67:69
9+
|
10+
LL | fn floatify_sibling<C>(ints: &C) -> <C as Collection<i32>>::Sibling<f32>
11+
| ^^^ type parameter not allowed
12+
13+
error[E0110]: lifetime parameters are not allowed on this type
14+
--> $DIR/collections.rs:23:50
15+
|
16+
LL | fn iterate<'iter>(&'iter self) -> Self::Iter<'iter>;
17+
| ^^^^^ lifetime parameter not allowed on this type
18+
19+
error[E0109]: type parameters are not allowed on this type
20+
--> $DIR/collections.rs:28:100
21+
|
22+
LL | type Sibling<U>: Collection<U> = <<Self as Collection<T>>::Family as CollectionFamily>::Member<U>;
23+
| ^ type parameter not allowed
24+
25+
error[E0110]: lifetime parameters are not allowed on this type
26+
--> $DIR/collections.rs:49: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/iterable.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,38 @@ trait Iterable {
2929
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
3030
}
3131

32+
// Impl for struct type
33+
impl<T> Iterable for Vec<T> {
34+
type Item<'a> = &'a T;
35+
type Iter<'a> = std::slice::Iter<'a, T>;
36+
type Iter2<'a> = &'a T;
37+
// gavento: ^^^ Not 100% sure about the intention here
38+
fn iter<'a>(&'a self) -> Self::Iter<'a> {
39+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
40+
self.iter()
41+
}
42+
}
43+
44+
// Impl for a primitive type
45+
impl<T> Iterable for [T] {
46+
type Item<'a> = &'a T;
47+
type Iter<'a> = std::slice::Iter<'a, T>;
48+
type Iter2<'a> = &'a T;
49+
// gavento: ^^^ Not 100% sure about the intention here
50+
fn iter<'a>(&'a self) -> Self::Iter<'a> {
51+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
52+
self.iter()
53+
}
54+
}
55+
56+
fn make_iter<'a, I: Iterable>(it: &'a I) -> I::Iter<'a> {
57+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
58+
it.iter()
59+
}
60+
61+
fn get_first<'a, I: Iterable>(it: &'a I) -> Option<I::Item<'a>> {
62+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
63+
it.iter().next()
64+
}
65+
3266
fn main() {}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,36 @@ error[E0110]: lifetime parameters are not allowed on this type
1010
LL | type Iter2<'a>: Deref<Target = <Self::Iter<'a> as Iterator>::Item>;
1111
| ^^ lifetime parameter not allowed on this type
1212

13+
error[E0110]: lifetime parameters are not allowed on this type
14+
--> $DIR/iterable.rs:56:53
15+
|
16+
LL | fn make_iter<'a, I: Iterable>(it: &'a I) -> I::Iter<'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:61:60
21+
|
22+
LL | fn get_first<'a, I: Iterable>(it: &'a I) -> Option<I::Item<'a>> {
23+
| ^^ lifetime parameter not allowed on this type
24+
1325
error[E0110]: lifetime parameters are not allowed on this type
1426
--> $DIR/iterable.rs:28:41
1527
|
1628
LL | fn iter<'a>(&'a self) -> Self::Iter<'a>;
1729
| ^^ lifetime parameter not allowed on this type
1830

19-
error: aborting due to 3 previous errors
31+
error[E0110]: lifetime parameters are not allowed on this type
32+
--> $DIR/iterable.rs:38:41
33+
|
34+
LL | fn iter<'a>(&'a self) -> Self::Iter<'a> {
35+
| ^^ lifetime parameter not allowed on this type
36+
37+
error[E0110]: lifetime parameters are not allowed on this type
38+
--> $DIR/iterable.rs:50:41
39+
|
40+
LL | fn iter<'a>(&'a self) -> Self::Iter<'a> {
41+
| ^^ lifetime parameter not allowed on this type
42+
43+
error: aborting due to 7 previous errors
2044

2145
For more information about this error, try `rustc --explain E0110`.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
13+
//FIXME(#44265): The lifetime shadowing and type parameter shadowing
14+
// should cause an error. This will be addressed by a future PR.
15+
// For now this compiles:
16+
// must-compile-successfully
17+
18+
trait Shadow<'a> {
19+
type Bar<'a>; // Error: shadowed lifetime
20+
}
21+
22+
trait NoShadow<'a> {
23+
type Bar<'b>; // OK
24+
}
25+
26+
impl<'a> NoShadow<'a> for &'a u32
27+
{
28+
type Bar<'a> = i32; // Error: shadowed lifetime
29+
}
30+
31+
trait ShadowT<T> {
32+
type Bar<T>; // Error: shadowed type parameter
33+
}
34+
35+
trait NoShadowT<T> {
36+
type Bar<U>; // OK
37+
}
38+
39+
impl<T> NoShadowT<T> for Option<T>
40+
{
41+
type Bar<T> = i32; // Error: shadowed type parameter
42+
}
43+
44+
fn main() {}

src/test/ui/rfc1598-generic-associated-types/shadowing.stdout

Whitespace-only changes.

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,48 @@ struct Foo<T: StreamingIterator> {
3535
fn foo<T>(iter: T) where T: StreamingIterator, for<'a> T::Item<'a>: Display { /* ... */ }
3636
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
3737

38+
// Full example of enumerate iterator
39+
40+
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
41+
struct StreamEnumerate<I> {
42+
iter: I,
43+
count: usize,
44+
}
45+
46+
impl<I: StreamingIterator> StreamingIterator for StreamEnumerate<I> {
47+
type Item<'a> = (usize, I::Item<'a>);
48+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
49+
fn next<'a>(&'a self) -> Option<Self::Item<'a>> {
50+
//~^ ERROR lifetime parameters are not allowed on this type [E0110]
51+
match self.iter.next() {
52+
None => None,
53+
Some(val) => {
54+
let r = Some((self.count, val));
55+
self.count += 1;
56+
r
57+
}
58+
}
59+
}
60+
}
61+
62+
impl<I> StreamEnumerate<I> {
63+
pub fn new(iter: I) -> Self {
64+
StreamEnumerate {
65+
count: 0,
66+
iter: iter,
67+
}
68+
}
69+
}
70+
71+
fn test_stream_enumerate() {
72+
let v = vec!["a", "b", "c"];
73+
let se = StreamEnumerate::new(v.iter());
74+
let a: &str = se.next().unwrap().1;
75+
for (i, s) in se {
76+
println!("{} {}", i, s);
77+
}
78+
println!("{}", a);
79+
}
80+
81+
3882
fn main() {}

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ error[E0110]: lifetime parameters are not allowed on this type
1616
LL | fn next<'a>(&'a self) -> Option<Self::Item<'a>>;
1717
| ^^ lifetime parameter not allowed on this type
1818

19-
error: aborting due to 3 previous errors
19+
error[E0110]: lifetime parameters are not allowed on this type
20+
--> $DIR/streaming_iterator.rs:47:37
21+
|
22+
LL | type Item<'a> = (usize, I::Item<'a>);
23+
| ^^ lifetime parameter not allowed on this type
24+
25+
error[E0110]: lifetime parameters are not allowed on this type
26+
--> $DIR/streaming_iterator.rs:49:48
27+
|
28+
LL | fn next<'a>(&'a self) -> Option<Self::Item<'a>> {
29+
| ^^ lifetime parameter not allowed on this type
30+
31+
error: aborting due to 5 previous errors
2032

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

0 commit comments

Comments
 (0)