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