Skip to content

Commit 41ee87c

Browse files
committed
fix: misleading add dyn to derive macro suggestion
1 parent 2d8651a commit 41ee87c

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
@@ -3116,7 +3116,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
31163116
let label = "add `dyn` keyword before this trait";
31173117
let mut diag =
31183118
rustc_errors::struct_span_err!(tcx.sess, self_ty.span, E0782, "{}", msg);
3119-
diag.multipart_suggestion_verbose(label, sugg, Applicability::MachineApplicable);
3119+
if self_ty.span.can_be_used_for_suggestions() {
3120+
diag.multipart_suggestion_verbose(
3121+
label,
3122+
sugg,
3123+
Applicability::MachineApplicable,
3124+
);
3125+
}
31203126
// check if the impl trait that we are considering is a impl of a local trait
31213127
self.maybe_lint_blanket_trait_impl(&self_ty, &mut diag);
31223128
diag.emit();

src/test/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() {}
+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)