]> git.lizzy.rs Git - rust.git/blob - src/test/ui/specialization/specialization-default-methods.rs
9ae3d1e9f3931cb58b3dd1f0e595a1a306903b4c
[rust.git] / src / test / ui / specialization / specialization-default-methods.rs
1 // run-pass
2
3 #![feature(specialization)]
4
5 // Test that default methods are cascaded correctly
6
7 // First, test only use of explicit `default` items:
8
9 trait Foo {
10     fn foo(&self) -> bool;
11 }
12
13 // Specialization tree for Foo:
14 //
15 //        T
16 //       / \
17 //    i32   i64
18
19 impl<T> Foo for T {
20     default fn foo(&self) -> bool { false }
21 }
22
23 impl Foo for i32 {}
24
25 impl Foo for i64 {
26     fn foo(&self) -> bool { true }
27 }
28
29 fn test_foo() {
30     assert!(!0i8.foo());
31     assert!(!0i32.foo());
32     assert!(0i64.foo());
33 }
34
35 // Next, test mixture of explicit `default` and provided methods:
36
37 trait Bar {
38     fn bar(&self) -> i32 { 0 }
39 }
40
41 // Specialization tree for Bar.
42 // Uses of $ designate that method is provided
43 //
44 //           $Bar   (the trait)
45 //             |
46 //             T
47 //            /|\
48 //           / | \
49 //          /  |  \
50 //         /   |   \
51 //        /    |    \
52 //       /     |     \
53 //     $i32   &str  $Vec<T>
54 //                    /\
55 //                   /  \
56 //            Vec<i32>  $Vec<i64>
57
58 impl<T> Bar for T {
59     default fn bar(&self) -> i32 { 0 }
60 }
61
62 impl Bar for i32 {
63     fn bar(&self) -> i32 { 1 }
64 }
65 impl<'a> Bar for &'a str {}
66
67 impl<T> Bar for Vec<T> {
68     default fn bar(&self) -> i32 { 2 }
69 }
70 impl Bar for Vec<i32> {}
71 impl Bar for Vec<i64> {
72     fn bar(&self) -> i32 { 3 }
73 }
74
75 fn test_bar() {
76     assert!(0u8.bar() == 0);
77     assert!(0i32.bar() == 1);
78     assert!("hello".bar() == 0);
79     assert!(vec![()].bar() == 2);
80     assert!(vec![0i32].bar() == 2);
81     assert!(vec![0i64].bar() == 3);
82 }
83
84 fn main() {
85     test_foo();
86     test_bar();
87 }