Skip to content

Commit d7fcd01

Browse files
authored
Rollup merge of #106072 - eopb:dyn-derive, r=estebank
fix: misleading "add dyn keyword before derive macro" suggestion Fixes #106071
2 parents afaf3e0 + 1caec6f commit d7fcd01

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
@@ -3305,7 +3305,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
33053305
let label = "add `dyn` keyword before this trait";
33063306
let mut diag =
33073307
rustc_errors::struct_span_err!(tcx.sess, self_ty.span, E0782, "{}", msg);
3308-
diag.multipart_suggestion_verbose(label, sugg, Applicability::MachineApplicable);
3308+
if self_ty.span.can_be_used_for_suggestions() {
3309+
diag.multipart_suggestion_verbose(
3310+
label,
3311+
sugg,
3312+
Applicability::MachineApplicable,
3313+
);
3314+
}
33093315
// check if the impl trait that we are considering is a impl of a local trait
33103316
self.maybe_lint_blanket_trait_impl(&self_ty, &mut diag);
33113317
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)