Skip to content

Commit d3a6d4b

Browse files
committed
Use multipart suggestions.
1 parent 6152b1d commit d3a6d4b

19 files changed

+254
-62
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

+32-19
Original file line numberDiff line numberDiff line change
@@ -2617,40 +2617,53 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
26172617
if let hir::TyKind::TraitObject([poly_trait_ref, ..], _, TraitObjectSyntax::None) =
26182618
self_ty.kind
26192619
{
2620-
let (mut sugg, app) = match tcx.sess.source_map().span_to_snippet(self_ty.span) {
2621-
Ok(s) if poly_trait_ref.trait_ref.path.is_global() => {
2622-
(format!("dyn ({})", s), Applicability::MachineApplicable)
2623-
}
2624-
Ok(s) => (format!("dyn {}", s), Applicability::MachineApplicable),
2625-
Err(_) => ("dyn <type>".to_string(), Applicability::HasPlaceholders),
2626-
};
2627-
if in_path {
2628-
let has_bracket = tcx
2620+
let needs_bracket = in_path
2621+
&& !tcx
26292622
.sess
26302623
.source_map()
26312624
.span_to_prev_source(self_ty.span)
26322625
.ok()
26332626
.map_or(false, |s| s.trim_end().ends_with('<'));
2634-
if !has_bracket {
2635-
sugg = format!("<{}>", sugg);
2636-
}
2637-
}
2627+
2628+
let is_global = poly_trait_ref.trait_ref.path.is_global();
2629+
let sugg = Vec::from_iter([
2630+
(
2631+
self_ty.span.shrink_to_lo(),
2632+
format!(
2633+
"{}dyn {}",
2634+
if needs_bracket { "<" } else { "" },
2635+
if is_global { "(" } else { "" },
2636+
),
2637+
),
2638+
(
2639+
self_ty.span.shrink_to_hi(),
2640+
format!(
2641+
"{}{}",
2642+
if is_global { ")" } else { "" },
2643+
if needs_bracket { ">" } else { "" },
2644+
),
2645+
),
2646+
]);
26382647
if self_ty.span.edition() >= Edition::Edition2021 {
26392648
let msg = "trait objects must include the `dyn` keyword";
26402649
let label = "add `dyn` keyword before this trait";
2641-
let mut err =
2642-
rustc_errors::struct_span_err!(tcx.sess, self_ty.span, E0782, "{}", msg);
2643-
err.span_suggestion_verbose(self_ty.span, label, sugg, app).emit();
2650+
rustc_errors::struct_span_err!(tcx.sess, self_ty.span, E0782, "{}", msg)
2651+
.multipart_suggestion_verbose(label, sugg, Applicability::MachineApplicable)
2652+
.emit();
26442653
} else {
26452654
let msg = "trait objects without an explicit `dyn` are deprecated";
26462655
tcx.struct_span_lint_hir(
26472656
BARE_TRAIT_OBJECTS,
26482657
self_ty.hir_id,
26492658
self_ty.span,
26502659
|lint| {
2651-
let mut db = lint.build(msg);
2652-
db.span_suggestion(self_ty.span, "use `dyn`", sugg, app);
2653-
db.emit()
2660+
lint.build(msg)
2661+
.multipart_suggestion_verbose(
2662+
"use `dyn`",
2663+
sugg,
2664+
Applicability::MachineApplicable,
2665+
)
2666+
.emit()
26542667
},
26552668
);
26562669
}

src/test/ui/did_you_mean/bad-assoc-ty.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,15 @@ warning: trait objects without an explicit `dyn` are deprecated
103103
--> $DIR/bad-assoc-ty.rs:33:10
104104
|
105105
LL | type H = Fn(u8) -> (u8)::Output;
106-
| ^^^^^^^^^^^^^^ help: use `dyn`: `<dyn Fn(u8) -> (u8)>`
106+
| ^^^^^^^^^^^^^^
107107
|
108108
= note: `#[warn(bare_trait_objects)]` on by default
109109
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
110110
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
111+
help: use `dyn`
112+
|
113+
LL | type H = <dyn Fn(u8) -> (u8)>::Output;
114+
| ++++ +
111115

112116
error[E0223]: ambiguous associated type
113117
--> $DIR/bad-assoc-ty.rs:33:10

src/test/ui/dyn-keyword/dyn-2018-edition-lint.stderr

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: trait objects without an explicit `dyn` are deprecated
22
--> $DIR/dyn-2018-edition-lint.rs:4:17
33
|
44
LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
5-
| ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`
5+
| ^^^^^^^^^
66
|
77
note: the lint level is defined here
88
--> $DIR/dyn-2018-edition-lint.rs:2:8
@@ -11,15 +11,25 @@ LL | #[deny(bare_trait_objects)]
1111
| ^^^^^^^^^^^^^^^^^^
1212
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
1313
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
14+
help: use `dyn`
15+
|
16+
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
17+
LL + fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
18+
|
1419

1520
error: trait objects without an explicit `dyn` are deprecated
1621
--> $DIR/dyn-2018-edition-lint.rs:4:35
1722
|
1823
LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
19-
| ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`
24+
| ^^^^^^^^^
2025
|
2126
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
2227
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
28+
help: use `dyn`
29+
|
30+
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
31+
LL + fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
32+
|
2333

2434
error: aborting due to 2 previous errors
2535

src/test/ui/dyn-keyword/dyn-2021-edition-error.stderr

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
66
|
77
help: add `dyn` keyword before this trait
88
|
9-
LL | fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
10-
| ~~~~~~~~~~~~~
9+
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
10+
LL + fn function(x: &dyn SomeTrait, y: Box<SomeTrait>) {
11+
|
1112

1213
error[E0782]: trait objects must include the `dyn` keyword
1314
--> $DIR/dyn-2021-edition-error.rs:3:35
@@ -17,8 +18,9 @@ LL | fn function(x: &SomeTrait, y: Box<SomeTrait>) {
1718
|
1819
help: add `dyn` keyword before this trait
1920
|
20-
LL | fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
21-
| ~~~~~~~~~~~~~
21+
LL - fn function(x: &SomeTrait, y: Box<SomeTrait>) {
22+
LL + fn function(x: &SomeTrait, y: Box<dyn SomeTrait>) {
23+
|
2224

2325
error: aborting due to 2 previous errors
2426

src/test/ui/dyn-keyword/dyn-angle-brackets.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error: trait objects without an explicit `dyn` are deprecated
22
--> $DIR/dyn-angle-brackets.rs:15:10
33
|
44
LL | <fmt::Debug>::fmt(self, f)
5-
| ^^^^^^^^^^ help: use `dyn`: `dyn fmt::Debug`
5+
| ^^^^^^^^^^
66
|
77
note: the lint level is defined here
88
--> $DIR/dyn-angle-brackets.rs:4:9
@@ -11,6 +11,11 @@ LL | #![deny(bare_trait_objects)]
1111
| ^^^^^^^^^^^^^^^^^^
1212
= warning: this is accepted in the current edition (Rust 2018) but is a hard error in Rust 2021!
1313
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
14+
help: use `dyn`
15+
|
16+
LL - <fmt::Debug>::fmt(self, f)
17+
LL + <dyn fmt::Debug>::fmt(self, f)
18+
|
1419

1520
error: aborting due to previous error
1621

src/test/ui/editions/dyn-trait-sugg-2021.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | Foo::hi(123);
77
help: add `dyn` keyword before this trait
88
|
99
LL | <dyn Foo>::hi(123);
10-
| ~~~~~~~~~
10+
| ++++ +
1111

1212
error: aborting due to previous error
1313

src/test/ui/issues/issue-28344.stderr

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ warning: trait objects without an explicit `dyn` are deprecated
22
--> $DIR/issue-28344.rs:4:17
33
|
44
LL | let x: u8 = BitXor::bitor(0 as u8, 0 as u8);
5-
| ^^^^^^ help: use `dyn`: `<dyn BitXor>`
5+
| ^^^^^^
66
|
77
= note: `#[warn(bare_trait_objects)]` on by default
88
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
99
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
10+
help: use `dyn`
11+
|
12+
LL | let x: u8 = <dyn BitXor>::bitor(0 as u8, 0 as u8);
13+
| ++++ +
1014

1115
error[E0191]: the value of the associated type `Output` (from trait `BitXor`) must be specified
1216
--> $DIR/issue-28344.rs:4:17
@@ -27,10 +31,14 @@ warning: trait objects without an explicit `dyn` are deprecated
2731
--> $DIR/issue-28344.rs:10:13
2832
|
2933
LL | let g = BitXor::bitor;
30-
| ^^^^^^ help: use `dyn`: `<dyn BitXor>`
34+
| ^^^^^^
3135
|
3236
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
3337
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
38+
help: use `dyn`
39+
|
40+
LL | let g = <dyn BitXor>::bitor;
41+
| ++++ +
3442

3543
error[E0191]: the value of the associated type `Output` (from trait `BitXor`) must be specified
3644
--> $DIR/issue-28344.rs:10:13

src/test/ui/issues/issue-58734.stderr

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ warning: trait objects without an explicit `dyn` are deprecated
22
--> $DIR/issue-58734.rs:20:5
33
|
44
LL | Trait::nonexistent(());
5-
| ^^^^^ help: use `dyn`: `<dyn Trait>`
5+
| ^^^^^
66
|
77
= note: `#[warn(bare_trait_objects)]` on by default
88
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
99
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
10+
help: use `dyn`
11+
|
12+
LL | <dyn Trait>::nonexistent(());
13+
| ++++ +
1014

1115
error[E0599]: no function or associated item named `nonexistent` found for trait object `dyn Trait` in the current scope
1216
--> $DIR/issue-58734.rs:20:12

src/test/ui/issues/issue-86756.stderr

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,16 @@ warning: trait objects without an explicit `dyn` are deprecated
1818
--> $DIR/issue-86756.rs:5:15
1919
|
2020
LL | eq::<dyn, Foo>
21-
| ^^^ help: use `dyn`: `dyn Foo`
21+
| ^^^
2222
|
2323
= note: `#[warn(bare_trait_objects)]` on by default
2424
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2525
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
26+
help: use `dyn`
27+
|
28+
LL - eq::<dyn, Foo>
29+
LL + eq::<dyn, dyn Foo>
30+
|
2631

2732
error[E0107]: missing generics for trait `Foo`
2833
--> $DIR/issue-86756.rs:5:15

src/test/ui/lint/bare-trait-objects-path.stderr

+20-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ warning: trait objects without an explicit `dyn` are deprecated
22
--> $DIR/bare-trait-objects-path.rs:23:12
33
|
44
LL | let _: Dyn::Ty;
5-
| ^^^ help: use `dyn`: `<dyn Dyn>`
5+
| ^^^
66
|
77
= note: `#[warn(bare_trait_objects)]` on by default
88
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
99
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
10+
help: use `dyn`
11+
|
12+
LL | let _: <dyn Dyn>::Ty;
13+
| ++++ +
1014

1115
error[E0223]: ambiguous associated type
1216
--> $DIR/bare-trait-objects-path.rs:23:12
@@ -18,28 +22,40 @@ warning: trait objects without an explicit `dyn` are deprecated
1822
--> $DIR/bare-trait-objects-path.rs:14:5
1923
|
2024
LL | Dyn::func();
21-
| ^^^ help: use `dyn`: `<dyn Dyn>`
25+
| ^^^
2226
|
2327
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2428
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
29+
help: use `dyn`
30+
|
31+
LL | <dyn Dyn>::func();
32+
| ++++ +
2533

2634
warning: trait objects without an explicit `dyn` are deprecated
2735
--> $DIR/bare-trait-objects-path.rs:17:5
2836
|
2937
LL | ::Dyn::func();
30-
| ^^^^^ help: use `dyn`: `<dyn (::Dyn)>`
38+
| ^^^^^
3139
|
3240
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
3341
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
42+
help: use `dyn`
43+
|
44+
LL | <dyn (::Dyn)>::func();
45+
| ++++++ ++
3446

3547
warning: trait objects without an explicit `dyn` are deprecated
3648
--> $DIR/bare-trait-objects-path.rs:20:5
3749
|
3850
LL | Dyn::CONST;
39-
| ^^^ help: use `dyn`: `<dyn Dyn>`
51+
| ^^^
4052
|
4153
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
4254
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
55+
help: use `dyn`
56+
|
57+
LL | <dyn Dyn>::CONST;
58+
| ++++ +
4359

4460
error: aborting due to previous error; 4 warnings emitted
4561

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

+18-3
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,44 @@ warning: trait objects without an explicit `dyn` are deprecated
22
--> $DIR/allowed-group-warn-by-default-lint.rs:10:25
33
|
44
LL | pub fn function(_x: Box<SomeTrait>) {}
5-
| ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`
5+
| ^^^^^^^^^
66
|
77
= note: requested on the command line with `--force-warn bare-trait-objects`
88
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
99
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
10+
help: use `dyn`
11+
|
12+
LL - pub fn function(_x: Box<SomeTrait>) {}
13+
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
14+
|
1015

1116
warning: trait objects without an explicit `dyn` are deprecated
1217
--> $DIR/allowed-group-warn-by-default-lint.rs:10:25
1318
|
1419
LL | pub fn function(_x: Box<SomeTrait>) {}
15-
| ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`
20+
| ^^^^^^^^^
1621
|
1722
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
1823
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
24+
help: use `dyn`
25+
|
26+
LL - pub fn function(_x: Box<SomeTrait>) {}
27+
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
28+
|
1929

2030
warning: trait objects without an explicit `dyn` are deprecated
2131
--> $DIR/allowed-group-warn-by-default-lint.rs:10:25
2232
|
2333
LL | pub fn function(_x: Box<SomeTrait>) {}
24-
| ^^^^^^^^^ help: use `dyn`: `dyn SomeTrait`
34+
| ^^^^^^^^^
2535
|
2636
= warning: this is accepted in the current edition (Rust 2015) but is a hard error in Rust 2021!
2737
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2021/warnings-promoted-to-error.html>
38+
help: use `dyn`
39+
|
40+
LL - pub fn function(_x: Box<SomeTrait>) {}
41+
LL + pub fn function(_x: Box<dyn SomeTrait>) {}
42+
|
2843

2944
warning: 3 warnings emitted
3045

0 commit comments

Comments
 (0)