Skip to content

Add tests for dyn-incompatible cases #136210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
trait Trait {
fn function() {}
}

impl Trait for () {}

fn main() {
<dyn Trait>::function(); //~ ERROR the trait `Trait` is not dyn compatible
}
Original file line number Diff line number Diff line change
@@ -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 | <dyn Trait>::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 <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $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`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ run-pass

trait Trait {
fn function(&self) {}
}

impl dyn Trait {
}

impl Trait for () {}

fn main() {
Trait::function(&());
}
Original file line number Diff line number Diff line change
@@ -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() {
<dyn Trait>::function(&());
//~^ ERROR the trait `Trait` is not dyn compatible
//~| ERROR the trait `Trait` is not dyn compatible
}
Original file line number Diff line number Diff line change
@@ -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 <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $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 <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $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 | <dyn Trait>::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 <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $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 | <dyn Trait>::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 <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $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`.
11 changes: 11 additions & 0 deletions tests/ui/dyn-keyword/trait-dyn-in-qualified-path-with-collision.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
trait Trait {
fn function(&self) {}
}
impl dyn Trait {
fn function(&self) {}
}
impl Trait for () {}
fn main() {
<dyn Trait>::function(&());
//~^ ERROR multiple applicable items in scope
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0034]: multiple applicable items in scope
--> $DIR/trait-dyn-in-qualified-path-with-collision.rs:9:18
|
LL | <dyn Trait>::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`.
8 changes: 8 additions & 0 deletions tests/ui/dyn-keyword/trait-dyn-in-qualified-path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
trait Trait: Sized {
fn function() {}
}
fn main() {
<dyn Trait>::function();
//~^ ERROR trait `Trait` is not dyn compatible
//~| ERROR the size for values of type `dyn Trait` cannot be known at compilation time
}
34 changes: 34 additions & 0 deletions tests/ui/dyn-keyword/trait-dyn-in-qualified-path.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
error[E0038]: the trait `Trait` is not dyn compatible
--> $DIR/trait-dyn-in-qualified-path.rs:5:6
|
LL | <dyn Trait>::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 <https://doc.rust-lang.org/reference/items/traits.html#object-safety>
--> $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 | <dyn Trait>::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`.
Original file line number Diff line number Diff line change
@@ -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>::default();
| ^^^^^^^
|
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= note: `#[warn(bare_trait_objects)]` on by default
help: if this is a dyn-compatible trait, use `dyn`
|
LL | let x: u32 = <dyn Default>::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();
| ^^^^^^^ `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 <https://doc.rust-lang.org/reference/items/traits.html#object-safety>

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>::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>::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<dyn Default>`

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();
| ^^^^^^^^^^^^^^^^^^^^ `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 <https://doc.rust-lang.org/reference/items/traits.html#object-safety>

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>::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`.
Original file line number Diff line number Diff line change
@@ -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>::default();
| ^^^^^^^
|
help: you can add the `dyn` keyword if you want a trait object
|
LL | let x: u32 = <dyn Default>::default();
| +++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0782`.
14 changes: 14 additions & 0 deletions tests/ui/dyn-keyword/trait-missing-dyn-in-qualified-path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@revisions: edition2018 edition2021
//@[edition2018] edition:2018
//@[edition2021] edition:2021
fn main() {
let x: u32 = <Default>::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
}
Loading