Skip to content

Commit efb20bc

Browse files
committed
Point to previous applicability when declared multiple times
1 parent ec85a1b commit efb20bc

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

compiler/rustc_macros/src/diagnostics/diagnostic_builder.rs

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -478,26 +478,12 @@ impl DiagnosticDeriveBuilder {
478478
let formatted_str = self.build_format(&s.value(), s.span());
479479
code.set_once((formatted_str, span));
480480
}
481-
"applicability" => {
482-
applicability = match applicability {
483-
Some(v) => {
484-
span_err(
485-
span,
486-
"applicability cannot be set in both the field and \
487-
attribute",
488-
)
489-
.emit();
490-
Some(v)
491-
}
492-
None => match Applicability::from_str(&s.value()) {
493-
Ok(v) => Some(quote! { #v }),
494-
Err(()) => {
495-
span_err(span, "invalid applicability").emit();
496-
None
497-
}
498-
},
481+
"applicability" => match Applicability::from_str(&s.value()) {
482+
Ok(v) => applicability.set_once((quote! { #v }, span)),
483+
Err(()) => {
484+
span_err(span, "invalid applicability").emit();
499485
}
500-
}
486+
},
501487
_ => throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
502488
diag.help(
503489
"only `message`, `code` and `applicability` are valid field \
@@ -516,8 +502,9 @@ impl DiagnosticDeriveBuilder {
516502
}
517503
}
518504

519-
let applicability =
520-
applicability.unwrap_or_else(|| quote!(rustc_errors::Applicability::Unspecified));
505+
let applicability = applicability
506+
.value()
507+
.unwrap_or_else(|| quote!(rustc_errors::Applicability::Unspecified));
521508

522509
let name = path.segments.last().unwrap().ident.to_string();
523510
let method = format_ident!("span_{}", name);
@@ -559,7 +546,7 @@ impl DiagnosticDeriveBuilder {
559546
fn span_and_applicability_of_ty(
560547
&self,
561548
info: FieldInfo<'_>,
562-
) -> Result<(TokenStream, Option<TokenStream>), DiagnosticDeriveError> {
549+
) -> Result<(TokenStream, Option<(TokenStream, proc_macro::Span)>), DiagnosticDeriveError> {
563550
match &info.ty {
564551
// If `ty` is `Span` w/out applicability, then use `Applicability::Unspecified`.
565552
ty @ Type::Path(..) if type_matches_path(ty, &["rustc_span", "Span"]) => {
@@ -594,14 +581,14 @@ impl DiagnosticDeriveBuilder {
594581
let Some((span_idx, _)) = span_idx else {
595582
type_err(&tup.span())?;
596583
};
597-
let Some((applicability_idx, _applicability_span)) = applicability_idx else {
584+
let Some((applicability_idx, applicability_span)) = applicability_idx else {
598585
type_err(&tup.span())?;
599586
};
600587
let binding = &info.binding.binding;
601588
let span = quote!(#binding.#span_idx);
602589
let applicability = quote!(#binding.#applicability_idx);
603590

604-
Ok((span, Some(applicability)))
591+
Ok((span, Some((applicability, applicability_span))))
605592
}
606593
// If `ty` isn't a `Span` or `(Span, Applicability)` then emit an error.
607594
_ => throw_span_err!(info.span.unwrap(), "wrong field type for suggestion", |diag| {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ struct ErrorWithNoteCustomWrongOrder {
436436
#[diag(typeck::ambiguous_lifetime_bound, code = "E0123")]
437437
struct ApplicabilityInBoth {
438438
#[suggestion(typeck::suggestion, code = "...", applicability = "maybe-incorrect")]
439-
//~^ ERROR applicability cannot be set in both the field and attribute
439+
//~^ ERROR specified multiple times
440440
suggestion: (Span, Applicability),
441441
}
442442

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,11 +293,17 @@ error: `#[label = ...]` is not a valid attribute
293293
LL | #[label = "bar"]
294294
| ^^^^^^^^^^^^^^^^
295295

296-
error: applicability cannot be set in both the field and attribute
296+
error: specified multiple times
297297
--> $DIR/diagnostic-derive.rs:438:52
298298
|
299299
LL | #[suggestion(typeck::suggestion, code = "...", applicability = "maybe-incorrect")]
300300
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
301+
|
302+
note: previously specified here
303+
--> $DIR/diagnostic-derive.rs:440:24
304+
|
305+
LL | suggestion: (Span, Applicability),
306+
| ^^^^^^^^^^^^^
301307

302308
error: invalid applicability
303309
--> $DIR/diagnostic-derive.rs:446:52

0 commit comments

Comments
 (0)