From 29fd82b24e8990ab133cb07d7c670202a197743e Mon Sep 17 00:00:00 2001 From: trevyn <230691+trevyn@users.noreply.github.com> Date: Sat, 20 Jan 2024 12:41:56 +0400 Subject: [PATCH] Be less confident when `dyn` suggestion is not checked for object safety --- .../rustc_hir_analysis/src/astconv/lint.rs | 2 +- tests/ui/did_you_mean/bad-assoc-ty.stderr | 2 +- .../dyn-keyword/dyn-2018-edition-lint.stderr | 6 +- .../ui/dyn-keyword/dyn-angle-brackets.stderr | 2 +- ...lifetime-from-bare-trait-obj-114664.stderr | 2 +- tests/ui/issues/issue-28344.stderr | 4 +- tests/ui/issues/issue-58734.stderr | 2 +- tests/ui/issues/issue-86756.stderr | 2 +- tests/ui/lint/bare-trait-objects-path.stderr | 8 +- .../allowed-group-warn-by-default-lint.stderr | 2 +- .../ui/lint/force-warn/cap-lints-allow.stderr | 2 +- ...up-allowed-cli-warn-by-default-lint.stderr | 2 +- .../lint-group-allowed-lint-group.stderr | 2 +- ...-group-allowed-warn-by-default-lint.stderr | 2 +- .../avoid-ice-on-warning-2.old.stderr | 4 +- .../avoid-ice-on-warning-3.old.stderr | 12 +-- .../avoid-ice-on-warning.old.stderr | 2 +- .../bare-trait-dont-suggest-dyn.old.stderr | 2 +- .../parser/trait-object-trait-parens.stderr | 6 +- tests/ui/suggestions/issue-116434-2015.rs | 23 ++++++ tests/ui/suggestions/issue-116434-2015.stderr | 81 +++++++++++++++++++ tests/ui/suggestions/issue-116434-2021.rs | 17 ++++ tests/ui/suggestions/issue-116434-2021.stderr | 26 ++++++ tests/ui/suggestions/issue-61963.stderr | 4 +- .../suggest-swapping-self-ty-and-trait.stderr | 2 +- .../ui/traits/bound/not-on-bare-trait.stderr | 2 +- .../unspecified-self-in-trait-ref.stderr | 10 +-- 27 files changed, 189 insertions(+), 42 deletions(-) create mode 100644 tests/ui/suggestions/issue-116434-2015.rs create mode 100644 tests/ui/suggestions/issue-116434-2015.stderr create mode 100644 tests/ui/suggestions/issue-116434-2021.rs create mode 100644 tests/ui/suggestions/issue-116434-2021.stderr diff --git a/compiler/rustc_hir_analysis/src/astconv/lint.rs b/compiler/rustc_hir_analysis/src/astconv/lint.rs index a6ac8ecd950ea..cee7c84adb2e9 100644 --- a/compiler/rustc_hir_analysis/src/astconv/lint.rs +++ b/compiler/rustc_hir_analysis/src/astconv/lint.rs @@ -243,7 +243,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o { tcx.node_span_lint(BARE_TRAIT_OBJECTS, self_ty.hir_id, self_ty.span, msg, |lint| { if self_ty.span.can_be_used_for_suggestions() { lint.multipart_suggestion_verbose( - "use `dyn`", + "if this is an object-safe trait, use `dyn`", sugg, Applicability::MachineApplicable, ); diff --git a/tests/ui/did_you_mean/bad-assoc-ty.stderr b/tests/ui/did_you_mean/bad-assoc-ty.stderr index eed01267224d3..d5754bdc66432 100644 --- a/tests/ui/did_you_mean/bad-assoc-ty.stderr +++ b/tests/ui/did_you_mean/bad-assoc-ty.stderr @@ -182,7 +182,7 @@ LL | type H = Fn(u8) -> (u8)::Output; = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | type H = (u8)>::Output; | ++++ + diff --git a/tests/ui/dyn-keyword/dyn-2018-edition-lint.stderr b/tests/ui/dyn-keyword/dyn-2018-edition-lint.stderr index 65d44604dc9fc..711bfa188ecd8 100644 --- a/tests/ui/dyn-keyword/dyn-2018-edition-lint.stderr +++ b/tests/ui/dyn-keyword/dyn-2018-edition-lint.stderr @@ -11,7 +11,7 @@ note: the lint level is defined here | LL | #[deny(bare_trait_objects)] | ^^^^^^^^^^^^^^^^^^ -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | fn function(x: &dyn SomeTrait, y: Box) { | +++ @@ -24,7 +24,7 @@ LL | fn function(x: &SomeTrait, y: Box) { | = warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | fn function(x: &SomeTrait, y: Box) { | +++ @@ -37,7 +37,7 @@ LL | let _x: &SomeTrait = todo!(); | = warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | let _x: &dyn SomeTrait = todo!(); | +++ diff --git a/tests/ui/dyn-keyword/dyn-angle-brackets.stderr b/tests/ui/dyn-keyword/dyn-angle-brackets.stderr index 0b194cb8364c6..41298cc73c818 100644 --- a/tests/ui/dyn-keyword/dyn-angle-brackets.stderr +++ b/tests/ui/dyn-keyword/dyn-angle-brackets.stderr @@ -11,7 +11,7 @@ note: the lint level is defined here | LL | #![deny(bare_trait_objects)] | ^^^^^^^^^^^^^^^^^^ -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | ::fmt(self, f) | +++ diff --git a/tests/ui/impl-trait/fresh-lifetime-from-bare-trait-obj-114664.stderr b/tests/ui/impl-trait/fresh-lifetime-from-bare-trait-obj-114664.stderr index 84aaedf183815..3cb3af89bfc25 100644 --- a/tests/ui/impl-trait/fresh-lifetime-from-bare-trait-obj-114664.stderr +++ b/tests/ui/impl-trait/fresh-lifetime-from-bare-trait-obj-114664.stderr @@ -7,7 +7,7 @@ LL | fn ice() -> impl AsRef { = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | fn ice() -> impl AsRef { | +++ diff --git a/tests/ui/issues/issue-28344.stderr b/tests/ui/issues/issue-28344.stderr index 71d642109ac8c..8b427b692a79c 100644 --- a/tests/ui/issues/issue-28344.stderr +++ b/tests/ui/issues/issue-28344.stderr @@ -7,7 +7,7 @@ LL | let x: u8 = BitXor::bitor(0 as u8, 0 as u8); = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | let x: u8 = ::bitor(0 as u8, 0 as u8); | ++++ + @@ -35,7 +35,7 @@ LL | let g = BitXor::bitor; | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | let g = ::bitor; | ++++ + diff --git a/tests/ui/issues/issue-58734.stderr b/tests/ui/issues/issue-58734.stderr index 5ae1ec7cac8f8..71581e96844ee 100644 --- a/tests/ui/issues/issue-58734.stderr +++ b/tests/ui/issues/issue-58734.stderr @@ -7,7 +7,7 @@ LL | Trait::nonexistent(()); = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | ::nonexistent(()); | ++++ + diff --git a/tests/ui/issues/issue-86756.stderr b/tests/ui/issues/issue-86756.stderr index bfa7459ab4a39..d0906a6fa74f4 100644 --- a/tests/ui/issues/issue-86756.stderr +++ b/tests/ui/issues/issue-86756.stderr @@ -21,7 +21,7 @@ LL | eq:: = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | eq:: | +++ diff --git a/tests/ui/lint/bare-trait-objects-path.stderr b/tests/ui/lint/bare-trait-objects-path.stderr index c5d72707f80e8..da1d9f248a01f 100644 --- a/tests/ui/lint/bare-trait-objects-path.stderr +++ b/tests/ui/lint/bare-trait-objects-path.stderr @@ -7,7 +7,7 @@ LL | let _: Dyn::Ty; = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | let _: ::Ty; | ++++ + @@ -26,7 +26,7 @@ LL | Dyn::func(); | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | ::func(); | ++++ + @@ -39,7 +39,7 @@ LL | ::Dyn::func(); | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | ::func(); | ++++++ ++ @@ -52,7 +52,7 @@ LL | Dyn::CONST; | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | ::CONST; | ++++ + diff --git a/tests/ui/lint/force-warn/allowed-group-warn-by-default-lint.stderr b/tests/ui/lint/force-warn/allowed-group-warn-by-default-lint.stderr index e9b7b248e612f..388dc6160cb95 100644 --- a/tests/ui/lint/force-warn/allowed-group-warn-by-default-lint.stderr +++ b/tests/ui/lint/force-warn/allowed-group-warn-by-default-lint.stderr @@ -7,7 +7,7 @@ LL | pub fn function(_x: Box) {} = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: requested on the command line with `--force-warn bare-trait-objects` -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | pub fn function(_x: Box) {} | +++ diff --git a/tests/ui/lint/force-warn/cap-lints-allow.stderr b/tests/ui/lint/force-warn/cap-lints-allow.stderr index e569b2f9f1ae9..a037fb671af29 100644 --- a/tests/ui/lint/force-warn/cap-lints-allow.stderr +++ b/tests/ui/lint/force-warn/cap-lints-allow.stderr @@ -7,7 +7,7 @@ LL | pub fn function(_x: Box) {} = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: requested on the command line with `--force-warn bare-trait-objects` -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | pub fn function(_x: Box) {} | +++ diff --git a/tests/ui/lint/force-warn/lint-group-allowed-cli-warn-by-default-lint.stderr b/tests/ui/lint/force-warn/lint-group-allowed-cli-warn-by-default-lint.stderr index c971e4d0d4d21..a74cda2239f50 100644 --- a/tests/ui/lint/force-warn/lint-group-allowed-cli-warn-by-default-lint.stderr +++ b/tests/ui/lint/force-warn/lint-group-allowed-cli-warn-by-default-lint.stderr @@ -8,7 +8,7 @@ LL | pub fn function(_x: Box) {} = note: for more information, see = note: `--force-warn bare-trait-objects` implied by `--force-warn rust-2018-idioms` = help: to override `--force-warn rust-2018-idioms` add `#[allow(bare_trait_objects)]` -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | pub fn function(_x: Box) {} | +++ diff --git a/tests/ui/lint/force-warn/lint-group-allowed-lint-group.stderr b/tests/ui/lint/force-warn/lint-group-allowed-lint-group.stderr index 97b8694984dd7..c9472a3b9b9d3 100644 --- a/tests/ui/lint/force-warn/lint-group-allowed-lint-group.stderr +++ b/tests/ui/lint/force-warn/lint-group-allowed-lint-group.stderr @@ -8,7 +8,7 @@ LL | pub fn function(_x: Box) {} = note: for more information, see = note: `--force-warn bare-trait-objects` implied by `--force-warn rust-2018-idioms` = help: to override `--force-warn rust-2018-idioms` add `#[allow(bare_trait_objects)]` -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | pub fn function(_x: Box) {} | +++ diff --git a/tests/ui/lint/force-warn/lint-group-allowed-warn-by-default-lint.stderr b/tests/ui/lint/force-warn/lint-group-allowed-warn-by-default-lint.stderr index cd030cc1fcd18..558d5cbb53156 100644 --- a/tests/ui/lint/force-warn/lint-group-allowed-warn-by-default-lint.stderr +++ b/tests/ui/lint/force-warn/lint-group-allowed-warn-by-default-lint.stderr @@ -8,7 +8,7 @@ LL | pub fn function(_x: Box) {} = note: for more information, see = note: `--force-warn bare-trait-objects` implied by `--force-warn rust-2018-idioms` = help: to override `--force-warn rust-2018-idioms` add `#[allow(bare_trait_objects)]` -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | pub fn function(_x: Box) {} | +++ diff --git a/tests/ui/object-safety/avoid-ice-on-warning-2.old.stderr b/tests/ui/object-safety/avoid-ice-on-warning-2.old.stderr index 41c09b7df6289..70e7ea535284d 100644 --- a/tests/ui/object-safety/avoid-ice-on-warning-2.old.stderr +++ b/tests/ui/object-safety/avoid-ice-on-warning-2.old.stderr @@ -7,7 +7,7 @@ LL | fn id(f: Copy) -> usize { = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | fn id(f: dyn Copy) -> usize { | +++ @@ -21,7 +21,7 @@ LL | fn id(f: Copy) -> usize { = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | fn id(f: dyn Copy) -> usize { | +++ diff --git a/tests/ui/object-safety/avoid-ice-on-warning-3.old.stderr b/tests/ui/object-safety/avoid-ice-on-warning-3.old.stderr index a36e2519c804d..f499e2d946ffe 100644 --- a/tests/ui/object-safety/avoid-ice-on-warning-3.old.stderr +++ b/tests/ui/object-safety/avoid-ice-on-warning-3.old.stderr @@ -7,7 +7,7 @@ LL | trait B { fn f(a: A) -> A; } = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | trait B { fn f(a: dyn A) -> A; } | +++ @@ -20,7 +20,7 @@ LL | trait B { fn f(a: A) -> A; } | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | trait B { fn f(a: A) -> dyn A; } | +++ @@ -33,7 +33,7 @@ LL | trait A { fn g(b: B) -> B; } | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | trait A { fn g(b: dyn B) -> B; } | +++ @@ -46,7 +46,7 @@ LL | trait A { fn g(b: B) -> B; } | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | trait A { fn g(b: B) -> dyn B; } | +++ @@ -60,7 +60,7 @@ LL | trait B { fn f(a: A) -> A; } = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | trait B { fn f(a: dyn A) -> A; } | +++ @@ -96,7 +96,7 @@ LL | trait A { fn g(b: B) -> B; } = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | trait A { fn g(b: dyn B) -> B; } | +++ diff --git a/tests/ui/object-safety/avoid-ice-on-warning.old.stderr b/tests/ui/object-safety/avoid-ice-on-warning.old.stderr index 7c7af9682800f..3939c06eabe5b 100644 --- a/tests/ui/object-safety/avoid-ice-on-warning.old.stderr +++ b/tests/ui/object-safety/avoid-ice-on-warning.old.stderr @@ -19,7 +19,7 @@ LL | fn call_this(f: F) : Fn(&str) + call_that {} = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | fn call_this(f: F) : dyn Fn(&str) + call_that {} | +++ diff --git a/tests/ui/object-safety/bare-trait-dont-suggest-dyn.old.stderr b/tests/ui/object-safety/bare-trait-dont-suggest-dyn.old.stderr index 274d5a639a43b..f795e910d2152 100644 --- a/tests/ui/object-safety/bare-trait-dont-suggest-dyn.old.stderr +++ b/tests/ui/object-safety/bare-trait-dont-suggest-dyn.old.stderr @@ -11,7 +11,7 @@ note: the lint level is defined here | LL | #![deny(bare_trait_objects)] | ^^^^^^^^^^^^^^^^^^ -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | fn ord_prefer_dot(s: String) -> dyn Ord { | +++ diff --git a/tests/ui/parser/trait-object-trait-parens.stderr b/tests/ui/parser/trait-object-trait-parens.stderr index 5e07a3fe6c740..3134746b930ac 100644 --- a/tests/ui/parser/trait-object-trait-parens.stderr +++ b/tests/ui/parser/trait-object-trait-parens.stderr @@ -25,7 +25,7 @@ LL | let _: Box<(Obj) + (?Sized) + (for<'a> Trait<'a>)>; = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | let _: Box Trait<'a>)>; | +++ @@ -49,7 +49,7 @@ LL | let _: Box Trait<'a>) + (Obj)>; | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | let _: Box Trait<'a>) + (Obj)>; | +++ @@ -73,7 +73,7 @@ LL | let _: Box Trait<'a> + (Obj) + (?Sized)>; | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | let _: Box Trait<'a> + (Obj) + (?Sized)>; | +++ diff --git a/tests/ui/suggestions/issue-116434-2015.rs b/tests/ui/suggestions/issue-116434-2015.rs new file mode 100644 index 0000000000000..614fc27b77187 --- /dev/null +++ b/tests/ui/suggestions/issue-116434-2015.rs @@ -0,0 +1,23 @@ +trait Foo { + type Clone; + fn foo() -> Clone; + //~^ WARNING trait objects without an explicit `dyn` are deprecated [bare_trait_objects] + //~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! + //~| WARNING trait objects without an explicit `dyn` are deprecated [bare_trait_objects] + //~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! + //~| ERROR the trait `Clone` cannot be made into an object [E0038] +} + +trait DbHandle: Sized {} + +trait DbInterface { + type DbHandle; + fn handle() -> DbHandle; + //~^ WARNING trait objects without an explicit `dyn` are deprecated [bare_trait_objects] + //~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! + //~| WARNING trait objects without an explicit `dyn` are deprecated [bare_trait_objects] + //~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! + //~| ERROR the trait `DbHandle` cannot be made into an object [E0038] +} + +fn main() {} diff --git a/tests/ui/suggestions/issue-116434-2015.stderr b/tests/ui/suggestions/issue-116434-2015.stderr new file mode 100644 index 0000000000000..2d87029b6eb11 --- /dev/null +++ b/tests/ui/suggestions/issue-116434-2015.stderr @@ -0,0 +1,81 @@ +warning: trait objects without an explicit `dyn` are deprecated + --> $DIR/issue-116434-2015.rs:3:17 + | +LL | fn foo() -> Clone; + | ^^^^^ + | + = warning: this is accepted in the current edition (Rust 2015) 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 an object-safe trait, use `dyn` + | +LL | fn foo() -> dyn Clone; + | +++ + +warning: trait objects without an explicit `dyn` are deprecated + --> $DIR/issue-116434-2015.rs:15:20 + | +LL | fn handle() -> DbHandle; + | ^^^^^^^^ + | + = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! + = note: for more information, see +help: if this is an object-safe trait, use `dyn` + | +LL | fn handle() -> dyn DbHandle; + | +++ + +warning: trait objects without an explicit `dyn` are deprecated + --> $DIR/issue-116434-2015.rs:3:17 + | +LL | fn foo() -> Clone; + | ^^^^^ + | + = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! + = note: for more information, see + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` +help: if this is an object-safe trait, use `dyn` + | +LL | fn foo() -> dyn Clone; + | +++ + +error[E0038]: the trait `Clone` cannot be made into an object + --> $DIR/issue-116434-2015.rs:3:17 + | +LL | fn foo() -> Clone; + | ^^^^^ `Clone` cannot be made into an object + | + = note: the trait cannot be made into an object because it requires `Self: Sized` + = note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + +warning: trait objects without an explicit `dyn` are deprecated + --> $DIR/issue-116434-2015.rs:15:20 + | +LL | fn handle() -> DbHandle; + | ^^^^^^^^ + | + = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! + = note: for more information, see + = note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no` +help: if this is an object-safe trait, use `dyn` + | +LL | fn handle() -> dyn DbHandle; + | +++ + +error[E0038]: the trait `DbHandle` cannot be made into an object + --> $DIR/issue-116434-2015.rs:15:20 + | +LL | fn handle() -> DbHandle; + | ^^^^^^^^ `DbHandle` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/issue-116434-2015.rs:11:17 + | +LL | trait DbHandle: Sized {} + | -------- ^^^^^ ...because it requires `Self: Sized` + | | + | this trait cannot be made into an object... + +error: aborting due to 2 previous errors; 4 warnings emitted + +For more information about this error, try `rustc --explain E0038`. diff --git a/tests/ui/suggestions/issue-116434-2021.rs b/tests/ui/suggestions/issue-116434-2021.rs new file mode 100644 index 0000000000000..74c30e0cc1fbe --- /dev/null +++ b/tests/ui/suggestions/issue-116434-2021.rs @@ -0,0 +1,17 @@ +// edition:2021 + +trait Foo { + type Clone; + fn foo() -> Clone; + //~^ ERROR the trait `Clone` cannot be made into an object [E0038] +} + +trait DbHandle: Sized {} + +trait DbInterface { + type DbHandle; + fn handle() -> DbHandle; + //~^ ERROR the trait `DbHandle` cannot be made into an object [E0038] +} + +fn main() {} diff --git a/tests/ui/suggestions/issue-116434-2021.stderr b/tests/ui/suggestions/issue-116434-2021.stderr new file mode 100644 index 0000000000000..43ad82d484a66 --- /dev/null +++ b/tests/ui/suggestions/issue-116434-2021.stderr @@ -0,0 +1,26 @@ +error[E0038]: the trait `Clone` cannot be made into an object + --> $DIR/issue-116434-2021.rs:5:17 + | +LL | fn foo() -> Clone; + | ^^^^^ `Clone` cannot be made into an object + | + = note: the trait cannot be made into an object because it requires `Self: Sized` + = note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + +error[E0038]: the trait `DbHandle` cannot be made into an object + --> $DIR/issue-116434-2021.rs:13:20 + | +LL | fn handle() -> DbHandle; + | ^^^^^^^^ `DbHandle` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit + --> $DIR/issue-116434-2021.rs:9:17 + | +LL | trait DbHandle: Sized {} + | -------- ^^^^^ ...because it requires `Self: Sized` + | | + | this trait 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/tests/ui/suggestions/issue-61963.stderr b/tests/ui/suggestions/issue-61963.stderr index 754d02b1c021b..084b0cbeef292 100644 --- a/tests/ui/suggestions/issue-61963.stderr +++ b/tests/ui/suggestions/issue-61963.stderr @@ -11,7 +11,7 @@ note: the lint level is defined here | LL | #![deny(bare_trait_objects)] | ^^^^^^^^^^^^^^^^^^ -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | bar: Box, | +++ @@ -24,7 +24,7 @@ LL | pub struct Foo { | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | dyn pub struct Foo { | +++ diff --git a/tests/ui/suggestions/suggest-swapping-self-ty-and-trait.stderr b/tests/ui/suggestions/suggest-swapping-self-ty-and-trait.stderr index ffd505fffb48e..0098814f81e78 100644 --- a/tests/ui/suggestions/suggest-swapping-self-ty-and-trait.stderr +++ b/tests/ui/suggestions/suggest-swapping-self-ty-and-trait.stderr @@ -40,7 +40,7 @@ LL | impl<'a, T> Struct for Trait<'a, T> {} = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | impl<'a, T> Struct for dyn Trait<'a, T> {} | +++ diff --git a/tests/ui/traits/bound/not-on-bare-trait.stderr b/tests/ui/traits/bound/not-on-bare-trait.stderr index 6d56851bf3495..f1e7a28654a74 100644 --- a/tests/ui/traits/bound/not-on-bare-trait.stderr +++ b/tests/ui/traits/bound/not-on-bare-trait.stderr @@ -7,7 +7,7 @@ LL | fn foo(_x: Foo + Send) { = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | fn foo(_x: dyn Foo + Send) { | +++ diff --git a/tests/ui/traits/unspecified-self-in-trait-ref.stderr b/tests/ui/traits/unspecified-self-in-trait-ref.stderr index b5e8e88676c61..3614348ceedc7 100644 --- a/tests/ui/traits/unspecified-self-in-trait-ref.stderr +++ b/tests/ui/traits/unspecified-self-in-trait-ref.stderr @@ -7,7 +7,7 @@ LL | let a = Foo::lol(); = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see = note: `#[warn(bare_trait_objects)]` on by default -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | let a = ::lol(); | ++++ + @@ -26,7 +26,7 @@ LL | let b = Foo::<_>::lol(); | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | let b = >::lol(); | ++++ + @@ -45,7 +45,7 @@ LL | let c = Bar::lol(); | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | let c = ::lol(); | ++++ + @@ -64,7 +64,7 @@ LL | let d = Bar::::lol(); | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | let d = >::lol(); | ++++ + @@ -83,7 +83,7 @@ LL | let e = Bar::::lol(); | = warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021! = note: for more information, see -help: use `dyn` +help: if this is an object-safe trait, use `dyn` | LL | let e = >::lol(); | ++++ +