]> git.lizzy.rs Git - rust.git/blob - src/test/ui/const-generics/dyn-supertraits.rs
Merge commit '4911ab124c481430672a3833b37075e6435ec34d' into clippyup
[rust.git] / src / test / ui / const-generics / dyn-supertraits.rs
1 // run-pass
2 // revisions: full min
3
4 #![cfg_attr(full, feature(const_generics))]
5 #![cfg_attr(full, allow(incomplete_features))]
6 #![cfg_attr(min, feature(min_const_generics))]
7
8 trait Foo<const N: usize> {
9     fn myfun(&self) -> usize;
10 }
11 trait Bar<const N: usize> : Foo<N> {}
12 trait Baz: Foo<3> {}
13
14 struct FooType<const N: usize>;
15 struct BarType<const N: usize>;
16 struct BazType;
17
18 impl<const N: usize> Foo<N> for FooType<N> {
19     fn myfun(&self) -> usize { N }
20 }
21 impl<const N: usize> Foo<N> for BarType<N> {
22     fn myfun(&self) -> usize { N + 1 }
23 }
24 impl<const N: usize> Bar<N> for BarType<N> {}
25 impl Foo<3> for BazType {
26     fn myfun(&self) -> usize { 999 }
27 }
28 impl Baz for BazType {}
29
30 trait Foz {}
31 trait Boz: Foo<3> + Foz {}
32 trait Bok<const N: usize>: Foo<N> + Foz {}
33
34 struct FozType;
35 struct BozType;
36 struct BokType<const N: usize>;
37
38 impl Foz for FozType {}
39
40 impl Foz for BozType {}
41 impl Foo<3> for BozType {
42     fn myfun(&self) -> usize { 9999 }
43 }
44 impl Boz for BozType {}
45
46 impl<const N: usize> Foz for BokType<N> {}
47 impl<const N: usize> Foo<N> for BokType<N> {
48     fn myfun(&self) -> usize { N + 2 }
49 }
50 impl<const N: usize> Bok<N> for BokType<N> {}
51
52 fn a<const N: usize>(x: &dyn Foo<N>) -> usize { x.myfun() }
53 fn b(x: &dyn Foo<3>) -> usize { x.myfun() }
54 fn c<T: Bok<N>, const N: usize>(x: T) -> usize { a::<N>(&x) }
55 fn d<T: ?Sized + Foo<3>>(x: &T) -> usize { x.myfun() }
56 fn e(x: &dyn Bar<3>) -> usize { d(x) }
57
58 fn main() {
59     let foo = FooType::<3> {};
60     assert!(a(&foo) == 3);
61     assert!(b(&foo) == 3);
62     assert!(d(&foo) == 3);
63
64     let bar = BarType::<3> {};
65     assert!(a(&bar) == 4);
66     assert!(b(&bar) == 4);
67     assert!(d(&bar) == 4);
68     assert!(e(&bar) == 4);
69
70     let baz = BazType {};
71     assert!(a(&baz) == 999);
72     assert!(b(&baz) == 999);
73     assert!(d(&baz) == 999);
74
75     let boz = BozType {};
76     assert!(a(&boz) == 9999);
77     assert!(b(&boz) == 9999);
78     assert!(d(&boz) == 9999);
79
80     let bok = BokType::<3> {};
81     assert!(a(&bok) == 5);
82     assert!(b(&bok) == 5);
83     assert!(d(&bok) == 5);
84     assert!(c(BokType::<3> {}) == 5);
85 }