]> git.proxmox.com Git - rustc.git/blame - src/test/ui/const-generics/dyn-supertraits.rs
New upstream version 1.56.0~beta.4+dfsg1
[rustc.git] / src / test / ui / const-generics / dyn-supertraits.rs
CommitLineData
29967ef6 1// run-pass
29967ef6
XL
2
3trait Foo<const N: usize> {
4 fn myfun(&self) -> usize;
5}
6trait Bar<const N: usize> : Foo<N> {}
7trait Baz: Foo<3> {}
8
9struct FooType<const N: usize>;
10struct BarType<const N: usize>;
11struct BazType;
12
13impl<const N: usize> Foo<N> for FooType<N> {
14 fn myfun(&self) -> usize { N }
15}
16impl<const N: usize> Foo<N> for BarType<N> {
17 fn myfun(&self) -> usize { N + 1 }
18}
19impl<const N: usize> Bar<N> for BarType<N> {}
20impl Foo<3> for BazType {
21 fn myfun(&self) -> usize { 999 }
22}
23impl Baz for BazType {}
24
25trait Foz {}
26trait Boz: Foo<3> + Foz {}
27trait Bok<const N: usize>: Foo<N> + Foz {}
28
29struct FozType;
30struct BozType;
31struct BokType<const N: usize>;
32
33impl Foz for FozType {}
34
35impl Foz for BozType {}
36impl Foo<3> for BozType {
37 fn myfun(&self) -> usize { 9999 }
38}
39impl Boz for BozType {}
40
41impl<const N: usize> Foz for BokType<N> {}
42impl<const N: usize> Foo<N> for BokType<N> {
43 fn myfun(&self) -> usize { N + 2 }
44}
45impl<const N: usize> Bok<N> for BokType<N> {}
46
47fn a<const N: usize>(x: &dyn Foo<N>) -> usize { x.myfun() }
48fn b(x: &dyn Foo<3>) -> usize { x.myfun() }
49fn c<T: Bok<N>, const N: usize>(x: T) -> usize { a::<N>(&x) }
50fn d<T: ?Sized + Foo<3>>(x: &T) -> usize { x.myfun() }
51fn e(x: &dyn Bar<3>) -> usize { d(x) }
52
53fn 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}