Add more tests.

This commit is contained in:
Alexander Regueiro 2018-10-25 01:03:25 +01:00
parent 1cda3c3b49
commit a8fcfcef30
7 changed files with 147 additions and 54 deletions

View file

@ -10,9 +10,14 @@
#![feature(trait_alias)]
use std::marker::PhantomData;
trait SimpleAlias = Default;
trait GenericAlias<T> = Iterator<Item=T>;
trait Partial<T> = IntoIterator<Item=T>;
trait GenericAlias<T> = Iterator<Item = T>;
trait Partial<T> = IntoIterator<Item = T>;
trait SpecificAlias = GenericAlias<i32>;
trait PartialEqRef<'a, T> = PartialEq<&'a T>;
trait StaticAlias = 'static;
trait Things<T> {}
trait Romeo {}
@ -26,19 +31,54 @@ impl<T> Romeo for Fore<T> {}
trait WithWhere<Art, Thou> = Romeo + Romeo where Fore<(Art, Thou)>: Romeo;
trait BareWhere<Wild, Are> = where The<Wild>: Things<Are>;
trait CD = Clone + Default;
trait Empty {}
trait EmptyAlias = Empty;
trait CloneDefault = Clone + Default;
trait SendSyncAlias = Send + Sync;
trait WhereSendAlias = where Self: Send;
trait SendEqAlias<T> = Send where T: PartialEq<Self>;
trait I32Iterator = Iterator<Item = i32>;
fn foo<T: CD>() -> (T, T) {
#[allow(dead_code)]
struct Foo<T: SendSyncAlias>(PhantomData<T>);
#[allow(dead_code)]
struct Bar<T>(PhantomData<T>) where T: SendSyncAlias;
impl EmptyAlias {}
impl<T: SendSyncAlias> Empty for T {}
fn a<T: CloneDefault>() -> (T, T) {
let one = T::default();
let two = one.clone();
(one, two)
}
fn main() {
let both = foo::<i32>();
assert_eq!(both.0, 0);
assert_eq!(both.1, 0);
let both: (i32, i32) = foo();
assert_eq!(both.0, 0);
assert_eq!(both.1, 0);
fn b(x: &impl SendEqAlias<i32>) -> bool {
22_i32 == *x
}
fn c<T: I32Iterator>(x: &mut T) -> Option<i32> {
x.next()
}
fn d<T: SendSyncAlias>() {
is_send_and_sync::<T>();
}
fn is_send_and_sync<T: Send + Sync>() {}
fn main() {
let both = a::<i32>();
assert_eq!(both.0, 0);
assert_eq!(both.1, 0);
let both: (i32, i32) = a();
assert_eq!(both.0, 0);
assert_eq!(both.1, 0);
assert!(b(&22));
assert_eq!(c(&mut vec![22].into_iter()), Some(22));
d::<i32>();
}

View file

@ -13,9 +13,15 @@
trait Foo = PartialEq<i32> + Send;
trait Bar = Foo + Sync;
trait I32Iterator = Iterator<Item = i32>;
pub fn main() {
let a: &Bar = &123;
let a: &dyn Bar = &123;
assert!(*a == 123);
let b = Box::new(456) as Box<dyn Foo>;
assert!(*b == 456);
// FIXME(alexreg): associated type should be gotten from trait alias definition
// let c: &dyn I32Iterator = &vec![123].into_iter();
// assert_eq!(c.next(), Some(123));
}

View file

@ -1,38 +0,0 @@
error: type parameters on the left side of a trait alias cannot be bounded
--> $DIR/trait-alias-fail.rs:14:14
|
LL | trait Alias2<T: Clone = ()> = Default;
| ^
error: type parameters on the left side of a trait alias cannot have defaults
--> $DIR/trait-alias-fail.rs:14:14
|
LL | trait Alias2<T: Clone = ()> = Default;
| ^
error[E0404]: expected trait, found trait alias `Alias1`
--> $DIR/trait-alias-fail.rs:18:6
|
LL | impl Alias1 for () {}
| ^^^^^^ not a trait
error[E0658]: trait aliases are experimental (see issue #41517)
--> $DIR/trait-alias-fail.rs:13:1
|
LL | trait Alias1<T> = Default where T: Clone;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(trait_alias)] to the crate attributes to enable
error[E0658]: trait aliases are experimental (see issue #41517)
--> $DIR/trait-alias-fail.rs:14:1
|
LL | trait Alias2<T: Clone = ()> = Default;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(trait_alias)] to the crate attributes to enable
error: aborting due to 5 previous errors
Some errors occurred: E0404, E0658.
For more information about an error, try `rustc --explain E0404`.

View file

@ -10,11 +10,12 @@
// gate-test-trait_alias
trait Alias1<T> = Default where T: Clone;
trait Alias2<T: Clone = ()> = Default;
trait CloneDefault<T> = Default where T: Clone;
trait BoundedAlias<T: Clone = ()> = Default;
impl Alias1 {}
trait A<T: Send> {}
trait B<T> = A<T>; // FIXME: parameter T should need a bound here, or semantics should be changed
impl Alias1 for () {}
impl CloneDefault for () {}
fn main() {}

View file

@ -0,0 +1,46 @@
error: type parameters on the left side of a trait alias cannot be bounded
--> $DIR/trait-alias-fail1.rs:14:20
|
LL | trait BoundedAlias<T: Clone = ()> = Default;
| ^
error: type parameters on the left side of a trait alias cannot have defaults
--> $DIR/trait-alias-fail1.rs:14:20
|
LL | trait BoundedAlias<T: Clone = ()> = Default;
| ^
error[E0404]: expected trait, found trait alias `CloneDefault`
--> $DIR/trait-alias-fail1.rs:19:6
|
LL | impl CloneDefault for () {}
| ^^^^^^^^^^^^ not a trait
error[E0658]: trait aliases are experimental (see issue #41517)
--> $DIR/trait-alias-fail1.rs:13:1
|
LL | trait CloneDefault<T> = Default where T: Clone;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(trait_alias)] to the crate attributes to enable
error[E0658]: trait aliases are experimental (see issue #41517)
--> $DIR/trait-alias-fail1.rs:14:1
|
LL | trait BoundedAlias<T: Clone = ()> = Default;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(trait_alias)] to the crate attributes to enable
error[E0658]: trait aliases are experimental (see issue #41517)
--> $DIR/trait-alias-fail1.rs:17:1
|
LL | trait B<T> = A<T>; // FIXME: this should not work... or should it?
| ^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(trait_alias)] to the crate attributes to enable
error: aborting due to 6 previous errors
Some errors occurred: E0404, E0658.
For more information about an error, try `rustc --explain E0404`.

View file

@ -0,0 +1,19 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// gate-test-trait_alias
trait EqAlias = Eq;
trait IteratorAlias = Iterator;
fn main() {
let _: &dyn EqAlias = &123;
let _: &dyn IteratorAlias = &vec![123].into_iter();
}

View file

@ -0,0 +1,19 @@
error[E0658]: trait aliases are experimental (see issue #41517)
--> $DIR/trait-alias-fail2.rs:13:1
|
LL | trait EqAlias = Eq;
| ^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(trait_alias)] to the crate attributes to enable
error[E0658]: trait aliases are experimental (see issue #41517)
--> $DIR/trait-alias-fail2.rs:14:1
|
LL | trait IteratorAlias = Iterator;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: add #![feature(trait_alias)] to the crate attributes to enable
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0658`.