From d21bc6562a03917073bd1fd4f65f447cf5a92aba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 27 May 2022 10:47:05 -0700 Subject: [PATCH 1/2] Add test for #97343 --- src/test/ui/derives/issue-97343.rs | 8 ++++++++ src/test/ui/derives/issue-97343.stderr | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/test/ui/derives/issue-97343.rs create mode 100644 src/test/ui/derives/issue-97343.stderr diff --git a/src/test/ui/derives/issue-97343.rs b/src/test/ui/derives/issue-97343.rs new file mode 100644 index 0000000000000..6c5626f4796ad --- /dev/null +++ b/src/test/ui/derives/issue-97343.rs @@ -0,0 +1,8 @@ +use std::fmt::Debug; + +#[derive(Debug)] //~ ERROR expected struct, variant or union type, found type parameter `Irrelevant` +pub struct Irrelevant { //~ ERROR type arguments are not allowed for this type + irrelevant: Irrelevant, +} + +fn main() {} diff --git a/src/test/ui/derives/issue-97343.stderr b/src/test/ui/derives/issue-97343.stderr new file mode 100644 index 0000000000000..418178b01b910 --- /dev/null +++ b/src/test/ui/derives/issue-97343.stderr @@ -0,0 +1,22 @@ +error[E0574]: expected struct, variant or union type, found type parameter `Irrelevant` + --> $DIR/issue-97343.rs:3:10 + | +LL | #[derive(Debug)] + | ^^^^^ not a struct, variant or union type + | + = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info) + +error[E0109]: type arguments are not allowed for this type + --> $DIR/issue-97343.rs:4:23 + | +LL | #[derive(Debug)] + | ----- in this derive macro expansion +LL | pub struct Irrelevant { + | ^^^^^^^^^^ type argument not allowed + | + = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info) + +error: aborting due to 2 previous errors + +Some errors have detailed explanations: E0109, E0574. +For more information about an error, try `rustc --explain E0109`. From f2a1b7b7724fef2876094f9b4a6c356345d7dd67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 27 May 2022 10:48:12 -0700 Subject: [PATCH 2/2] Modify `derive(Debug)` to use `Self` in struct literal to avoid redundant error #97343 --- .../src/deriving/generic/mod.rs | 4 +++- src/test/ui/derives/issue-97343.rs | 2 +- src/test/ui/derives/issue-97343.stderr | 13 ++----------- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs index 0832fdad8b871..53369afae278c 100644 --- a/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs +++ b/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs @@ -1039,7 +1039,9 @@ impl<'a> MethodDef<'a> { let span = trait_.span; let mut patterns = Vec::new(); for i in 0..self_args.len() { - let struct_path = cx.path(span, vec![type_ident]); + // We could use `type_ident` instead of `Self`, but in the case of a type parameter + // shadowing the struct name, that causes a second, unnecessary E0578 error. #97343 + let struct_path = cx.path(span, vec![Ident::new(kw::SelfUpper, type_ident.span)]); let (pat, ident_expr) = trait_.create_struct_pattern( cx, struct_path, diff --git a/src/test/ui/derives/issue-97343.rs b/src/test/ui/derives/issue-97343.rs index 6c5626f4796ad..adec6c7a5c5a3 100644 --- a/src/test/ui/derives/issue-97343.rs +++ b/src/test/ui/derives/issue-97343.rs @@ -1,6 +1,6 @@ use std::fmt::Debug; -#[derive(Debug)] //~ ERROR expected struct, variant or union type, found type parameter `Irrelevant` +#[derive(Debug)] pub struct Irrelevant { //~ ERROR type arguments are not allowed for this type irrelevant: Irrelevant, } diff --git a/src/test/ui/derives/issue-97343.stderr b/src/test/ui/derives/issue-97343.stderr index 418178b01b910..eedd54f1e9f0c 100644 --- a/src/test/ui/derives/issue-97343.stderr +++ b/src/test/ui/derives/issue-97343.stderr @@ -1,11 +1,3 @@ -error[E0574]: expected struct, variant or union type, found type parameter `Irrelevant` - --> $DIR/issue-97343.rs:3:10 - | -LL | #[derive(Debug)] - | ^^^^^ not a struct, variant or union type - | - = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info) - error[E0109]: type arguments are not allowed for this type --> $DIR/issue-97343.rs:4:23 | @@ -16,7 +8,6 @@ LL | pub struct Irrelevant { | = note: this error originates in the derive macro `Debug` (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 2 previous errors +error: aborting due to previous error -Some errors have detailed explanations: E0109, E0574. -For more information about an error, try `rustc --explain E0109`. +For more information about this error, try `rustc --explain E0109`.