From e95db1bde46dc7ee2e3062c278458b873279b2af Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 18 Sep 2024 21:31:48 -0400 Subject: [PATCH] Add more tests --- .../supertrait-shadowing/assoc-const.rs | 23 +++++++++++ .../assoc-const.run.stdout | 1 + .../false-subtrait-after-inference.rs | 23 +++++++++++ .../false-subtrait-after-inference.stderr | 38 +++++++++++++++++++ .../supertrait-shadowing/out-of-scope.rs | 25 ++++++++++++ .../out-of-scope.run.stdout | 1 + .../trivially-false-subtrait.rs | 26 +++++++++++++ .../supertrait-shadowing/type-dependent.rs | 29 ++++++++++++++ .../type-dependent.run.stdout | 1 + 9 files changed, 167 insertions(+) create mode 100644 tests/ui/methods/supertrait-shadowing/assoc-const.rs create mode 100644 tests/ui/methods/supertrait-shadowing/assoc-const.run.stdout create mode 100644 tests/ui/methods/supertrait-shadowing/false-subtrait-after-inference.rs create mode 100644 tests/ui/methods/supertrait-shadowing/false-subtrait-after-inference.stderr create mode 100644 tests/ui/methods/supertrait-shadowing/out-of-scope.rs create mode 100644 tests/ui/methods/supertrait-shadowing/out-of-scope.run.stdout create mode 100644 tests/ui/methods/supertrait-shadowing/trivially-false-subtrait.rs create mode 100644 tests/ui/methods/supertrait-shadowing/type-dependent.rs create mode 100644 tests/ui/methods/supertrait-shadowing/type-dependent.run.stdout diff --git a/tests/ui/methods/supertrait-shadowing/assoc-const.rs b/tests/ui/methods/supertrait-shadowing/assoc-const.rs new file mode 100644 index 0000000000000..a542ce7d326d1 --- /dev/null +++ b/tests/ui/methods/supertrait-shadowing/assoc-const.rs @@ -0,0 +1,23 @@ +//@ run-pass +//@ check-run-results + +#![feature(supertrait_item_shadowing)] +#![allow(dead_code)] + +trait A { + const CONST: i32; +} +impl A for T { + const CONST: i32 = 1; +} + +trait B: A { + const CONST: i32; +} +impl B for T { + const CONST: i32 = 2; +} + +fn main() { + println!("{}", i32::CONST); +} diff --git a/tests/ui/methods/supertrait-shadowing/assoc-const.run.stdout b/tests/ui/methods/supertrait-shadowing/assoc-const.run.stdout new file mode 100644 index 0000000000000..0cfbf08886fca --- /dev/null +++ b/tests/ui/methods/supertrait-shadowing/assoc-const.run.stdout @@ -0,0 +1 @@ +2 diff --git a/tests/ui/methods/supertrait-shadowing/false-subtrait-after-inference.rs b/tests/ui/methods/supertrait-shadowing/false-subtrait-after-inference.rs new file mode 100644 index 0000000000000..dad35e03513bd --- /dev/null +++ b/tests/ui/methods/supertrait-shadowing/false-subtrait-after-inference.rs @@ -0,0 +1,23 @@ +#![feature(supertrait_item_shadowing)] + +struct W(T); + +trait Upstream { + fn hello(&self) {} +} +impl Upstream for T {} + +trait Downstream: Upstream { + fn hello(&self) {} +} +impl Downstream for W where T: Foo {} + +trait Foo {} + +fn main() { + let x = W(Default::default()); + x.hello(); + //~^ ERROR the trait bound `i32: Foo` is not satisfied + //~| WARN trait method `hello` from `Downstream` shadows identically named method from supertrait + let _: i32 = x.0; +} diff --git a/tests/ui/methods/supertrait-shadowing/false-subtrait-after-inference.stderr b/tests/ui/methods/supertrait-shadowing/false-subtrait-after-inference.stderr new file mode 100644 index 0000000000000..11ca83502fd9d --- /dev/null +++ b/tests/ui/methods/supertrait-shadowing/false-subtrait-after-inference.stderr @@ -0,0 +1,38 @@ +warning: trait method `hello` from `Downstream` shadows identically named method from supertrait + --> $DIR/false-subtrait-after-inference.rs:19:7 + | +LL | x.hello(); + | ^^^^^ + | +note: method from `Downstream` shadows a supertrait method + --> $DIR/false-subtrait-after-inference.rs:11:5 + | +LL | fn hello(&self) {} + | ^^^^^^^^^^^^^^^ +note: method from `Upstream` is shadowed by a subtrait method + --> $DIR/false-subtrait-after-inference.rs:6:5 + | +LL | fn hello(&self) {} + | ^^^^^^^^^^^^^^^ + = note: `#[warn(supertrait_item_shadowing)]` on by default + +error[E0277]: the trait bound `i32: Foo` is not satisfied + --> $DIR/false-subtrait-after-inference.rs:19:7 + | +LL | x.hello(); + | ^^^^^ the trait `Foo` is not implemented for `i32`, which is required by `W<_>: Downstream` + | +help: this trait has no implementations, consider adding one + --> $DIR/false-subtrait-after-inference.rs:15:1 + | +LL | trait Foo {} + | ^^^^^^^^^ +note: required for `W` to implement `Downstream` + --> $DIR/false-subtrait-after-inference.rs:13:9 + | +LL | impl Downstream for W where T: Foo {} + | ^^^^^^^^^^ ^^^^ --- unsatisfied trait bound introduced here + +error: aborting due to 1 previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0277`. diff --git a/tests/ui/methods/supertrait-shadowing/out-of-scope.rs b/tests/ui/methods/supertrait-shadowing/out-of-scope.rs new file mode 100644 index 0000000000000..bd263be59cc7d --- /dev/null +++ b/tests/ui/methods/supertrait-shadowing/out-of-scope.rs @@ -0,0 +1,25 @@ +//@ run-pass +//@ check-run-results + +#![feature(supertrait_item_shadowing)] +#![allow(dead_code)] + +mod out_of_scope { + pub trait Subtrait: super::Supertrait { + fn hello(&self) { + println!("subtrait"); + } + } + impl Subtrait for T {} +} + +trait Supertrait { + fn hello(&self) { + println!("supertrait"); + } +} +impl Supertrait for T {} + +fn main() { + ().hello(); +} diff --git a/tests/ui/methods/supertrait-shadowing/out-of-scope.run.stdout b/tests/ui/methods/supertrait-shadowing/out-of-scope.run.stdout new file mode 100644 index 0000000000000..1019e5f354346 --- /dev/null +++ b/tests/ui/methods/supertrait-shadowing/out-of-scope.run.stdout @@ -0,0 +1 @@ +supertrait diff --git a/tests/ui/methods/supertrait-shadowing/trivially-false-subtrait.rs b/tests/ui/methods/supertrait-shadowing/trivially-false-subtrait.rs new file mode 100644 index 0000000000000..e44c7c18083d0 --- /dev/null +++ b/tests/ui/methods/supertrait-shadowing/trivially-false-subtrait.rs @@ -0,0 +1,26 @@ +//@ check-pass + +// Make sure we don't prefer a subtrait that we would've otherwise eliminated +// in `consider_probe` during method probing. + +#![feature(supertrait_item_shadowing)] +#![allow(dead_code)] + +struct W(T); + +trait Upstream { + fn hello(&self) {} +} +impl Upstream for T {} + +trait Downstream: Upstream { + fn hello(&self) {} +} +impl Downstream for W where T: Foo {} + +trait Foo {} + +fn main() { + let x = W(1i32); + x.hello(); +} diff --git a/tests/ui/methods/supertrait-shadowing/type-dependent.rs b/tests/ui/methods/supertrait-shadowing/type-dependent.rs new file mode 100644 index 0000000000000..3af884fd52dcd --- /dev/null +++ b/tests/ui/methods/supertrait-shadowing/type-dependent.rs @@ -0,0 +1,29 @@ +//@ run-pass +//@ check-run-results + +// Makes sure we can shadow with type-dependent method syntax. + +#![feature(supertrait_item_shadowing)] +#![allow(dead_code)] + +trait A { + fn hello() { + println!("A"); + } +} +impl A for T {} + +trait B: A { + fn hello() { + println!("B"); + } +} +impl B for T {} + +fn foo() { + T::hello(); +} + +fn main() { + foo::<()>(); +} diff --git a/tests/ui/methods/supertrait-shadowing/type-dependent.run.stdout b/tests/ui/methods/supertrait-shadowing/type-dependent.run.stdout new file mode 100644 index 0000000000000..223b7836fb19f --- /dev/null +++ b/tests/ui/methods/supertrait-shadowing/type-dependent.run.stdout @@ -0,0 +1 @@ +B