Skip to content

Commit

Permalink
Rollup merge of rust-lang#88657 - camelid:fix-dyn-sugg, r=m-ou-se
Browse files Browse the repository at this point in the history
Fix 2021 `dyn` suggestion that used code as label

The arguments to `span_suggestion` were in the wrong order, so the error
looked like this:

    error[E0783]: trait objects without an explicit `dyn` are deprecated
      --> src/test/ui/editions/dyn-trait-sugg-2021.rs:10:5
       |
    10 |     Foo::hi(123);
       |     ^^^ help: <dyn Foo>: `use `dyn``

Now the error looks like this, as expected:

    error[E0783]: trait objects without an explicit `dyn` are deprecated
      --> src/test/ui/editions/dyn-trait-sugg-2021.rs:10:5
       |
    10 |     Foo::hi(123);
       |     ^^^ help: use `dyn`: `<dyn Foo>`

This issue was only present in the 2021 error; the 2018 lint was
correct.

r? `@m-ou-se`
  • Loading branch information
m-ou-se authored Sep 5, 2021
2 parents 5fdc8eb + 486d79f commit b4d06bf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
8 changes: 4 additions & 4 deletions compiler/rustc_typeck/src/check/fn_ctxt/_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
Ok(s) if s.starts_with('<') => sugg,
_ => format!("<{}>", sugg),
};
let replace = String::from("use `dyn`");
let sugg_label = "use `dyn`";
if self.sess().edition() >= Edition::Edition2021 {
let mut err = rustc_errors::struct_span_err!(
self.sess(),
Expand All @@ -956,8 +956,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
);
err.span_suggestion(
self_ty.span,
&sugg,
replace,
sugg_label,
sugg,
Applicability::MachineApplicable,
)
.emit();
Expand All @@ -968,7 +968,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self_ty.span,
|lint| {
let mut db = lint.build(msg);
db.span_suggestion(self_ty.span, &replace, sugg, app);
db.span_suggestion(self_ty.span, sugg_label, sugg, app);
db.emit()
},
);
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/editions/dyn-trait-sugg-2021.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// edition:2021

trait Foo<T> {}

impl<T> dyn Foo<T> {
fn hi(_x: T) {}
}

fn main() {
Foo::hi(123);
//~^ ERROR trait objects without an explicit `dyn` are deprecated
}
9 changes: 9 additions & 0 deletions src/test/ui/editions/dyn-trait-sugg-2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0783]: trait objects without an explicit `dyn` are deprecated
--> $DIR/dyn-trait-sugg-2021.rs:10:5
|
LL | Foo::hi(123);
| ^^^ help: use `dyn`: `<dyn Foo>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0783`.

0 comments on commit b4d06bf

Please sign in to comment.