Skip to content

Commit a8fcfce

Browse files
author
Alexander Regueiro
committed
Add more tests.
1 parent 1cda3c3 commit a8fcfce

File tree

7 files changed

+142
-49
lines changed

7 files changed

+142
-49
lines changed

src/test/run-pass/traits/trait-alias-bounds.rs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010

1111
#![feature(trait_alias)]
1212

13+
use std::marker::PhantomData;
14+
1315
trait SimpleAlias = Default;
14-
trait GenericAlias<T> = Iterator<Item=T>;
15-
trait Partial<T> = IntoIterator<Item=T>;
16+
trait GenericAlias<T> = Iterator<Item = T>;
17+
trait Partial<T> = IntoIterator<Item = T>;
18+
trait SpecificAlias = GenericAlias<i32>;
19+
trait PartialEqRef<'a, T> = PartialEq<&'a T>;
20+
trait StaticAlias = 'static;
1621

1722
trait Things<T> {}
1823
trait Romeo {}
@@ -26,19 +31,54 @@ impl<T> Romeo for Fore<T> {}
2631
trait WithWhere<Art, Thou> = Romeo + Romeo where Fore<(Art, Thou)>: Romeo;
2732
trait BareWhere<Wild, Are> = where The<Wild>: Things<Are>;
2833

29-
trait CD = Clone + Default;
34+
trait Empty {}
35+
trait EmptyAlias = Empty;
36+
trait CloneDefault = Clone + Default;
37+
trait SendSyncAlias = Send + Sync;
38+
trait WhereSendAlias = where Self: Send;
39+
trait SendEqAlias<T> = Send where T: PartialEq<Self>;
40+
trait I32Iterator = Iterator<Item = i32>;
41+
42+
#[allow(dead_code)]
43+
struct Foo<T: SendSyncAlias>(PhantomData<T>);
44+
#[allow(dead_code)]
45+
struct Bar<T>(PhantomData<T>) where T: SendSyncAlias;
46+
47+
impl EmptyAlias {}
48+
49+
impl<T: SendSyncAlias> Empty for T {}
3050

31-
fn foo<T: CD>() -> (T, T) {
51+
fn a<T: CloneDefault>() -> (T, T) {
3252
let one = T::default();
3353
let two = one.clone();
3454
(one, two)
3555
}
3656

57+
fn b(x: &impl SendEqAlias<i32>) -> bool {
58+
22_i32 == *x
59+
}
60+
61+
fn c<T: I32Iterator>(x: &mut T) -> Option<i32> {
62+
x.next()
63+
}
64+
65+
fn d<T: SendSyncAlias>() {
66+
is_send_and_sync::<T>();
67+
}
68+
69+
fn is_send_and_sync<T: Send + Sync>() {}
70+
3771
fn main() {
38-
let both = foo::<i32>();
72+
let both = a::<i32>();
3973
assert_eq!(both.0, 0);
4074
assert_eq!(both.1, 0);
41-
let both: (i32, i32) = foo();
75+
let both: (i32, i32) = a();
4276
assert_eq!(both.0, 0);
4377
assert_eq!(both.1, 0);
78+
79+
assert!(b(&22));
80+
81+
assert_eq!(c(&mut vec![22].into_iter()), Some(22));
82+
83+
d::<i32>();
4484
}

src/test/run-pass/traits/trait-alias-object-type.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,15 @@
1313
trait Foo = PartialEq<i32> + Send;
1414
trait Bar = Foo + Sync;
1515

16+
trait I32Iterator = Iterator<Item = i32>;
17+
1618
pub fn main() {
17-
let a: &Bar = &123;
19+
let a: &dyn Bar = &123;
1820
assert!(*a == 123);
1921
let b = Box::new(456) as Box<dyn Foo>;
2022
assert!(*b == 456);
23+
24+
// FIXME(alexreg): associated type should be gotten from trait alias definition
25+
// let c: &dyn I32Iterator = &vec![123].into_iter();
26+
// assert_eq!(c.next(), Some(123));
2127
}

src/test/ui/traits/trait-alias-fail.stderr

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/test/ui/traits/trait-alias-fail.rs renamed to src/test/ui/traits/trait-alias-fail1.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010

1111
// gate-test-trait_alias
1212

13-
trait Alias1<T> = Default where T: Clone;
14-
trait Alias2<T: Clone = ()> = Default;
13+
trait CloneDefault<T> = Default where T: Clone;
14+
trait BoundedAlias<T: Clone = ()> = Default;
1515

16-
impl Alias1 {}
16+
trait A<T: Send> {}
17+
trait B<T> = A<T>; // FIXME: parameter T should need a bound here, or semantics should be changed
1718

18-
impl Alias1 for () {}
19+
impl CloneDefault for () {}
1920

2021
fn main() {}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
error: type parameters on the left side of a trait alias cannot be bounded
2+
--> $DIR/trait-alias-fail1.rs:14:20
3+
|
4+
LL | trait BoundedAlias<T: Clone = ()> = Default;
5+
| ^
6+
7+
error: type parameters on the left side of a trait alias cannot have defaults
8+
--> $DIR/trait-alias-fail1.rs:14:20
9+
|
10+
LL | trait BoundedAlias<T: Clone = ()> = Default;
11+
| ^
12+
13+
error[E0404]: expected trait, found trait alias `CloneDefault`
14+
--> $DIR/trait-alias-fail1.rs:19:6
15+
|
16+
LL | impl CloneDefault for () {}
17+
| ^^^^^^^^^^^^ not a trait
18+
19+
error[E0658]: trait aliases are experimental (see issue #41517)
20+
--> $DIR/trait-alias-fail1.rs:13:1
21+
|
22+
LL | trait CloneDefault<T> = Default where T: Clone;
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
|
25+
= help: add #![feature(trait_alias)] to the crate attributes to enable
26+
27+
error[E0658]: trait aliases are experimental (see issue #41517)
28+
--> $DIR/trait-alias-fail1.rs:14:1
29+
|
30+
LL | trait BoundedAlias<T: Clone = ()> = Default;
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
|
33+
= help: add #![feature(trait_alias)] to the crate attributes to enable
34+
35+
error[E0658]: trait aliases are experimental (see issue #41517)
36+
--> $DIR/trait-alias-fail1.rs:17:1
37+
|
38+
LL | trait B<T> = A<T>; // FIXME: this should not work... or should it?
39+
| ^^^^^^^^^^^^^^^^^^
40+
|
41+
= help: add #![feature(trait_alias)] to the crate attributes to enable
42+
43+
error: aborting due to 6 previous errors
44+
45+
Some errors occurred: E0404, E0658.
46+
For more information about an error, try `rustc --explain E0404`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2018 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+
// gate-test-trait_alias
12+
13+
trait EqAlias = Eq;
14+
trait IteratorAlias = Iterator;
15+
16+
fn main() {
17+
let _: &dyn EqAlias = &123;
18+
let _: &dyn IteratorAlias = &vec![123].into_iter();
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0658]: trait aliases are experimental (see issue #41517)
2+
--> $DIR/trait-alias-fail2.rs:13:1
3+
|
4+
LL | trait EqAlias = Eq;
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: add #![feature(trait_alias)] to the crate attributes to enable
8+
9+
error[E0658]: trait aliases are experimental (see issue #41517)
10+
--> $DIR/trait-alias-fail2.rs:14:1
11+
|
12+
LL | trait IteratorAlias = Iterator;
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
|
15+
= help: add #![feature(trait_alias)] to the crate attributes to enable
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0658`.

0 commit comments

Comments
 (0)