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