diff --git a/src/test/ui/suggestions/object-unsafe-trait-references-self.rs b/src/test/ui/suggestions/object-unsafe-trait-references-self.rs new file mode 100644 index 000000000000..07bf053e9966 --- /dev/null +++ b/src/test/ui/suggestions/object-unsafe-trait-references-self.rs @@ -0,0 +1,12 @@ +trait Trait { + fn baz(&self, _: Self) {} + fn bat(&self) -> Self {} +} + +fn bar(x: &dyn Trait) {} //~ ERROR the trait `Trait` cannot be made into an object + +trait Other: Sized {} + +fn foo(x: &dyn Other) {} //~ ERROR the trait `Other` cannot be made into an object + +fn main() {} diff --git a/src/test/ui/suggestions/object-unsafe-trait-references-self.stderr b/src/test/ui/suggestions/object-unsafe-trait-references-self.stderr new file mode 100644 index 000000000000..c3cfad70bf43 --- /dev/null +++ b/src/test/ui/suggestions/object-unsafe-trait-references-self.stderr @@ -0,0 +1,30 @@ +error[E0038]: the trait `Trait` cannot be made into an object + --> $DIR/object-unsafe-trait-references-self.rs:6:11 + | +LL | trait Trait { + | ----- this trait cannot be made into an object... +LL | fn baz(&self, _: Self) {} + | ---- ...because method `baz` references the `Self` type in this parameter +LL | fn bat(&self) -> Self {} + | ---- ...because method `bat` references the `Self` type in its return type +... +LL | fn bar(x: &dyn Trait) {} + | ^^^^^^^^^^ the trait `Trait` cannot be made into an object + | + = help: consider moving `baz` to another trait + = help: consider moving `bat` to another trait + +error[E0038]: the trait `Other` cannot be made into an object + --> $DIR/object-unsafe-trait-references-self.rs:10:11 + | +LL | trait Other: Sized {} + | ----- ----- ...because it requires `Self: Sized` + | | + | this trait cannot be made into an object... +LL | +LL | fn foo(x: &dyn Other) {} + | ^^^^^^^^^^ the trait `Other` cannot be made into an object + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0038`. diff --git a/src/test/ui/suggestions/object-unsafe-trait-should-use-where-sized.fixed b/src/test/ui/suggestions/object-unsafe-trait-should-use-where-sized.fixed new file mode 100644 index 000000000000..c4b8960b65e4 --- /dev/null +++ b/src/test/ui/suggestions/object-unsafe-trait-should-use-where-sized.fixed @@ -0,0 +1,13 @@ +// run-rustfix +#![allow(unused_variables, dead_code)] + +trait Trait { + fn foo() where Self: Other, Self: Sized, { } + fn bar(self: &Self) {} //~ ERROR invalid `self` parameter type +} + +fn bar(x: &dyn Trait) {} //~ ERROR the trait `Trait` cannot be made into an object + +trait Other {} + +fn main() {} diff --git a/src/test/ui/suggestions/object-unsafe-trait-should-use-where-sized.rs b/src/test/ui/suggestions/object-unsafe-trait-should-use-where-sized.rs new file mode 100644 index 000000000000..38d9aea16ebf --- /dev/null +++ b/src/test/ui/suggestions/object-unsafe-trait-should-use-where-sized.rs @@ -0,0 +1,13 @@ +// run-rustfix +#![allow(unused_variables, dead_code)] + +trait Trait { + fn foo() where Self: Other, { } + fn bar(self: ()) {} //~ ERROR invalid `self` parameter type +} + +fn bar(x: &dyn Trait) {} //~ ERROR the trait `Trait` cannot be made into an object + +trait Other {} + +fn main() {} diff --git a/src/test/ui/suggestions/object-unsafe-trait-should-use-where-sized.stderr b/src/test/ui/suggestions/object-unsafe-trait-should-use-where-sized.stderr new file mode 100644 index 000000000000..6466a768ecbe --- /dev/null +++ b/src/test/ui/suggestions/object-unsafe-trait-should-use-where-sized.stderr @@ -0,0 +1,35 @@ +error[E0038]: the trait `Trait` cannot be made into an object + --> $DIR/object-unsafe-trait-should-use-where-sized.rs:9:11 + | +LL | trait Trait { + | ----- this trait cannot be made into an object... +LL | fn foo() where Self: Other, { } + | --- ...because associated function `foo` has no `self` parameter +LL | fn bar(self: ()) {} + | -- ...because method `bar`'s `self` parameter cannot be dispatched on +... +LL | fn bar(x: &dyn Trait) {} + | ^^^^^^^^^^ the trait `Trait` cannot be made into an object + | +help: consider turning `foo` into a method by giving it a `&self` argument or constraining it so it does not apply to trait objects + | +LL | fn foo() where Self: Other, Self: Sized, { } + | ^^^^^^^^^^^^^ +help: consider changing method `bar`'s `self` parameter to be `&self` + | +LL | fn bar(self: &Self) {} + | ^^^^^ + +error[E0307]: invalid `self` parameter type: () + --> $DIR/object-unsafe-trait-should-use-where-sized.rs:6:18 + | +LL | fn bar(self: ()) {} + | ^^ + | + = note: type of `self` must be `Self` or a type that dereferences to it + = help: consider changing to `self`, `&self`, `&mut self`, `self: Box`, `self: Rc`, `self: Arc`, or `self: Pin

` (where P is one of the previous types except `Self`) + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0038, E0307. +For more information about an error, try `rustc --explain E0038`.