diff options
Diffstat (limited to 'tests/ui/traits/default-method')
22 files changed, 630 insertions, 0 deletions
diff --git a/tests/ui/traits/default-method/auxiliary/xc.rs b/tests/ui/traits/default-method/auxiliary/xc.rs new file mode 100644 index 000000000..0fb26af80 --- /dev/null +++ b/tests/ui/traits/default-method/auxiliary/xc.rs @@ -0,0 +1,40 @@ +pub struct Something { pub x: isize } + +pub trait A { + fn f(&self) -> isize; + fn g(&self) -> isize { 10 } + fn h(&self) -> isize { 11 } + fn lurr(x: &Self, y: &Self) -> isize { x.g() + y.h() } +} + + +impl A for isize { + fn f(&self) -> isize { 10 } +} + +impl A for Something { + fn f(&self) -> isize { 10 } +} + +pub trait B<T> { + fn thing<U>(&self, x: T, y: U) -> (T, U) { (x, y) } + fn staticthing<U>(_z: &Self, x: T, y: U) -> (T, U) { (x, y) } +} + +impl<T> B<T> for isize { } +impl B<f64> for bool { } + + + +pub trait TestEquality { + fn test_eq(&self, rhs: &Self) -> bool; + fn test_neq(&self, rhs: &Self) -> bool { + !self.test_eq(rhs) + } +} + +impl TestEquality for isize { + fn test_eq(&self, rhs: &isize) -> bool { + *self == *rhs + } +} diff --git a/tests/ui/traits/default-method/auxiliary/xc_2.rs b/tests/ui/traits/default-method/auxiliary/xc_2.rs new file mode 100644 index 000000000..979233820 --- /dev/null +++ b/tests/ui/traits/default-method/auxiliary/xc_2.rs @@ -0,0 +1,17 @@ +// aux-build:xc.rs + +extern crate xc as aux; +use aux::A; + +pub struct a_struct { pub x: isize } + +impl A for a_struct { + fn f(&self) -> isize { 10 } +} + +// This function will need to get inlined, and badness may result. +pub fn welp<A>(x: A) -> A { + let a = a_struct { x: 0 }; + a.g(); + x +} diff --git a/tests/ui/traits/default-method/bound-subst.rs b/tests/ui/traits/default-method/bound-subst.rs new file mode 100644 index 000000000..6a5d5c8ba --- /dev/null +++ b/tests/ui/traits/default-method/bound-subst.rs @@ -0,0 +1,18 @@ +// run-pass + + +trait A<T> { + fn g<U>(&self, x: T, y: U) -> (T, U) { (x, y) } +} + +impl A<i32> for i32 { } +impl<T> A<T> for u32 { } + +fn f<T, U, V: A<T>>(i: V, j: T, k: U) -> (T, U) { + i.g(j, k) +} + +pub fn main () { + assert_eq!(f(0, 1, 2), (1, 2)); + assert_eq!(f(0, 1, 2), (1, 2)); +} diff --git a/tests/ui/traits/default-method/bound-subst2.rs b/tests/ui/traits/default-method/bound-subst2.rs new file mode 100644 index 000000000..78eabba2d --- /dev/null +++ b/tests/ui/traits/default-method/bound-subst2.rs @@ -0,0 +1,16 @@ +// run-pass + + +trait A<T> { + fn g(&self, x: T) -> T { x } +} + +impl A<isize> for isize { } + +fn f<T, V: A<T>>(i: V, j: T) -> T { + i.g(j) +} + +pub fn main () { + assert_eq!(f(0, 2), 2); +} diff --git a/tests/ui/traits/default-method/bound-subst3.rs b/tests/ui/traits/default-method/bound-subst3.rs new file mode 100644 index 000000000..dd39dec4b --- /dev/null +++ b/tests/ui/traits/default-method/bound-subst3.rs @@ -0,0 +1,17 @@ +// run-pass + + +trait A { + fn g<T>(&self, x: T, y: T) -> (T, T) { (x, y) } +} + +impl A for isize { } + +fn f<T, V: A>(i: V, j: T, k: T) -> (T, T) { + i.g(j, k) +} + +pub fn main () { + assert_eq!(f(0, 1, 2), (1, 2)); + assert_eq!(f(0, 1u8, 2u8), (1u8, 2u8)); +} diff --git a/tests/ui/traits/default-method/bound-subst4.rs b/tests/ui/traits/default-method/bound-subst4.rs new file mode 100644 index 000000000..ef1330645 --- /dev/null +++ b/tests/ui/traits/default-method/bound-subst4.rs @@ -0,0 +1,19 @@ +// run-pass +#![allow(unused_variables)] + + +trait A<T> { + fn g(&self, x: usize) -> usize { x } + fn h(&self, x: T) { } +} + +impl<T> A<T> for isize { } + +fn f<T, V: A<T>>(i: V, j: usize) -> usize { + i.g(j) +} + +pub fn main () { + assert_eq!(f::<f64, isize>(0, 2), 2); + assert_eq!(f::<usize, isize>(0, 2), 2); +} diff --git a/tests/ui/traits/default-method/bound.rs b/tests/ui/traits/default-method/bound.rs new file mode 100644 index 000000000..0855a9db8 --- /dev/null +++ b/tests/ui/traits/default-method/bound.rs @@ -0,0 +1,16 @@ +// run-pass + + +trait A { + fn g(&self) -> isize { 10 } +} + +impl A for isize { } + +fn f<T:A>(i: T) { + assert_eq!(i.g(), 10); +} + +pub fn main () { + f(0); +} diff --git a/tests/ui/traits/default-method/macro.rs b/tests/ui/traits/default-method/macro.rs new file mode 100644 index 000000000..2b50ee9b4 --- /dev/null +++ b/tests/ui/traits/default-method/macro.rs @@ -0,0 +1,20 @@ +// run-pass + + +trait Foo { + fn bar(&self) -> String { + format!("test") + } +} + +enum Baz { + Quux +} + +impl Foo for Baz { +} + +pub fn main() { + let q = Baz::Quux; + assert_eq!(q.bar(), "test".to_string()); +} diff --git a/tests/ui/traits/default-method/mut.rs b/tests/ui/traits/default-method/mut.rs new file mode 100644 index 000000000..5f8e983b0 --- /dev/null +++ b/tests/ui/traits/default-method/mut.rs @@ -0,0 +1,11 @@ +// run-pass +#![allow(unused_assignments)] +// pretty-expanded FIXME #23616 + +#![allow(unused_variables)] + +trait Foo { + fn foo(&self, mut v: isize) { v = 1; } +} + +pub fn main() {} diff --git a/tests/ui/traits/default-method/rustc_must_implement_one_of.rs b/tests/ui/traits/default-method/rustc_must_implement_one_of.rs new file mode 100644 index 000000000..5ba2f5ce3 --- /dev/null +++ b/tests/ui/traits/default-method/rustc_must_implement_one_of.rs @@ -0,0 +1,44 @@ +#![feature(rustc_attrs)] + +#[rustc_must_implement_one_of(eq, neq)] +trait Equal { + fn eq(&self, other: &Self) -> bool { + !self.neq(other) + } + + fn neq(&self, other: &Self) -> bool { + !self.eq(other) + } +} + +struct T0; +struct T1; +struct T2; +struct T3; + +impl Equal for T0 { + fn eq(&self, _other: &Self) -> bool { + true + } +} + +impl Equal for T1 { + fn neq(&self, _other: &Self) -> bool { + false + } +} + +impl Equal for T2 { + fn eq(&self, _other: &Self) -> bool { + true + } + + fn neq(&self, _other: &Self) -> bool { + false + } +} + +impl Equal for T3 {} +//~^ not all trait items implemented, missing one of: `eq`, `neq` + +fn main() {} diff --git a/tests/ui/traits/default-method/rustc_must_implement_one_of.stderr b/tests/ui/traits/default-method/rustc_must_implement_one_of.stderr new file mode 100644 index 000000000..5a4dd1388 --- /dev/null +++ b/tests/ui/traits/default-method/rustc_must_implement_one_of.stderr @@ -0,0 +1,15 @@ +error[E0046]: not all trait items implemented, missing one of: `eq`, `neq` + --> $DIR/rustc_must_implement_one_of.rs:41:1 + | +LL | impl Equal for T3 {} + | ^^^^^^^^^^^^^^^^^ missing one of `eq`, `neq` in implementation + | +note: required because of this annotation + --> $DIR/rustc_must_implement_one_of.rs:3:1 + | +LL | #[rustc_must_implement_one_of(eq, neq)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0046`. diff --git a/tests/ui/traits/default-method/rustc_must_implement_one_of_duplicates.rs b/tests/ui/traits/default-method/rustc_must_implement_one_of_duplicates.rs new file mode 100644 index 000000000..8db5fa615 --- /dev/null +++ b/tests/ui/traits/default-method/rustc_must_implement_one_of_duplicates.rs @@ -0,0 +1,19 @@ +#![feature(rustc_attrs)] + +#[rustc_must_implement_one_of(a, a)] +//~^ functions names are duplicated +trait Trait { + fn a() {} +} + +#[rustc_must_implement_one_of(b, a, a, c, b, c)] +//~^ functions names are duplicated +//~| functions names are duplicated +//~| functions names are duplicated +trait Trait1 { + fn a() {} + fn b() {} + fn c() {} +} + +fn main() {} diff --git a/tests/ui/traits/default-method/rustc_must_implement_one_of_duplicates.stderr b/tests/ui/traits/default-method/rustc_must_implement_one_of_duplicates.stderr new file mode 100644 index 000000000..cd1476a6e --- /dev/null +++ b/tests/ui/traits/default-method/rustc_must_implement_one_of_duplicates.stderr @@ -0,0 +1,34 @@ +error: functions names are duplicated + --> $DIR/rustc_must_implement_one_of_duplicates.rs:3:31 + | +LL | #[rustc_must_implement_one_of(a, a)] + | ^ ^ + | + = note: all `#[rustc_must_implement_one_of]` arguments must be unique + +error: functions names are duplicated + --> $DIR/rustc_must_implement_one_of_duplicates.rs:9:34 + | +LL | #[rustc_must_implement_one_of(b, a, a, c, b, c)] + | ^ ^ + | + = note: all `#[rustc_must_implement_one_of]` arguments must be unique + +error: functions names are duplicated + --> $DIR/rustc_must_implement_one_of_duplicates.rs:9:31 + | +LL | #[rustc_must_implement_one_of(b, a, a, c, b, c)] + | ^ ^ + | + = note: all `#[rustc_must_implement_one_of]` arguments must be unique + +error: functions names are duplicated + --> $DIR/rustc_must_implement_one_of_duplicates.rs:9:40 + | +LL | #[rustc_must_implement_one_of(b, a, a, c, b, c)] + | ^ ^ + | + = note: all `#[rustc_must_implement_one_of]` arguments must be unique + +error: aborting due to 4 previous errors + diff --git a/tests/ui/traits/default-method/rustc_must_implement_one_of_gated.rs b/tests/ui/traits/default-method/rustc_must_implement_one_of_gated.rs new file mode 100644 index 000000000..ec2995872 --- /dev/null +++ b/tests/ui/traits/default-method/rustc_must_implement_one_of_gated.rs @@ -0,0 +1,13 @@ +#[rustc_must_implement_one_of(eq, neq)] +//~^ the `#[rustc_must_implement_one_of]` attribute is used to change minimal complete definition of a trait, it's currently in experimental form and should be changed before being exposed outside of the std +trait Equal { + fn eq(&self, other: &Self) -> bool { + !self.neq(other) + } + + fn neq(&self, other: &Self) -> bool { + !self.eq(other) + } +} + +fn main() {} diff --git a/tests/ui/traits/default-method/rustc_must_implement_one_of_gated.stderr b/tests/ui/traits/default-method/rustc_must_implement_one_of_gated.stderr new file mode 100644 index 000000000..228bc3e35 --- /dev/null +++ b/tests/ui/traits/default-method/rustc_must_implement_one_of_gated.stderr @@ -0,0 +1,11 @@ +error[E0658]: the `#[rustc_must_implement_one_of]` attribute is used to change minimal complete definition of a trait, it's currently in experimental form and should be changed before being exposed outside of the std + --> $DIR/rustc_must_implement_one_of_gated.rs:1:1 + | +LL | #[rustc_must_implement_one_of(eq, neq)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: add `#![feature(rustc_attrs)]` to the crate attributes to enable + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0658`. diff --git a/tests/ui/traits/default-method/rustc_must_implement_one_of_misuse.rs b/tests/ui/traits/default-method/rustc_must_implement_one_of_misuse.rs new file mode 100644 index 000000000..b1b91966c --- /dev/null +++ b/tests/ui/traits/default-method/rustc_must_implement_one_of_misuse.rs @@ -0,0 +1,46 @@ +#![feature(rustc_attrs)] + +#[rustc_must_implement_one_of(a, b)] +//~^ function not found in this trait +//~| function not found in this trait +trait Tr0 {} + +#[rustc_must_implement_one_of(a, b)] +//~^ function not found in this trait +trait Tr1 { + fn a() {} +} + +#[rustc_must_implement_one_of(a)] +//~^ the `#[rustc_must_implement_one_of]` attribute must be used with at least 2 args +trait Tr2 { + fn a() {} +} + +#[rustc_must_implement_one_of] +//~^ malformed `rustc_must_implement_one_of` attribute input +trait Tr3 {} + +#[rustc_must_implement_one_of(A, B)] +trait Tr4 { + const A: u8 = 1; //~ not a function + + type B; //~ not a function +} + +#[rustc_must_implement_one_of(a, b)] +trait Tr5 { + fn a(); //~ function doesn't have a default implementation + + fn b(); //~ function doesn't have a default implementation +} + +#[rustc_must_implement_one_of(abc, xyz)] +//~^ attribute should be applied to a trait +fn function() {} + +#[rustc_must_implement_one_of(abc, xyz)] +//~^ attribute should be applied to a trait +struct Struct {} + +fn main() {} diff --git a/tests/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr b/tests/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr new file mode 100644 index 000000000..38e692521 --- /dev/null +++ b/tests/ui/traits/default-method/rustc_must_implement_one_of_misuse.stderr @@ -0,0 +1,100 @@ +error: malformed `rustc_must_implement_one_of` attribute input + --> $DIR/rustc_must_implement_one_of_misuse.rs:20:1 + | +LL | #[rustc_must_implement_one_of] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: must be of the form: `#[rustc_must_implement_one_of(function1, function2, ...)]` + +error: attribute should be applied to a trait + --> $DIR/rustc_must_implement_one_of_misuse.rs:38:1 + | +LL | #[rustc_must_implement_one_of(abc, xyz)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | +LL | fn function() {} + | ---------------- not a trait + +error: attribute should be applied to a trait + --> $DIR/rustc_must_implement_one_of_misuse.rs:42:1 + | +LL | #[rustc_must_implement_one_of(abc, xyz)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | +LL | struct Struct {} + | ---------------- not a trait + +error: function not found in this trait + --> $DIR/rustc_must_implement_one_of_misuse.rs:3:31 + | +LL | #[rustc_must_implement_one_of(a, b)] + | ^ + +error: function not found in this trait + --> $DIR/rustc_must_implement_one_of_misuse.rs:3:34 + | +LL | #[rustc_must_implement_one_of(a, b)] + | ^ + +error: function not found in this trait + --> $DIR/rustc_must_implement_one_of_misuse.rs:8:34 + | +LL | #[rustc_must_implement_one_of(a, b)] + | ^ + +error: the `#[rustc_must_implement_one_of]` attribute must be used with at least 2 args + --> $DIR/rustc_must_implement_one_of_misuse.rs:14:1 + | +LL | #[rustc_must_implement_one_of(a)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: not a function + --> $DIR/rustc_must_implement_one_of_misuse.rs:26:5 + | +LL | const A: u8 = 1; + | ^^^^^^^^^^^^^^^^ + | +note: required by this annotation + --> $DIR/rustc_must_implement_one_of_misuse.rs:24:1 + | +LL | #[rustc_must_implement_one_of(A, B)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: all `#[rustc_must_implement_one_of]` arguments must be associated function names + +error: not a function + --> $DIR/rustc_must_implement_one_of_misuse.rs:28:5 + | +LL | type B; + | ^^^^^^^ + | +note: required by this annotation + --> $DIR/rustc_must_implement_one_of_misuse.rs:24:1 + | +LL | #[rustc_must_implement_one_of(A, B)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + = note: all `#[rustc_must_implement_one_of]` arguments must be associated function names + +error: function doesn't have a default implementation + --> $DIR/rustc_must_implement_one_of_misuse.rs:33:5 + | +LL | fn a(); + | ^^^^^^^ + | +note: required by this annotation + --> $DIR/rustc_must_implement_one_of_misuse.rs:31:1 + | +LL | #[rustc_must_implement_one_of(a, b)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: function doesn't have a default implementation + --> $DIR/rustc_must_implement_one_of_misuse.rs:35:5 + | +LL | fn b(); + | ^^^^^^^ + | +note: required by this annotation + --> $DIR/rustc_must_implement_one_of_misuse.rs:31:1 + | +LL | #[rustc_must_implement_one_of(a, b)] + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error: aborting due to 11 previous errors + diff --git a/tests/ui/traits/default-method/self.rs b/tests/ui/traits/default-method/self.rs new file mode 100644 index 000000000..cdf4d1e14 --- /dev/null +++ b/tests/ui/traits/default-method/self.rs @@ -0,0 +1,18 @@ +// run-pass + + +trait Cat { + fn meow(&self) -> bool; + fn scratch(&self) -> bool { self.purr() } + fn purr(&self) -> bool { true } +} + +impl Cat for isize { + fn meow(&self) -> bool { + self.scratch() + } +} + +pub fn main() { + assert!(5.meow()); +} diff --git a/tests/ui/traits/default-method/supervtable.rs b/tests/ui/traits/default-method/supervtable.rs new file mode 100644 index 000000000..939ad5135 --- /dev/null +++ b/tests/ui/traits/default-method/supervtable.rs @@ -0,0 +1,28 @@ +// run-pass + + +// Tests that we can call a function bounded over a supertrait from +// a default method + +fn require_y<T: Y>(x: T) -> isize { x.y() } + +trait Y { + fn y(self) -> isize; +} + + +trait Z: Y + Sized { + fn x(self) -> isize { + require_y(self) + } +} + +impl Y for isize { + fn y(self) -> isize { self } +} + +impl Z for isize {} + +pub fn main() { + assert_eq!(12.x(), 12); +} diff --git a/tests/ui/traits/default-method/trivial.rs b/tests/ui/traits/default-method/trivial.rs new file mode 100644 index 000000000..dc41938ec --- /dev/null +++ b/tests/ui/traits/default-method/trivial.rs @@ -0,0 +1,21 @@ +// run-pass + + +trait Cat { + fn meow(&self) -> bool; + fn scratch(&self) -> bool; + fn purr(&self) -> bool { true } +} + +impl Cat for isize { + fn meow(&self) -> bool { + self.scratch() + } + fn scratch(&self) -> bool { + self.purr() + } +} + +pub fn main() { + assert!(5.meow()); +} diff --git a/tests/ui/traits/default-method/xc-2.rs b/tests/ui/traits/default-method/xc-2.rs new file mode 100644 index 000000000..1de61dcf8 --- /dev/null +++ b/tests/ui/traits/default-method/xc-2.rs @@ -0,0 +1,26 @@ +// run-pass +// aux-build:xc.rs +// aux-build:xc_2.rs + + + +extern crate xc as aux; +extern crate xc_2 as aux2; +use aux::A; +use aux2::{a_struct, welp}; + + +pub fn main () { + + let a = a_struct { x: 0 }; + let b = a_struct { x: 1 }; + + assert_eq!(0.g(), 10); + assert_eq!(a.g(), 10); + assert_eq!(a.h(), 11); + assert_eq!(b.g(), 10); + assert_eq!(b.h(), 11); + assert_eq!(A::lurr(&a, &b), 21); + + welp(&0); +} diff --git a/tests/ui/traits/default-method/xc.rs b/tests/ui/traits/default-method/xc.rs new file mode 100644 index 000000000..76a1573d6 --- /dev/null +++ b/tests/ui/traits/default-method/xc.rs @@ -0,0 +1,81 @@ +// run-pass +#![allow(dead_code)] +#![allow(non_camel_case_types)] + +// aux-build:xc.rs + + +extern crate xc as aux; +use aux::{A, TestEquality, Something}; +use aux::B; + +fn f<T: aux::A>(i: T) { + assert_eq!(i.g(), 10); +} + +fn welp<T>(i: isize, _x: &T) -> isize { + i.g() +} + +mod stuff { + pub struct thing { pub x: isize } +} + +impl A for stuff::thing { + fn f(&self) -> isize { 10 } +} + +fn g<T, U, V: B<T>>(i: V, j: T, k: U) -> (T, U) { + i.thing(j, k) +} + +fn eq<T: TestEquality>(lhs: &T, rhs: &T) -> bool { + lhs.test_eq(rhs) +} +fn neq<T: TestEquality>(lhs: &T, rhs: &T) -> bool { + lhs.test_neq(rhs) +} + + +impl TestEquality for stuff::thing { + fn test_eq(&self, rhs: &stuff::thing) -> bool { + //self.x.test_eq(&rhs.x) + eq(&self.x, &rhs.x) + } +} + + +pub fn main() { + // Some tests of random things + f(0); + + assert_eq!(A::lurr(&0, &1), 21); + + let a = stuff::thing { x: 0 }; + let b = stuff::thing { x: 1 }; + let c = Something { x: 1 }; + + assert_eq!(0.g(), 10); + assert_eq!(a.g(), 10); + assert_eq!(a.h(), 11); + assert_eq!(c.h(), 11); + + assert_eq!(0.thing(3.14f64, 1), (3.14f64, 1)); + assert_eq!(B::staticthing(&0, 3.14f64, 1), (3.14f64, 1)); + assert_eq!(B::<f64>::staticthing::<isize>(&0, 3.14, 1), (3.14, 1)); + + assert_eq!(g(0, 3.14f64, 1), (3.14f64, 1)); + assert_eq!(g(false, 3.14f64, 1), (3.14, 1)); + + + // Trying out a real one + assert!(12.test_neq(&10)); + assert!(!10.test_neq(&10)); + assert!(a.test_neq(&b)); + assert!(!a.test_neq(&a)); + + assert!(neq(&12, &10)); + assert!(!neq(&10, &10)); + assert!(neq(&a, &b)); + assert!(!neq(&a, &a)); +} |