From e1e0989a0d53d7521b9efdc774236c25bff844bd Mon Sep 17 00:00:00 2001 From: Charles Lew Date: Wed, 18 Aug 2021 19:02:28 +0800 Subject: [PATCH] Add a new test case to verify behavior. --- .../correct-supertrait-substitution.rs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/test/ui/traits/trait-upcasting/correct-supertrait-substitution.rs diff --git a/src/test/ui/traits/trait-upcasting/correct-supertrait-substitution.rs b/src/test/ui/traits/trait-upcasting/correct-supertrait-substitution.rs new file mode 100644 index 00000000000..8d0a9ef0ace --- /dev/null +++ b/src/test/ui/traits/trait-upcasting/correct-supertrait-substitution.rs @@ -0,0 +1,39 @@ +// run-pass +#![feature(trait_upcasting)] +#![allow(incomplete_features)] + +trait Foo: Bar + Bar {} +trait Bar { + fn bar(&self) -> String { + T::default().to_string() + } +} + +struct S1; + +impl Bar for S1 {} +impl Foo for S1 {} + +struct S2; +impl Bar for S2 {} +impl Bar for S2 {} +impl Foo for S2 {} + +fn test1(x: &dyn Foo) { + let s = x as &dyn Bar; + assert_eq!("0", &s.bar().to_string()); +} + +fn test2(x: &dyn Foo) { + let p = x as &dyn Bar; + assert_eq!("0", &p.bar().to_string()); + let q = x as &dyn Bar; + assert_eq!("false", &q.bar().to_string()); +} + +fn main() { + let s1 = S1; + test1(&s1); + let s2 = S2; + test2(&s2); +}