Skip to content

Commit 1caec6f

Browse files
committed
fix: misleading add dyn to derive macro suggestion
1 parent 44a500c commit 1caec6f

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -3307,7 +3307,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
33073307
let label = "add `dyn` keyword before this trait";
33083308
let mut diag =
33093309
rustc_errors::struct_span_err!(tcx.sess, self_ty.span, E0782, "{}", msg);
3310-
diag.multipart_suggestion_verbose(label, sugg, Applicability::MachineApplicable);
3310+
if self_ty.span.can_be_used_for_suggestions() {
3311+
diag.multipart_suggestion_verbose(
3312+
label,
3313+
sugg,
3314+
Applicability::MachineApplicable,
3315+
);
3316+
}
33113317
// check if the impl trait that we are considering is a impl of a local trait
33123318
self.maybe_lint_blanket_trait_impl(&self_ty, &mut diag);
33133319
diag.emit();

tests/ui/traits/issue-106072.rs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[derive(Clone)] //~ trait objects must include the `dyn` keyword
2+
//~| trait objects must include the `dyn` keyword
3+
struct Foo;
4+
trait Foo {} //~ the name `Foo` is defined multiple times
5+
fn main() {}

tests/ui/traits/issue-106072.stderr

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
error[E0428]: the name `Foo` is defined multiple times
2+
--> $DIR/issue-106072.rs:4:1
3+
|
4+
LL | struct Foo;
5+
| ----------- previous definition of the type `Foo` here
6+
LL | trait Foo {}
7+
| ^^^^^^^^^ `Foo` redefined here
8+
|
9+
= note: `Foo` must be defined only once in the type namespace of this module
10+
11+
error[E0782]: trait objects must include the `dyn` keyword
12+
--> $DIR/issue-106072.rs:1:10
13+
|
14+
LL | #[derive(Clone)]
15+
| ^^^^^
16+
|
17+
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
18+
19+
error[E0782]: trait objects must include the `dyn` keyword
20+
--> $DIR/issue-106072.rs:1:10
21+
|
22+
LL | #[derive(Clone)]
23+
| ^^^^^
24+
|
25+
= note: this error originates in the derive macro `Clone` (in Nightly builds, run with -Z macro-backtrace for more info)
26+
27+
error: aborting due to 3 previous errors
28+
29+
Some errors have detailed explanations: E0428, E0782.
30+
For more information about an error, try `rustc --explain E0428`.

0 commit comments

Comments
 (0)