Skip to content

Commit

Permalink
Rollup merge of #120530 - trevyn:issue-116434, r=compiler-errors
Browse files Browse the repository at this point in the history
Be less confident when `dyn` suggestion is not checked for object safety

#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 #116434

r? ```@fmease```
  • Loading branch information
oli-obk committed Feb 14, 2024
2 parents cc54612 + 29fd82b commit c4371a7
Show file tree
Hide file tree
Showing 27 changed files with 189 additions and 42 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/astconv/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/did_you_mean/bad-assoc-ty.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= note: `#[warn(bare_trait_objects)]` on by default
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | type H = <dyn Fn(u8) -> (u8)>::Output;
| ++++ +
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/dyn-keyword/dyn-2018-edition-lint.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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<SomeTrait>) {
| +++
Expand All @@ -24,7 +24,7 @@ LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
|
= 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>
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
| +++
Expand All @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | let _x: &dyn SomeTrait = todo!();
| +++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/dyn-keyword/dyn-angle-brackets.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 | <dyn fmt::Debug>::fmt(self, f)
| +++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | fn ice() -> impl AsRef<Fn(&())> {
= warning: this is accepted in the current edition (Rust 2015) 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: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | fn ice() -> impl AsRef<dyn Fn(&())> {
| +++
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/issues/issue-28344.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= 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 = <dyn BitXor>::bitor(0 as u8, 0 as u8);
| ++++ +
Expand Down Expand Up @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | let g = <dyn BitXor>::bitor;
| ++++ +
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-58734.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= note: `#[warn(bare_trait_objects)]` on by default
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | <dyn Trait>::nonexistent(());
| ++++ +
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-86756.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LL | eq::<dyn, Foo>
= warning: this is accepted in the current edition (Rust 2015) 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: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | eq::<dyn, dyn Foo>
| +++
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/lint/bare-trait-objects-path.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= note: `#[warn(bare_trait_objects)]` on by default
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | let _: <dyn Dyn>::Ty;
| ++++ +
Expand All @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | <dyn Dyn>::func();
| ++++ +
Expand All @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | <dyn (::Dyn)>::func();
| ++++++ ++
Expand All @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | <dyn Dyn>::CONST;
| ++++ +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
= warning: this is accepted in the current edition (Rust 2015) 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: 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<dyn SomeTrait>) {}
| +++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/force-warn/cap-lints-allow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
= warning: this is accepted in the current edition (Rust 2015) 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: 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<dyn SomeTrait>) {}
| +++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= 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<dyn SomeTrait>) {}
| +++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= 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<dyn SomeTrait>) {}
| +++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ LL | pub fn function(_x: Box<SomeTrait>) {}
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= 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<dyn SomeTrait>) {}
| +++
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/object-safety/avoid-ice-on-warning-2.old.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | fn id<F>(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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= 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>(f: dyn Copy) -> usize {
| +++
Expand All @@ -21,7 +21,7 @@ LL | fn id<F>(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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= 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>(f: dyn Copy) -> usize {
| +++
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/object-safety/avoid-ice-on-warning-3.old.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= 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; }
| +++
Expand All @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | trait B { fn f(a: A) -> dyn A; }
| +++
Expand All @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | trait A { fn g(b: dyn B) -> B; }
| +++
Expand All @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | trait A { fn g(b: B) -> dyn B; }
| +++
Expand All @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= 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; }
| +++
Expand Down Expand Up @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= 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; }
| +++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/object-safety/avoid-ice-on-warning.old.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ LL | fn call_this<F>(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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= 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: F) : dyn Fn(&str) + call_that {}
| +++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
| +++
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/parser/trait-object-trait-parens.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
= note: `#[warn(bare_trait_objects)]` on by default
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | let _: Box<dyn (Obj) + (?Sized) + (for<'a> Trait<'a>)>;
| +++
Expand All @@ -49,7 +49,7 @@ LL | let _: Box<?Sized + (for<'a> 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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | let _: Box<dyn ?Sized + (for<'a> Trait<'a>) + (Obj)>;
| +++
Expand All @@ -73,7 +73,7 @@ LL | let _: Box<for<'a> 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 <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
help: use `dyn`
help: if this is an object-safe trait, use `dyn`
|
LL | let _: Box<dyn for<'a> Trait<'a> + (Obj) + (?Sized)>;
| +++
Expand Down
23 changes: 23 additions & 0 deletions tests/ui/suggestions/issue-116434-2015.rs
Original file line number Diff line number Diff line change
@@ -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() {}
Loading

0 comments on commit c4371a7

Please sign in to comment.