From 2e72387fd03082cd067a4a56acf4b231b543c7fd Mon Sep 17 00:00:00 2001 From: Xiretza Date: Sat, 10 Sep 2022 14:28:12 +0200 Subject: [PATCH] Ensure code= in #[suggestion(...)] is only set once --- .../src/diagnostics/diagnostic_builder.rs | 4 ++-- .../session-diagnostic/diagnostic-derive.rs | 8 ++++++++ .../session-diagnostic/diagnostic-derive.stderr | 14 +++++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs b/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs index 2bff12b0059..11459e18190 100644 --- a/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs +++ b/compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs @@ -476,7 +476,7 @@ impl DiagnosticDeriveBuilder { match nested_name { "code" => { let formatted_str = self.build_format(&s.value(), s.span()); - code = Some(formatted_str); + code.set_once((formatted_str, span)); } "applicability" => { applicability = match applicability { @@ -524,7 +524,7 @@ impl DiagnosticDeriveBuilder { let msg = msg.unwrap_or_else(|| parse_quote! { _subdiag::suggestion }); let msg = quote! { rustc_errors::fluent::#msg }; - let code = code.unwrap_or_else(|| quote! { String::new() }); + let code = code.value().unwrap_or_else(|| quote! { String::new() }); Ok(quote! { #diag.#method(#span_field, #msg, #code, #applicability); }) } diff --git a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs index 80ea9082881..a113f2a034a 100644 --- a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs +++ b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs @@ -581,3 +581,11 @@ struct LintAttributeOnSessionDiag {} //~| ERROR diagnostic slug not specified //~| ERROR cannot find attribute `lint` in this scope struct LintAttributeOnLintDiag {} + +#[derive(Diagnostic)] +#[diag(typeck::ambiguous_lifetime_bound, code = "E0123")] +struct DuplicatedSuggestionCode { + #[suggestion(typeck::suggestion, code = "...", code = ",,,")] + //~^ ERROR specified multiple times + suggestion: Span, +} diff --git a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr index c3972beb512..aa768e28334 100644 --- a/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr +++ b/src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr @@ -403,6 +403,18 @@ LL | | struct LintAttributeOnLintDiag {} | = help: specify the slug as the first argument to the attribute, such as `#[diag(typeck::example_error)]` +error: specified multiple times + --> $DIR/diagnostic-derive.rs:588:52 + | +LL | #[suggestion(typeck::suggestion, code = "...", code = ",,,")] + | ^^^^^^^^^^^^ + | +note: previously specified here + --> $DIR/diagnostic-derive.rs:588:38 + | +LL | #[suggestion(typeck::suggestion, code = "...", code = ",,,")] + | ^^^^^^^^^^^^ + error: cannot find attribute `nonsense` in this scope --> $DIR/diagnostic-derive.rs:53:3 | @@ -459,7 +471,7 @@ LL | arg: impl IntoDiagnosticArg, | ^^^^^^^^^^^^^^^^^ required by this bound in `DiagnosticBuilder::<'a, G>::set_arg` = note: this error originates in the derive macro `Diagnostic` (in Nightly builds, run with -Z macro-backtrace for more info) -error: aborting due to 55 previous errors +error: aborting due to 56 previous errors Some errors have detailed explanations: E0277, E0425. For more information about an error, try `rustc --explain E0277`.