Skip to content

Commit 2e72387

Browse files
committed
Ensure code= in #[suggestion(...)] is only set once
1 parent adcc55d commit 2e72387

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ impl DiagnosticDeriveBuilder {
476476
match nested_name {
477477
"code" => {
478478
let formatted_str = self.build_format(&s.value(), s.span());
479-
code = Some(formatted_str);
479+
code.set_once((formatted_str, span));
480480
}
481481
"applicability" => {
482482
applicability = match applicability {
@@ -524,7 +524,7 @@ impl DiagnosticDeriveBuilder {
524524

525525
let msg = msg.unwrap_or_else(|| parse_quote! { _subdiag::suggestion });
526526
let msg = quote! { rustc_errors::fluent::#msg };
527-
let code = code.unwrap_or_else(|| quote! { String::new() });
527+
let code = code.value().unwrap_or_else(|| quote! { String::new() });
528528

529529
Ok(quote! { #diag.#method(#span_field, #msg, #code, #applicability); })
530530
}

src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,3 +581,11 @@ struct LintAttributeOnSessionDiag {}
581581
//~| ERROR diagnostic slug not specified
582582
//~| ERROR cannot find attribute `lint` in this scope
583583
struct LintAttributeOnLintDiag {}
584+
585+
#[derive(Diagnostic)]
586+
#[diag(typeck::ambiguous_lifetime_bound, code = "E0123")]
587+
struct DuplicatedSuggestionCode {
588+
#[suggestion(typeck::suggestion, code = "...", code = ",,,")]
589+
//~^ ERROR specified multiple times
590+
suggestion: Span,
591+
}

src/test/ui-fulldeps/session-diagnostic/diagnostic-derive.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,18 @@ LL | | struct LintAttributeOnLintDiag {}
403403
|
404404
= help: specify the slug as the first argument to the attribute, such as `#[diag(typeck::example_error)]`
405405

406+
error: specified multiple times
407+
--> $DIR/diagnostic-derive.rs:588:52
408+
|
409+
LL | #[suggestion(typeck::suggestion, code = "...", code = ",,,")]
410+
| ^^^^^^^^^^^^
411+
|
412+
note: previously specified here
413+
--> $DIR/diagnostic-derive.rs:588:38
414+
|
415+
LL | #[suggestion(typeck::suggestion, code = "...", code = ",,,")]
416+
| ^^^^^^^^^^^^
417+
406418
error: cannot find attribute `nonsense` in this scope
407419
--> $DIR/diagnostic-derive.rs:53:3
408420
|
@@ -459,7 +471,7 @@ LL | arg: impl IntoDiagnosticArg,
459471
| ^^^^^^^^^^^^^^^^^ required by this bound in `DiagnosticBuilder::<'a, G>::set_arg`
460472
= note: this error originates in the derive macro `Diagnostic` (in Nightly builds, run with -Z macro-backtrace for more info)
461473

462-
error: aborting due to 55 previous errors
474+
error: aborting due to 56 previous errors
463475

464476
Some errors have detailed explanations: E0277, E0425.
465477
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)