2024-02-16 14:02:50 -06:00
|
|
|
//@ run-pass
|
2020-10-28 05:36:19 -05:00
|
|
|
|
2020-10-28 07:29:13 -05:00
|
|
|
trait Foo<const N: usize> {
|
|
|
|
fn myfun(&self) -> usize;
|
|
|
|
}
|
2020-10-28 05:36:19 -05:00
|
|
|
trait Bar<const N: usize> : Foo<N> {}
|
2024-02-06 20:42:01 -06:00
|
|
|
trait Baz: Foo<3> {} //~ WARN trait `Baz` is never used
|
2020-10-28 05:36:19 -05:00
|
|
|
|
2020-10-28 07:29:13 -05:00
|
|
|
struct FooType<const N: usize>;
|
|
|
|
struct BarType<const N: usize>;
|
|
|
|
struct BazType;
|
2020-10-28 05:36:19 -05:00
|
|
|
|
2020-10-28 07:29:13 -05:00
|
|
|
impl<const N: usize> Foo<N> for FooType<N> {
|
|
|
|
fn myfun(&self) -> usize { N }
|
|
|
|
}
|
|
|
|
impl<const N: usize> Foo<N> for BarType<N> {
|
|
|
|
fn myfun(&self) -> usize { N + 1 }
|
|
|
|
}
|
2020-10-28 05:36:19 -05:00
|
|
|
impl<const N: usize> Bar<N> for BarType<N> {}
|
2020-10-28 07:29:13 -05:00
|
|
|
impl Foo<3> for BazType {
|
|
|
|
fn myfun(&self) -> usize { 999 }
|
|
|
|
}
|
2020-10-28 05:36:19 -05:00
|
|
|
impl Baz for BazType {}
|
|
|
|
|
|
|
|
trait Foz {}
|
2024-02-06 20:42:01 -06:00
|
|
|
trait Boz: Foo<3> + Foz {} //~ WARN trait `Boz` is never used
|
2020-10-28 05:36:19 -05:00
|
|
|
trait Bok<const N: usize>: Foo<N> + Foz {}
|
|
|
|
|
2024-02-06 20:42:01 -06:00
|
|
|
struct FozType; //~ WARN struct `FozType` is never constructed
|
2020-10-28 07:29:13 -05:00
|
|
|
struct BozType;
|
|
|
|
struct BokType<const N: usize>;
|
2020-10-28 05:36:19 -05:00
|
|
|
|
|
|
|
impl Foz for FozType {}
|
|
|
|
|
|
|
|
impl Foz for BozType {}
|
2020-10-28 07:29:13 -05:00
|
|
|
impl Foo<3> for BozType {
|
|
|
|
fn myfun(&self) -> usize { 9999 }
|
|
|
|
}
|
2020-10-28 05:36:19 -05:00
|
|
|
impl Boz for BozType {}
|
|
|
|
|
|
|
|
impl<const N: usize> Foz for BokType<N> {}
|
2020-10-28 07:29:13 -05:00
|
|
|
impl<const N: usize> Foo<N> for BokType<N> {
|
|
|
|
fn myfun(&self) -> usize { N + 2 }
|
|
|
|
}
|
2020-10-28 05:36:19 -05:00
|
|
|
impl<const N: usize> Bok<N> for BokType<N> {}
|
|
|
|
|
2020-10-28 07:51:15 -05:00
|
|
|
fn a<const N: usize>(x: &dyn Foo<N>) -> usize { x.myfun() }
|
|
|
|
fn b(x: &dyn Foo<3>) -> usize { x.myfun() }
|
|
|
|
fn c<T: Bok<N>, const N: usize>(x: T) -> usize { a::<N>(&x) }
|
|
|
|
fn d<T: ?Sized + Foo<3>>(x: &T) -> usize { x.myfun() }
|
|
|
|
fn e(x: &dyn Bar<3>) -> usize { d(x) }
|
2020-10-28 05:36:19 -05:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let foo = FooType::<3> {};
|
2020-10-28 07:51:15 -05:00
|
|
|
assert!(a(&foo) == 3);
|
|
|
|
assert!(b(&foo) == 3);
|
|
|
|
assert!(d(&foo) == 3);
|
2020-10-28 05:36:19 -05:00
|
|
|
|
|
|
|
let bar = BarType::<3> {};
|
2020-10-28 07:51:15 -05:00
|
|
|
assert!(a(&bar) == 4);
|
|
|
|
assert!(b(&bar) == 4);
|
|
|
|
assert!(d(&bar) == 4);
|
|
|
|
assert!(e(&bar) == 4);
|
2020-10-28 05:36:19 -05:00
|
|
|
|
|
|
|
let baz = BazType {};
|
2020-10-28 07:51:15 -05:00
|
|
|
assert!(a(&baz) == 999);
|
|
|
|
assert!(b(&baz) == 999);
|
|
|
|
assert!(d(&baz) == 999);
|
2020-10-28 05:36:19 -05:00
|
|
|
|
|
|
|
let boz = BozType {};
|
2020-10-28 07:51:15 -05:00
|
|
|
assert!(a(&boz) == 9999);
|
|
|
|
assert!(b(&boz) == 9999);
|
|
|
|
assert!(d(&boz) == 9999);
|
2020-10-28 05:36:19 -05:00
|
|
|
|
|
|
|
let bok = BokType::<3> {};
|
2020-10-28 07:51:15 -05:00
|
|
|
assert!(a(&bok) == 5);
|
|
|
|
assert!(b(&bok) == 5);
|
|
|
|
assert!(d(&bok) == 5);
|
|
|
|
assert!(c(BokType::<3> {}) == 5);
|
2020-10-28 05:36:19 -05:00
|
|
|
}
|