From 9e60f4511e39d012d5a21c10c55d706ec1e75e53 Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Wed, 28 Oct 2020 10:36:19 +0000 Subject: [PATCH 1/3] Add const generics tests for supertraits + dyn traits. --- src/test/ui/const-generics/dyn-supertraits.rs | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/test/ui/const-generics/dyn-supertraits.rs diff --git a/src/test/ui/const-generics/dyn-supertraits.rs b/src/test/ui/const-generics/dyn-supertraits.rs new file mode 100644 index 0000000000000..b72dd9cc90cd4 --- /dev/null +++ b/src/test/ui/const-generics/dyn-supertraits.rs @@ -0,0 +1,58 @@ +// check-pass +// revisions: full min + +#![cfg_attr(full, feature(const_generics))] +#![cfg_attr(full, allow(incomplete_features))] +#![cfg_attr(min, feature(min_const_generics))] + +trait Foo {} +trait Bar : Foo {} +trait Baz: Foo<3> {} + +struct FooType {} +struct BarType {} +struct BazType {} + +impl Foo for FooType {} +impl Foo for BarType {} +impl Bar for BarType {} +impl Foo<3> for BazType {} +impl Baz for BazType {} + +trait Foz {} +trait Boz: Foo<3> + Foz {} +trait Bok: Foo + Foz {} + +struct FozType {} +struct BozType {} +struct BokType {} + +impl Foz for FozType {} + +impl Foz for BozType {} +impl Foo<3> for BozType {} +impl Boz for BozType {} + +impl Foz for BokType {} +impl Foo for BokType {} +impl Bok for BokType {} + +fn a(x: &dyn Foo) {} +fn b(x: &dyn Foo<3>) {} + +fn main() { + let foo = FooType::<3> {}; + a(&foo); b(&foo); + + let bar = BarType::<3> {}; + a(&bar); b(&bar); + + let baz = BazType {}; + a(&baz); b(&baz); + + let boz = BozType {}; + a(&boz); b(&boz); + + let bok = BokType::<3> {}; + a(&bok); b(&bok); +} From fab79c27ef184ee3620681bfbdc1fd89ad10b4df Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Wed, 28 Oct 2020 12:29:13 +0000 Subject: [PATCH 2/3] Extend test to cover dyn methods/functions. --- src/test/ui/const-generics/dyn-supertraits.rs | 64 +++++++++++++------ 1 file changed, 44 insertions(+), 20 deletions(-) diff --git a/src/test/ui/const-generics/dyn-supertraits.rs b/src/test/ui/const-generics/dyn-supertraits.rs index b72dd9cc90cd4..8b956988c7c46 100644 --- a/src/test/ui/const-generics/dyn-supertraits.rs +++ b/src/test/ui/const-generics/dyn-supertraits.rs @@ -1,58 +1,82 @@ -// check-pass +// run-pass // revisions: full min #![cfg_attr(full, feature(const_generics))] #![cfg_attr(full, allow(incomplete_features))] #![cfg_attr(min, feature(min_const_generics))] -trait Foo {} +trait Foo { + fn myfun(&self) -> usize; +} trait Bar : Foo {} trait Baz: Foo<3> {} -struct FooType {} -struct BarType {} -struct BazType {} +struct FooType; +struct BarType; +struct BazType; -impl Foo for FooType {} -impl Foo for BarType {} +impl Foo for FooType { + fn myfun(&self) -> usize { N } +} +impl Foo for BarType { + fn myfun(&self) -> usize { N + 1 } +} impl Bar for BarType {} -impl Foo<3> for BazType {} +impl Foo<3> for BazType { + fn myfun(&self) -> usize { 999 } +} impl Baz for BazType {} trait Foz {} trait Boz: Foo<3> + Foz {} trait Bok: Foo + Foz {} -struct FozType {} -struct BozType {} -struct BokType {} +struct FozType; +struct BozType; +struct BokType; impl Foz for FozType {} impl Foz for BozType {} -impl Foo<3> for BozType {} +impl Foo<3> for BozType { + fn myfun(&self) -> usize { 9999 } +} impl Boz for BozType {} impl Foz for BokType {} -impl Foo for BokType {} +impl Foo for BokType { + fn myfun(&self) -> usize { N + 2 } +} impl Bok for BokType {} -fn a(x: &dyn Foo) {} -fn b(x: &dyn Foo<3>) {} +fn a(_: &dyn Foo) {} +fn b(_: &dyn Foo<3>) {} +fn c, const N: usize>(x: T) { a::(&x); } +fn d>(_: &T) {} +fn e(x: &dyn Bar<3>) { d(x); } + +fn get_myfun(x: &dyn Foo) -> usize { x.myfun() } fn main() { let foo = FooType::<3> {}; - a(&foo); b(&foo); + a(&foo); b(&foo); d(&foo); + assert!(get_myfun(&foo) == 3); let bar = BarType::<3> {}; - a(&bar); b(&bar); + a(&bar); b(&bar); d(&bar); e(&bar); + assert!(get_myfun(&bar) == 4); let baz = BazType {}; - a(&baz); b(&baz); + a(&baz); b(&baz); d(&baz); + assert!(get_myfun(&baz) == 999); let boz = BozType {}; - a(&boz); b(&boz); + a(&boz); b(&boz); d(&boz); + assert!(get_myfun(&boz) == 9999); let bok = BokType::<3> {}; - a(&bok); b(&bok); + a(&bok); b(&bok); d(&bok); + assert!(get_myfun(&bok) == 5); + + c(BokType::<3> {}); } From 22060fa0dc0f40652da875fd525e2a898c8584c5 Mon Sep 17 00:00:00 2001 From: Hameer Abbasi Date: Wed, 28 Oct 2020 12:51:15 +0000 Subject: [PATCH 3/3] Assert in every case. --- src/test/ui/const-generics/dyn-supertraits.rs | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/src/test/ui/const-generics/dyn-supertraits.rs b/src/test/ui/const-generics/dyn-supertraits.rs index 8b956988c7c46..0295255d8099c 100644 --- a/src/test/ui/const-generics/dyn-supertraits.rs +++ b/src/test/ui/const-generics/dyn-supertraits.rs @@ -49,34 +49,37 @@ impl Foo for BokType { } impl Bok for BokType {} -fn a(_: &dyn Foo) {} -fn b(_: &dyn Foo<3>) {} -fn c, const N: usize>(x: T) { a::(&x); } -fn d>(_: &T) {} -fn e(x: &dyn Bar<3>) { d(x); } - -fn get_myfun(x: &dyn Foo) -> usize { x.myfun() } +fn a(x: &dyn Foo) -> usize { x.myfun() } +fn b(x: &dyn Foo<3>) -> usize { x.myfun() } +fn c, const N: usize>(x: T) -> usize { a::(&x) } +fn d>(x: &T) -> usize { x.myfun() } +fn e(x: &dyn Bar<3>) -> usize { d(x) } fn main() { let foo = FooType::<3> {}; - a(&foo); b(&foo); d(&foo); - assert!(get_myfun(&foo) == 3); + assert!(a(&foo) == 3); + assert!(b(&foo) == 3); + assert!(d(&foo) == 3); let bar = BarType::<3> {}; - a(&bar); b(&bar); d(&bar); e(&bar); - assert!(get_myfun(&bar) == 4); + assert!(a(&bar) == 4); + assert!(b(&bar) == 4); + assert!(d(&bar) == 4); + assert!(e(&bar) == 4); let baz = BazType {}; - a(&baz); b(&baz); d(&baz); - assert!(get_myfun(&baz) == 999); + assert!(a(&baz) == 999); + assert!(b(&baz) == 999); + assert!(d(&baz) == 999); let boz = BozType {}; - a(&boz); b(&boz); d(&boz); - assert!(get_myfun(&boz) == 9999); + assert!(a(&boz) == 9999); + assert!(b(&boz) == 9999); + assert!(d(&boz) == 9999); let bok = BokType::<3> {}; - a(&bok); b(&bok); d(&bok); - assert!(get_myfun(&bok) == 5); - - c(BokType::<3> {}); + assert!(a(&bok) == 5); + assert!(b(&bok) == 5); + assert!(d(&bok) == 5); + assert!(c(BokType::<3> {}) == 5); }