Skip to content

Commit f2a1b7b

Browse files
committed
Modify derive(Debug) to use Self in struct literal to avoid redundant error
rust-lang#97343
1 parent d21bc65 commit f2a1b7b

File tree

3 files changed

+6
-13
lines changed

3 files changed

+6
-13
lines changed

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,9 @@ impl<'a> MethodDef<'a> {
10391039
let span = trait_.span;
10401040
let mut patterns = Vec::new();
10411041
for i in 0..self_args.len() {
1042-
let struct_path = cx.path(span, vec![type_ident]);
1042+
// We could use `type_ident` instead of `Self`, but in the case of a type parameter
1043+
// shadowing the struct name, that causes a second, unnecessary E0578 error. #97343
1044+
let struct_path = cx.path(span, vec![Ident::new(kw::SelfUpper, type_ident.span)]);
10431045
let (pat, ident_expr) = trait_.create_struct_pattern(
10441046
cx,
10451047
struct_path,

src/test/ui/derives/issue-97343.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::fmt::Debug;
22

3-
#[derive(Debug)] //~ ERROR expected struct, variant or union type, found type parameter `Irrelevant`
3+
#[derive(Debug)]
44
pub struct Irrelevant<Irrelevant> { //~ ERROR type arguments are not allowed for this type
55
irrelevant: Irrelevant,
66
}
Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error[E0574]: expected struct, variant or union type, found type parameter `Irrelevant`
2-
--> $DIR/issue-97343.rs:3:10
3-
|
4-
LL | #[derive(Debug)]
5-
| ^^^^^ not a struct, variant or union type
6-
|
7-
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
8-
91
error[E0109]: type arguments are not allowed for this type
102
--> $DIR/issue-97343.rs:4:23
113
|
@@ -16,7 +8,6 @@ LL | pub struct Irrelevant<Irrelevant> {
168
|
179
= note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info)
1810

19-
error: aborting due to 2 previous errors
11+
error: aborting due to previous error
2012

21-
Some errors have detailed explanations: E0109, E0574.
22-
For more information about an error, try `rustc --explain E0109`.
13+
For more information about this error, try `rustc --explain E0109`.

0 commit comments

Comments
 (0)