Skip to content

Commit c4371a7

Browse files
authored
Rollup merge of rust-lang#120530 - trevyn:issue-116434, r=compiler-errors
Be less confident when `dyn` suggestion is not checked for object safety rust-lang#120275 no longer checks bare traits for object safety when making a `dyn` suggestion on Rust < 2021. In this case, qualify the suggestion with a note that the trait must be object safe, to prevent user confusion as seen in rust-lang#116434 r? ```@fmease```
2 parents cc54612 + 29fd82b commit c4371a7

27 files changed

+189
-42
lines changed

compiler/rustc_hir_analysis/src/astconv/lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
243243
tcx.node_span_lint(BARE_TRAIT_OBJECTS, self_ty.hir_id, self_ty.span, msg, |lint| {
244244
if self_ty.span.can_be_used_for_suggestions() {
245245
lint.multipart_suggestion_verbose(
246-
"use `dyn`",
246+
"if this is an object-safe trait, use `dyn`",
247247
sugg,
248248
Applicability::MachineApplicable,
249249
);

tests/ui/did_you_mean/bad-assoc-ty.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ LL | type H = Fn(u8) -> (u8)::Output;
182182
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
183183
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
184184
= note: `#[warn(bare_trait_objects)]` on by default
185-
help: use `dyn`
185+
help: if this is an object-safe trait, use `dyn`
186186
|
187187
LL | type H = <dyn Fn(u8) -> (u8)>::Output;
188188
| ++++ +

tests/ui/dyn-keyword/dyn-2018-edition-lint.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ note: the lint level is defined here
1111
|
1212
LL | #[deny(bare_trait_objects)]
1313
| ^^^^^^^^^^^^^^^^^^
14-
help: use `dyn`
14+
help: if this is an object-safe trait, use `dyn`
1515
|
1616
LL | fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
1717
| +++
@@ -24,7 +24,7 @@ LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
2424
|
2525
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
2626
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
27-
help: use `dyn`
27+
help: if this is an object-safe trait, use `dyn`
2828
|
2929
LL | fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
3030
| +++
@@ -37,7 +37,7 @@ LL | let _x: &SomeTrait = todo!();
3737
|
3838
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
3939
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
40-
help: use `dyn`
40+
help: if this is an object-safe trait, use `dyn`
4141
|
4242
LL | let _x: &dyn SomeTrait = todo!();
4343
| +++

tests/ui/dyn-keyword/dyn-angle-brackets.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ note: the lint level is defined here
1111
|
1212
LL | #![deny(bare_trait_objects)]
1313
| ^^^^^^^^^^^^^^^^^^
14-
help: use `dyn`
14+
help: if this is an object-safe trait, use `dyn`
1515
|
1616
LL | <dyn fmt::Debug>::fmt(self, f)
1717
| +++

tests/ui/impl-trait/fresh-lifetime-from-bare-trait-obj-114664.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | fn ice() -> impl AsRef<Fn(&())> {
77
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
99
= note: `#[warn(bare_trait_objects)]` on by default
10-
help: use `dyn`
10+
help: if this is an object-safe trait, use `dyn`
1111
|
1212
LL | fn ice() -> impl AsRef<dyn Fn(&())> {
1313
| +++

tests/ui/issues/issue-28344.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let x: u8 = BitXor::bitor(0 as u8, 0 as u8);
77
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
99
= note: `#[warn(bare_trait_objects)]` on by default
10-
help: use `dyn`
10+
help: if this is an object-safe trait, use `dyn`
1111
|
1212
LL | let x: u8 = <dyn BitXor>::bitor(0 as u8, 0 as u8);
1313
| ++++ +
@@ -35,7 +35,7 @@ LL | let g = BitXor::bitor;
3535
|
3636
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
3737
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
38-
help: use `dyn`
38+
help: if this is an object-safe trait, use `dyn`
3939
|
4040
LL | let g = <dyn BitXor>::bitor;
4141
| ++++ +

tests/ui/issues/issue-58734.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | Trait::nonexistent(());
77
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
99
= note: `#[warn(bare_trait_objects)]` on by default
10-
help: use `dyn`
10+
help: if this is an object-safe trait, use `dyn`
1111
|
1212
LL | <dyn Trait>::nonexistent(());
1313
| ++++ +

tests/ui/issues/issue-86756.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ LL | eq::<dyn, Foo>
2121
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2222
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
2323
= note: `#[warn(bare_trait_objects)]` on by default
24-
help: use `dyn`
24+
help: if this is an object-safe trait, use `dyn`
2525
|
2626
LL | eq::<dyn, dyn Foo>
2727
| +++

tests/ui/lint/bare-trait-objects-path.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let _: Dyn::Ty;
77
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
99
= note: `#[warn(bare_trait_objects)]` on by default
10-
help: use `dyn`
10+
help: if this is an object-safe trait, use `dyn`
1111
|
1212
LL | let _: <dyn Dyn>::Ty;
1313
| ++++ +
@@ -26,7 +26,7 @@ LL | Dyn::func();
2626
|
2727
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2828
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
29-
help: use `dyn`
29+
help: if this is an object-safe trait, use `dyn`
3030
|
3131
LL | <dyn Dyn>::func();
3232
| ++++ +
@@ -39,7 +39,7 @@ LL | ::Dyn::func();
3939
|
4040
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
4141
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
42-
help: use `dyn`
42+
help: if this is an object-safe trait, use `dyn`
4343
|
4444
LL | <dyn (::Dyn)>::func();
4545
| ++++++ ++
@@ -52,7 +52,7 @@ LL | Dyn::CONST;
5252
|
5353
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
5454
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
55-
help: use `dyn`
55+
help: if this is an object-safe trait, use `dyn`
5656
|
5757
LL | <dyn Dyn>::CONST;
5858
| ++++ +

tests/ui/lint/force-warn/allowed-group-warn-by-default-lint.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
77
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
99
= note: requested on the command line with `--force-warn bare-trait-objects`
10-
help: use `dyn`
10+
help: if this is an object-safe trait, use `dyn`
1111
|
1212
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
1313
| +++

tests/ui/lint/force-warn/cap-lints-allow.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
77
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
99
= note: requested on the command line with `--force-warn bare-trait-objects`
10-
help: use `dyn`
10+
help: if this is an object-safe trait, use `dyn`
1111
|
1212
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
1313
| +++

tests/ui/lint/force-warn/lint-group-allowed-cli-warn-by-default-lint.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
99
= note: `--force-warn bare-trait-objects` implied by `--force-warn rust-2018-idioms`
1010
= help: to override `--force-warn rust-2018-idioms` add `#[allow(bare_trait_objects)]`
11-
help: use `dyn`
11+
help: if this is an object-safe trait, use `dyn`
1212
|
1313
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
1414
| +++

tests/ui/lint/force-warn/lint-group-allowed-lint-group.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
99
= note: `--force-warn bare-trait-objects` implied by `--force-warn rust-2018-idioms`
1010
= help: to override `--force-warn rust-2018-idioms` add `#[allow(bare_trait_objects)]`
11-
help: use `dyn`
11+
help: if this is an object-safe trait, use `dyn`
1212
|
1313
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
1414
| +++

tests/ui/lint/force-warn/lint-group-allowed-warn-by-default-lint.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
99
= note: `--force-warn bare-trait-objects` implied by `--force-warn rust-2018-idioms`
1010
= help: to override `--force-warn rust-2018-idioms` add `#[allow(bare_trait_objects)]`
11-
help: use `dyn`
11+
help: if this is an object-safe trait, use `dyn`
1212
|
1313
LL | pub fn function(_x: Box<dyn SomeTrait>) {}
1414
| +++

tests/ui/object-safety/avoid-ice-on-warning-2.old.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | fn id<F>(f: Copy) -> usize {
77
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
99
= note: `#[warn(bare_trait_objects)]` on by default
10-
help: use `dyn`
10+
help: if this is an object-safe trait, use `dyn`
1111
|
1212
LL | fn id<F>(f: dyn Copy) -> usize {
1313
| +++
@@ -21,7 +21,7 @@ LL | fn id<F>(f: Copy) -> usize {
2121
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2222
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
2323
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
24-
help: use `dyn`
24+
help: if this is an object-safe trait, use `dyn`
2525
|
2626
LL | fn id<F>(f: dyn Copy) -> usize {
2727
| +++

tests/ui/object-safety/avoid-ice-on-warning-3.old.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | trait B { fn f(a: A) -> A; }
77
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
88
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
99
= note: `#[warn(bare_trait_objects)]` on by default
10-
help: use `dyn`
10+
help: if this is an object-safe trait, use `dyn`
1111
|
1212
LL | trait B { fn f(a: dyn A) -> A; }
1313
| +++
@@ -20,7 +20,7 @@ LL | trait B { fn f(a: A) -> A; }
2020
|
2121
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2222
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
23-
help: use `dyn`
23+
help: if this is an object-safe trait, use `dyn`
2424
|
2525
LL | trait B { fn f(a: A) -> dyn A; }
2626
| +++
@@ -33,7 +33,7 @@ LL | trait A { fn g(b: B) -> B; }
3333
|
3434
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
3535
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
36-
help: use `dyn`
36+
help: if this is an object-safe trait, use `dyn`
3737
|
3838
LL | trait A { fn g(b: dyn B) -> B; }
3939
| +++
@@ -46,7 +46,7 @@ LL | trait A { fn g(b: B) -> B; }
4646
|
4747
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
4848
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
49-
help: use `dyn`
49+
help: if this is an object-safe trait, use `dyn`
5050
|
5151
LL | trait A { fn g(b: B) -> dyn B; }
5252
| +++
@@ -60,7 +60,7 @@ LL | trait B { fn f(a: A) -> A; }
6060
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
6161
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
6262
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
63-
help: use `dyn`
63+
help: if this is an object-safe trait, use `dyn`
6464
|
6565
LL | trait B { fn f(a: dyn A) -> A; }
6666
| +++
@@ -96,7 +96,7 @@ LL | trait A { fn g(b: B) -> B; }
9696
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
9797
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
9898
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
99-
help: use `dyn`
99+
help: if this is an object-safe trait, use `dyn`
100100
|
101101
LL | trait A { fn g(b: dyn B) -> B; }
102102
| +++

tests/ui/object-safety/avoid-ice-on-warning.old.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ LL | fn call_this<F>(f: F) : Fn(&str) + call_that {}
1919
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2020
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
2121
= note: `#[warn(bare_trait_objects)]` on by default
22-
help: use `dyn`
22+
help: if this is an object-safe trait, use `dyn`
2323
|
2424
LL | fn call_this<F>(f: F) : dyn Fn(&str) + call_that {}
2525
| +++

tests/ui/object-safety/bare-trait-dont-suggest-dyn.old.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ note: the lint level is defined here
1111
|
1212
LL | #![deny(bare_trait_objects)]
1313
| ^^^^^^^^^^^^^^^^^^
14-
help: use `dyn`
14+
help: if this is an object-safe trait, use `dyn`
1515
|
1616
LL | fn ord_prefer_dot(s: String) -> dyn Ord {
1717
| +++

tests/ui/parser/trait-object-trait-parens.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LL | let _: Box<(Obj) + (?Sized) + (for<'a> Trait<'a>)>;
2525
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2626
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
2727
= note: `#[warn(bare_trait_objects)]` on by default
28-
help: use `dyn`
28+
help: if this is an object-safe trait, use `dyn`
2929
|
3030
LL | let _: Box<dyn (Obj) + (?Sized) + (for<'a> Trait<'a>)>;
3131
| +++
@@ -49,7 +49,7 @@ LL | let _: Box<?Sized + (for<'a> Trait<'a>) + (Obj)>;
4949
|
5050
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
5151
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
52-
help: use `dyn`
52+
help: if this is an object-safe trait, use `dyn`
5353
|
5454
LL | let _: Box<dyn ?Sized + (for<'a> Trait<'a>) + (Obj)>;
5555
| +++
@@ -73,7 +73,7 @@ LL | let _: Box<for<'a> Trait<'a> + (Obj) + (?Sized)>;
7373
|
7474
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
7575
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
76-
help: use `dyn`
76+
help: if this is an object-safe trait, use `dyn`
7777
|
7878
LL | let _: Box<dyn for<'a> Trait<'a> + (Obj) + (?Sized)>;
7979
| +++
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
trait Foo {
2+
type Clone;
3+
fn foo() -> Clone;
4+
//~^ WARNING trait objects without an explicit `dyn` are deprecated [bare_trait_objects]
5+
//~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
6+
//~| WARNING trait objects without an explicit `dyn` are deprecated [bare_trait_objects]
7+
//~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
8+
//~| ERROR the trait `Clone` cannot be made into an object [E0038]
9+
}
10+
11+
trait DbHandle: Sized {}
12+
13+
trait DbInterface {
14+
type DbHandle;
15+
fn handle() -> DbHandle;
16+
//~^ WARNING trait objects without an explicit `dyn` are deprecated [bare_trait_objects]
17+
//~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
18+
//~| WARNING trait objects without an explicit `dyn` are deprecated [bare_trait_objects]
19+
//~| WARNING this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
20+
//~| ERROR the trait `DbHandle` cannot be made into an object [E0038]
21+
}
22+
23+
fn main() {}

0 commit comments

Comments
 (0)