Skip to content

Commit 57679fb

Browse files
committed
Better error recovery in Subdiagnostic derive
1 parent e7251cc commit 57679fb

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

compiler/rustc_macros/src/diagnostics/subdiagnostic.rs

+28-20
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::collections::HashMap;
1313
use syn::{spanned::Spanned, Attribute, Meta, MetaList, MetaNameValue, NestedMeta, Path};
1414
use synstructure::{BindingInfo, Structure, VariantInfo};
1515

16+
use super::error::invalid_attr;
1617
use super::utils::{SpannedOption, SubdiagnosticKind};
1718

1819
/// The central struct for constructing the `add_to_diagnostic` method from an annotated struct.
@@ -271,18 +272,18 @@ impl<'a> SubdiagnosticDeriveBuilder<'a> {
271272
"skip_arg" => Ok(quote! {}),
272273
"primary_span" => {
273274
if kind_stats.has_multipart_suggestion {
274-
throw_invalid_attr!(attr, &Meta::Path(path), |diag| {
275-
diag.help(
275+
invalid_attr(attr, &Meta::Path(path))
276+
.help(
276277
"multipart suggestions use one or more `#[suggestion_part]`s rather \
277278
than one `#[primary_span]`",
278279
)
279-
})
280-
}
281-
282-
report_error_if_not_applied_to_span(attr, &info)?;
280+
.emit();
281+
} else {
282+
report_error_if_not_applied_to_span(attr, &info)?;
283283

284-
let binding = info.binding.binding.clone();
285-
self.span_field.set_once(binding, span);
284+
let binding = info.binding.binding.clone();
285+
self.span_field.set_once(binding, span);
286+
}
286287

287288
Ok(quote! {})
288289
}
@@ -292,14 +293,16 @@ impl<'a> SubdiagnosticDeriveBuilder<'a> {
292293
if kind_stats.has_multipart_suggestion {
293294
span_err(span, "`#[suggestion_part(...)]` attribute without `code = \"...\"`")
294295
.emit();
295-
Ok(quote! {})
296296
} else {
297-
throw_invalid_attr!(attr, &Meta::Path(path), |diag| {
298-
diag.help(
299-
"`#[suggestion_part(...)]` is only valid in multipart suggestions, use `#[primary_span]` instead",
300-
)
301-
});
297+
invalid_attr(attr, &Meta::Path(path))
298+
.help(
299+
"`#[suggestion_part(...)]` is only valid in multipart suggestions, \
300+
use `#[primary_span]` instead",
301+
)
302+
.emit();
302303
}
304+
305+
Ok(quote! {})
303306
}
304307
"applicability" => {
305308
if kind_stats.has_multipart_suggestion || kind_stats.has_normal_suggestion {
@@ -322,19 +325,24 @@ impl<'a> SubdiagnosticDeriveBuilder<'a> {
322325

323326
Ok(quote! {})
324327
}
325-
_ => throw_invalid_attr!(attr, &Meta::Path(path), |diag| {
328+
_ => {
326329
let mut span_attrs = vec![];
327330
if kind_stats.has_multipart_suggestion {
328331
span_attrs.push("suggestion_part");
329332
}
330333
if !kind_stats.all_multipart_suggestions {
331334
span_attrs.push("primary_span")
332335
}
333-
diag.help(format!(
334-
"only `{}`, `applicability` and `skip_arg` are valid field attributes",
335-
span_attrs.join(", ")
336-
))
337-
}),
336+
337+
invalid_attr(attr, &Meta::Path(path))
338+
.help(format!(
339+
"only `{}`, `applicability` and `skip_arg` are valid field attributes",
340+
span_attrs.join(", ")
341+
))
342+
.emit();
343+
344+
Ok(quote! {})
345+
}
338346
}
339347
}
340348

0 commit comments

Comments
 (0)