Skip to content

Commit 572ea25

Browse files
authored
Rollup merge of rust-lang#78478 - hameerabbasi:const-generics-supertraits, r=lcnr
Add const generics tests for supertraits + dyn traits. Partially addresses rust-lang#78433
2 parents 5e3cc6e + 22060fa commit 572ea25

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

0 commit comments

Comments
 (0)