Add note_once/help_once to diagnostic derives

This commit is contained in:
Xiretza 2024-04-24 17:28:26 +00:00
parent 56bca95875
commit eee14e9adf
3 changed files with 30 additions and 4 deletions

View File

@ -158,7 +158,9 @@ impl DiagnosticDeriveVariantBuilder {
let slug = subdiag.slug.unwrap_or_else(|| match subdiag.kind {
SubdiagnosticKind::Label => parse_quote! { _subdiag::label },
SubdiagnosticKind::Note => parse_quote! { _subdiag::note },
SubdiagnosticKind::NoteOnce => parse_quote! { _subdiag::note_once },
SubdiagnosticKind::Help => parse_quote! { _subdiag::help },
SubdiagnosticKind::HelpOnce => parse_quote! { _subdiag::help_once },
SubdiagnosticKind::Warn => parse_quote! { _subdiag::warn },
SubdiagnosticKind::Suggestion { .. } => parse_quote! { _subdiag::suggestion },
SubdiagnosticKind::MultipartSuggestion { .. } => unreachable!(),
@ -233,9 +235,11 @@ impl DiagnosticDeriveVariantBuilder {
};
let fn_ident = format_ident!("{}", subdiag);
match subdiag {
SubdiagnosticKind::Note | SubdiagnosticKind::Help | SubdiagnosticKind::Warn => {
Ok(self.add_subdiagnostic(&fn_ident, slug))
}
SubdiagnosticKind::Note
| SubdiagnosticKind::NoteOnce
| SubdiagnosticKind::Help
| SubdiagnosticKind::HelpOnce
| SubdiagnosticKind::Warn => Ok(self.add_subdiagnostic(&fn_ident, slug)),
SubdiagnosticKind::Label | SubdiagnosticKind::Suggestion { .. } => {
throw_invalid_attr!(attr, |diag| diag
.help("`#[label]` and `#[suggestion]` can only be applied to fields"));
@ -347,7 +351,11 @@ impl DiagnosticDeriveVariantBuilder {
report_error_if_not_applied_to_span(attr, &info)?;
Ok(self.add_spanned_subdiagnostic(binding, &fn_ident, slug))
}
SubdiagnosticKind::Note | SubdiagnosticKind::Help | SubdiagnosticKind::Warn => {
SubdiagnosticKind::Note
| SubdiagnosticKind::NoteOnce
| SubdiagnosticKind::Help
| SubdiagnosticKind::HelpOnce
| SubdiagnosticKind::Warn => {
let inner = info.ty.inner_type();
if type_matches_path(inner, &["rustc_span", "Span"])
|| type_matches_path(inner, &["rustc_span", "MultiSpan"])

View File

@ -575,8 +575,12 @@ pub(super) enum SubdiagnosticKind {
Label,
/// `#[note(...)]`
Note,
/// `#[note_once(...)]`
NoteOnce,
/// `#[help(...)]`
Help,
/// `#[help_once(...)]`
HelpOnce,
/// `#[warning(...)]`
Warn,
/// `#[suggestion{,_short,_hidden,_verbose}]`
@ -624,7 +628,9 @@ impl SubdiagnosticVariant {
let mut kind = match name {
"label" => SubdiagnosticKind::Label,
"note" => SubdiagnosticKind::Note,
"note_once" => SubdiagnosticKind::NoteOnce,
"help" => SubdiagnosticKind::Help,
"help_once" => SubdiagnosticKind::HelpOnce,
"warning" => SubdiagnosticKind::Warn,
_ => {
// Recover old `#[(multipart_)suggestion_*]` syntaxes
@ -682,7 +688,9 @@ impl SubdiagnosticVariant {
match kind {
SubdiagnosticKind::Label
| SubdiagnosticKind::Note
| SubdiagnosticKind::NoteOnce
| SubdiagnosticKind::Help
| SubdiagnosticKind::HelpOnce
| SubdiagnosticKind::Warn
| SubdiagnosticKind::MultipartSuggestion { .. } => {
return Ok(Some(SubdiagnosticVariant { kind, slug: None, no_span: false }));
@ -836,7 +844,9 @@ impl SubdiagnosticVariant {
}
SubdiagnosticKind::Label
| SubdiagnosticKind::Note
| SubdiagnosticKind::NoteOnce
| SubdiagnosticKind::Help
| SubdiagnosticKind::HelpOnce
| SubdiagnosticKind::Warn => {}
}
@ -849,7 +859,9 @@ impl quote::IdentFragment for SubdiagnosticKind {
match self {
SubdiagnosticKind::Label => write!(f, "label"),
SubdiagnosticKind::Note => write!(f, "note"),
SubdiagnosticKind::NoteOnce => write!(f, "note_once"),
SubdiagnosticKind::Help => write!(f, "help"),
SubdiagnosticKind::HelpOnce => write!(f, "help_once"),
SubdiagnosticKind::Warn => write!(f, "warn"),
SubdiagnosticKind::Suggestion { .. } => write!(f, "suggestions_with_style"),
SubdiagnosticKind::MultipartSuggestion { .. } => {

View File

@ -108,7 +108,9 @@ decl_derive!(
// struct attributes
diag,
help,
help_once,
note,
note_once,
warning,
// field attributes
skip_arg,
@ -125,7 +127,9 @@ decl_derive!(
// struct attributes
diag,
help,
help_once,
note,
note_once,
warning,
// field attributes
skip_arg,
@ -142,7 +146,9 @@ decl_derive!(
// struct/variant attributes
label,
help,
help_once,
note,
note_once,
warning,
subdiagnostic,
suggestion,