diff --git a/tests/ui/dyn-keyword/inferred-type-in-qualified-path-for-with-one-impl.rs b/tests/ui/dyn-keyword/inferred-type-in-qualified-path-for-with-one-impl.rs new file mode 100644 index 0000000000000..c1487e816955c --- /dev/null +++ b/tests/ui/dyn-keyword/inferred-type-in-qualified-path-for-with-one-impl.rs @@ -0,0 +1,10 @@ +trait Trait { + fn function() {} +} + +impl Trait for () {} + +fn main() { + Trait::function(); + //~^ ERROR cannot call associated function on trait without specifying the corresponding `impl` type +} diff --git a/tests/ui/dyn-keyword/inferred-type-in-qualified-path-for-with-one-impl.stderr b/tests/ui/dyn-keyword/inferred-type-in-qualified-path-for-with-one-impl.stderr new file mode 100644 index 0000000000000..84f144d191e45 --- /dev/null +++ b/tests/ui/dyn-keyword/inferred-type-in-qualified-path-for-with-one-impl.stderr @@ -0,0 +1,17 @@ +error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type + --> $DIR/inferred-type-in-qualified-path-for-with-one-impl.rs:8:5 + | +LL | fn function() {} + | ---------------- `Trait::function` defined here +... +LL | Trait::function(); + | ^^^^^^^^^^^^^^^^^ cannot call associated function of trait + | +help: use the fully-qualified path to the only available implementation + | +LL | <() as Trait>::function(); + | ++++++ + + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0790`. diff --git a/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-safe-function-with-one-impl.rs b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-safe-function-with-one-impl.rs new file mode 100644 index 0000000000000..d01e3b2e0d197 --- /dev/null +++ b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-safe-function-with-one-impl.rs @@ -0,0 +1,9 @@ +trait Trait { + fn function() {} +} + +impl Trait for () {} + +fn main() { + ::function(); //~ ERROR the trait `Trait` is not dyn compatible +} diff --git a/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-safe-function-with-one-impl.stderr b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-safe-function-with-one-impl.stderr new file mode 100644 index 0000000000000..a50699d5f6686 --- /dev/null +++ b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-safe-function-with-one-impl.stderr @@ -0,0 +1,27 @@ +error[E0038]: the trait `Trait` is not dyn compatible + --> $DIR/trait-dyn-in-qualified-path-for-object-safe-function-with-one-impl.rs:8:6 + | +LL | ::function(); + | ^^^^^^^^^ `Trait` is not dyn compatible + | +note: for a trait to be dyn compatible it needs to allow building a vtable + for more information, visit + --> $DIR/trait-dyn-in-qualified-path-for-object-safe-function-with-one-impl.rs:2:8 + | +LL | trait Trait { + | ----- this trait is not dyn compatible... +LL | fn function() {} + | ^^^^^^^^ ...because associated function `function` has no `self` parameter + = help: only type `()` implements `Trait`; consider using it directly instead. +help: consider turning `function` into a method by giving it a `&self` argument + | +LL | fn function(&self) {} + | +++++ +help: alternatively, consider constraining `function` so it does not apply to trait objects + | +LL | fn function() where Self: Sized {} + | +++++++++++++++++ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0038`. diff --git a/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-safe-function.rs b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-safe-function.rs new file mode 100644 index 0000000000000..fe2c28672eb91 --- /dev/null +++ b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-safe-function.rs @@ -0,0 +1,14 @@ +//@ run-pass + +trait Trait { + fn function(&self) {} +} + +impl dyn Trait { +} + +impl Trait for () {} + +fn main() { + Trait::function(&()); +} diff --git a/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs new file mode 100644 index 0000000000000..97e2073e59137 --- /dev/null +++ b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs @@ -0,0 +1,13 @@ +trait Trait: Sized {} + +impl dyn Trait { //~ ERROR the trait `Trait` is not dyn compatible + fn function(&self) {} //~ ERROR the trait `Trait` is not dyn compatible +} + +impl Trait for () {} + +fn main() { + ::function(&()); + //~^ ERROR the trait `Trait` is not dyn compatible + //~| ERROR the trait `Trait` is not dyn compatible +} diff --git a/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-unsafe-trait.stderr b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-unsafe-trait.stderr new file mode 100644 index 0000000000000..ff2028305b21f --- /dev/null +++ b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-for-object-unsafe-trait.stderr @@ -0,0 +1,68 @@ +error[E0038]: the trait `Trait` is not dyn compatible + --> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:3:6 + | +LL | impl dyn Trait { + | ^^^^^^^^^ `Trait` is not dyn compatible + | +note: for a trait to be dyn compatible it needs to allow building a vtable + for more information, visit + --> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:1:14 + | +LL | trait Trait: Sized {} + | ----- ^^^^^ ...because it requires `Self: Sized` + | | + | this trait is not dyn compatible... + = help: only type `()` implements `Trait`; consider using it directly instead. + +error[E0038]: the trait `Trait` is not dyn compatible + --> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:4:18 + | +LL | fn function(&self) {} + | ^^^^ `Trait` is not dyn compatible + | +note: for a trait to be dyn compatible it needs to allow building a vtable + for more information, visit + --> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:1:14 + | +LL | trait Trait: Sized {} + | ----- ^^^^^ ...because it requires `Self: Sized` + | | + | this trait is not dyn compatible... + = help: only type `()` implements `Trait`; consider using it directly instead. + +error[E0038]: the trait `Trait` is not dyn compatible + --> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:10:27 + | +LL | ::function(&()); + | ^^^ `Trait` is not dyn compatible + | +note: for a trait to be dyn compatible it needs to allow building a vtable + for more information, visit + --> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:1:14 + | +LL | trait Trait: Sized {} + | ----- ^^^^^ ...because it requires `Self: Sized` + | | + | this trait is not dyn compatible... + = help: only type `()` implements `Trait`; consider using it directly instead. + = note: required for the cast from `&()` to `&(dyn Trait + 'static)` + +error[E0038]: the trait `Trait` is not dyn compatible + --> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:10:6 + | +LL | ::function(&()); + | ^^^^^^^^^ `Trait` is not dyn compatible + | +note: for a trait to be dyn compatible it needs to allow building a vtable + for more information, visit + --> $DIR/trait-dyn-in-qualified-path-for-object-unsafe-trait.rs:1:14 + | +LL | trait Trait: Sized {} + | ----- ^^^^^ ...because it requires `Self: Sized` + | | + | this trait is not dyn compatible... + = help: only type `()` implements `Trait`; consider using it directly instead. + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0038`. diff --git a/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-with-collision.rs b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-with-collision.rs new file mode 100644 index 0000000000000..bb9f3a09d55ec --- /dev/null +++ b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-with-collision.rs @@ -0,0 +1,11 @@ +trait Trait { + fn function(&self) {} +} +impl dyn Trait { + fn function(&self) {} +} +impl Trait for () {} +fn main() { + ::function(&()); + //~^ ERROR multiple applicable items in scope +} diff --git a/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-with-collision.stderr b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-with-collision.stderr new file mode 100644 index 0000000000000..07107c78fc4ce --- /dev/null +++ b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path-with-collision.stderr @@ -0,0 +1,22 @@ +error[E0034]: multiple applicable items in scope + --> $DIR/trait-dyn-in-qualified-path-with-collision.rs:9:18 + | +LL | ::function(&()); + | -------------^^^^^^^^ multiple `function` found + | | + | help: use fully-qualified syntax to disambiguate: `Trait::` + | +note: candidate #1 is defined in the trait `Trait` + --> $DIR/trait-dyn-in-qualified-path-with-collision.rs:2:5 + | +LL | fn function(&self) {} + | ^^^^^^^^^^^^^^^^^^ +note: candidate #2 is defined in an impl for the type `(dyn Trait + 'static)` + --> $DIR/trait-dyn-in-qualified-path-with-collision.rs:5:5 + | +LL | fn function(&self) {} + | ^^^^^^^^^^^^^^^^^^ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0034`. diff --git a/tests/ui/dyn-keyword/trait-dyn-in-qualified-path.rs b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path.rs new file mode 100644 index 0000000000000..6a37cf6faa919 --- /dev/null +++ b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path.rs @@ -0,0 +1,8 @@ +trait Trait: Sized { + fn function() {} +} +fn main() { + ::function(); + //~^ ERROR trait `Trait` is not dyn compatible + //~| ERROR the size for values of type `dyn Trait` cannot be known at compilation time +} diff --git a/tests/ui/dyn-keyword/trait-dyn-in-qualified-path.stderr b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path.stderr new file mode 100644 index 0000000000000..c029ba8d5c377 --- /dev/null +++ b/tests/ui/dyn-keyword/trait-dyn-in-qualified-path.stderr @@ -0,0 +1,34 @@ +error[E0038]: the trait `Trait` is not dyn compatible + --> $DIR/trait-dyn-in-qualified-path.rs:5:6 + | +LL | ::function(); + | ^^^^^^^^^ `Trait` is not dyn compatible + | +note: for a trait to be dyn compatible it needs to allow building a vtable + for more information, visit + --> $DIR/trait-dyn-in-qualified-path.rs:1:14 + | +LL | trait Trait: Sized { + | ----- ^^^^^ ...because it requires `Self: Sized` + | | + | this trait is not dyn compatible... + +error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time + --> $DIR/trait-dyn-in-qualified-path.rs:5:6 + | +LL | ::function(); + | ^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `dyn Trait` +note: required by a bound in `Trait::function` + --> $DIR/trait-dyn-in-qualified-path.rs:1:14 + | +LL | trait Trait: Sized { + | ^^^^^ required by this bound in `Trait::function` +LL | fn function() {} + | -------- required by a bound in this associated function + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0038, E0277. +For more information about an error, try `rustc --explain E0038`. diff --git a/tests/ui/dyn-keyword/trait-missing-dyn-in-qualified-path.edition2018.stderr b/tests/ui/dyn-keyword/trait-missing-dyn-in-qualified-path.edition2018.stderr new file mode 100644 index 0000000000000..506cf07b50615 --- /dev/null +++ b/tests/ui/dyn-keyword/trait-missing-dyn-in-qualified-path.edition2018.stderr @@ -0,0 +1,69 @@ +warning: trait objects without an explicit `dyn` are deprecated + --> $DIR/trait-missing-dyn-in-qualified-path.rs:5:19 + | +LL | let x: u32 = ::default(); + | ^^^^^^^ + | + = warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021! + = note: for more information, see + = note: `#[warn(bare_trait_objects)]` on by default +help: if this is a dyn-compatible trait, use `dyn` + | +LL | let x: u32 = ::default(); + | +++ + +error[E0038]: the trait `Default` is not dyn compatible + --> $DIR/trait-missing-dyn-in-qualified-path.rs:5:19 + | +LL | let x: u32 = ::default(); + | ^^^^^^^ `Default` is not dyn compatible + | + = note: the trait is not dyn compatible because it requires `Self: Sized` + = note: for a trait to be dyn compatible it needs to allow building a vtable + for more information, visit + +error[E0277]: the size for values of type `dyn Default` cannot be known at compilation time + --> $DIR/trait-missing-dyn-in-qualified-path.rs:5:19 + | +LL | let x: u32 = ::default(); + | ^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `dyn Default` +note: required by a bound in `default` + --> $SRC_DIR/core/src/default.rs:LL:COL + +error[E0308]: mismatched types + --> $DIR/trait-missing-dyn-in-qualified-path.rs:5:18 + | +LL | let x: u32 = ::default(); + | --- ^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `dyn Default` + | | + | expected due to this + | + = note: expected type `u32` + found trait object `dyn Default` + = help: `u32` implements `Default` so you could change the expected type to `Box` + +error[E0038]: the trait `Default` is not dyn compatible + --> $DIR/trait-missing-dyn-in-qualified-path.rs:5:18 + | +LL | let x: u32 = ::default(); + | ^^^^^^^^^^^^^^^^^^^^ `Default` is not dyn compatible + | + = note: the trait is not dyn compatible because it requires `Self: Sized` + = note: for a trait to be dyn compatible it needs to allow building a vtable + for more information, visit + +error[E0277]: the size for values of type `dyn Default` cannot be known at compilation time + --> $DIR/trait-missing-dyn-in-qualified-path.rs:5:18 + | +LL | let x: u32 = ::default(); + | ^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time + | + = help: the trait `Sized` is not implemented for `dyn Default` + = note: the return type of a function must have a statically known size + +error: aborting due to 5 previous errors; 1 warning emitted + +Some errors have detailed explanations: E0038, E0277, E0308. +For more information about an error, try `rustc --explain E0038`. diff --git a/tests/ui/dyn-keyword/trait-missing-dyn-in-qualified-path.edition2021.stderr b/tests/ui/dyn-keyword/trait-missing-dyn-in-qualified-path.edition2021.stderr new file mode 100644 index 0000000000000..45e4f27551cbd --- /dev/null +++ b/tests/ui/dyn-keyword/trait-missing-dyn-in-qualified-path.edition2021.stderr @@ -0,0 +1,14 @@ +error[E0782]: expected a type, found a trait + --> $DIR/trait-missing-dyn-in-qualified-path.rs:5:19 + | +LL | let x: u32 = ::default(); + | ^^^^^^^ + | +help: you can add the `dyn` keyword if you want a trait object + | +LL | let x: u32 = ::default(); + | +++ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0782`. diff --git a/tests/ui/dyn-keyword/trait-missing-dyn-in-qualified-path.rs b/tests/ui/dyn-keyword/trait-missing-dyn-in-qualified-path.rs new file mode 100644 index 0000000000000..50f73531c51d9 --- /dev/null +++ b/tests/ui/dyn-keyword/trait-missing-dyn-in-qualified-path.rs @@ -0,0 +1,14 @@ +//@revisions: edition2018 edition2021 +//@[edition2018] edition:2018 +//@[edition2021] edition:2021 +fn main() { + let x: u32 = ::default(); + //[edition2021]~^ ERROR expected a type, found a trait + //[edition2018]~^^ WARNING trait objects without an explicit `dyn` are deprecated + //[edition2018]~| WARNING this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021! + //[edition2018]~| ERROR trait `Default` is not dyn compatible + //[edition2018]~| ERROR trait `Default` is not dyn compatible + //[edition2018]~| ERROR the size for values of type `dyn Default` cannot be known at compilation time + //[edition2018]~| ERROR mismatched types + //[edition2018]~| ERROR the size for values of type `dyn Default` cannot be known at compilation time +}