macros: support adding warnings to diags
Both diagnostic and subdiagnostic derives were missing the ability to add warnings to diagnostics - this is made more difficult by the `warn` attribute already existing, so this name being unavailable for the derives to use. `#[warn_]` is used instead, which requires special-casing so that `{span_,}warn` is called instead of `{span_,}warn_`. Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
parent
88c11c5bff
commit
81cf2294b4
@ -59,7 +59,7 @@ pub(crate) fn into_tokens(self) -> TokenStream {
|
||||
return DiagnosticDeriveError::ErrorHandled.to_compile_error();
|
||||
}
|
||||
(Some(DiagnosticDeriveKind::Lint), _) => {
|
||||
span_err(span, "only `#[error(..)]` and `#[warn(..)]` are supported")
|
||||
span_err(span, "only `#[error(..)]` and `#[warning(..)]` are supported")
|
||||
.help("use the `#[error(...)]` attribute to create a error")
|
||||
.emit();
|
||||
return DiagnosticDeriveError::ErrorHandled.to_compile_error();
|
||||
|
@ -8,7 +8,7 @@
|
||||
report_error_if_not_applied_to_span, report_type_error, type_is_unit, type_matches_path,
|
||||
Applicability, FieldInfo, FieldInnerTy, HasFieldMap, SetOnce,
|
||||
};
|
||||
use proc_macro2::{Ident, TokenStream};
|
||||
use proc_macro2::{Ident, Span, TokenStream};
|
||||
use quote::{format_ident, quote};
|
||||
use std::collections::HashMap;
|
||||
use std::str::FromStr;
|
||||
@ -156,7 +156,7 @@ fn generate_structure_code_for_attr(
|
||||
let name = name.as_str();
|
||||
let meta = attr.parse_meta()?;
|
||||
|
||||
let is_help_or_note = matches!(name, "help" | "note");
|
||||
let is_help_note_or_warn = matches!(name, "help" | "note" | "warn_");
|
||||
|
||||
let nested = match meta {
|
||||
// Most attributes are lists, like `#[error(..)]`/`#[warning(..)]` for most cases or
|
||||
@ -164,8 +164,12 @@ fn generate_structure_code_for_attr(
|
||||
Meta::List(MetaList { ref nested, .. }) => nested,
|
||||
// Subdiagnostics without spans can be applied to the type too, and these are just
|
||||
// paths: `#[help]` and `#[note]`
|
||||
Meta::Path(_) if is_help_or_note => {
|
||||
let fn_name = proc_macro2::Ident::new(name, attr.span());
|
||||
Meta::Path(_) if is_help_note_or_warn => {
|
||||
let fn_name = if name == "warn_" {
|
||||
Ident::new("warn", attr.span())
|
||||
} else {
|
||||
Ident::new(name, attr.span())
|
||||
};
|
||||
return Ok(quote! { #diag.#fn_name(rustc_errors::fluent::_subdiag::#fn_name); });
|
||||
}
|
||||
_ => throw_invalid_attr!(attr, &meta),
|
||||
@ -177,9 +181,11 @@ fn generate_structure_code_for_attr(
|
||||
"error" => self.kind.set_once((DiagnosticDeriveKind::Error, span)),
|
||||
"warning" => self.kind.set_once((DiagnosticDeriveKind::Warn, span)),
|
||||
"lint" => self.kind.set_once((DiagnosticDeriveKind::Lint, span)),
|
||||
"help" | "note" => (),
|
||||
"help" | "note" | "warn_" => (),
|
||||
_ => throw_invalid_attr!(attr, &meta, |diag| {
|
||||
diag.help("only `error`, `warning`, `help` and `note` are valid attributes")
|
||||
diag.help(
|
||||
"only `error`, `warning`, `help`, `note` and `warn_` are valid attributes",
|
||||
)
|
||||
}),
|
||||
}
|
||||
|
||||
@ -188,14 +194,16 @@ fn generate_structure_code_for_attr(
|
||||
let mut nested_iter = nested.into_iter();
|
||||
if let Some(nested_attr) = nested_iter.next() {
|
||||
// Report an error if there are any other list items after the path.
|
||||
if is_help_or_note && nested_iter.next().is_some() {
|
||||
if is_help_note_or_warn && nested_iter.next().is_some() {
|
||||
throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
|
||||
diag.help("`help` and `note` struct attributes can only have one argument")
|
||||
diag.help(
|
||||
"`help`, `note` and `warn_` struct attributes can only have one argument",
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
match nested_attr {
|
||||
NestedMeta::Meta(Meta::Path(path)) if is_help_or_note => {
|
||||
NestedMeta::Meta(Meta::Path(path)) if is_help_note_or_warn => {
|
||||
let fn_name = proc_macro2::Ident::new(name, attr.span());
|
||||
return Ok(quote! { #diag.#fn_name(rustc_errors::fluent::#path); });
|
||||
}
|
||||
@ -203,7 +211,7 @@ fn generate_structure_code_for_attr(
|
||||
self.slug.set_once((path.clone(), span));
|
||||
}
|
||||
NestedMeta::Meta(meta @ Meta::NameValue(_))
|
||||
if !is_help_or_note
|
||||
if !is_help_note_or_warn
|
||||
&& meta.path().segments.last().unwrap().ident.to_string() == "code" =>
|
||||
{
|
||||
// don't error for valid follow-up attributes
|
||||
@ -347,10 +355,12 @@ fn generate_inner_field_code_path(
|
||||
report_error_if_not_applied_to_span(attr, &info)?;
|
||||
Ok(self.add_spanned_subdiagnostic(binding, ident, parse_quote! { _subdiag::label }))
|
||||
}
|
||||
"note" | "help" => {
|
||||
let path = match name {
|
||||
"note" => parse_quote! { _subdiag::note },
|
||||
"help" => parse_quote! { _subdiag::help },
|
||||
"note" | "help" | "warn_" => {
|
||||
let warn_ident = Ident::new("warn", Span::call_site());
|
||||
let (ident, path) = match name {
|
||||
"note" => (ident, parse_quote! { _subdiag::note }),
|
||||
"help" => (ident, parse_quote! { _subdiag::help }),
|
||||
"warn_" => (&warn_ident, parse_quote! { _subdiag::warn }),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
if type_matches_path(&info.ty, &["rustc_span", "Span"]) {
|
||||
@ -387,10 +397,10 @@ fn generate_inner_field_code_list(
|
||||
"suggestion" | "suggestion_short" | "suggestion_hidden" | "suggestion_verbose" => {
|
||||
return self.generate_inner_field_code_suggestion(attr, info);
|
||||
}
|
||||
"label" | "help" | "note" => (),
|
||||
"label" | "help" | "note" | "warn_" => (),
|
||||
_ => throw_invalid_attr!(attr, &meta, |diag| {
|
||||
diag.help(
|
||||
"only `label`, `note`, `help` or `suggestion{,_short,_hidden,_verbose}` are \
|
||||
"only `label`, `help`, `note`, `warn` or `suggestion{,_short,_hidden,_verbose}` are \
|
||||
valid field attributes",
|
||||
)
|
||||
}),
|
||||
@ -419,7 +429,14 @@ fn generate_inner_field_code_list(
|
||||
Ok(self.add_spanned_subdiagnostic(binding, ident, msg))
|
||||
}
|
||||
"note" | "help" if type_is_unit(&info.ty) => Ok(self.add_subdiagnostic(ident, msg)),
|
||||
"note" | "help" => report_type_error(attr, "`Span` or `()`")?,
|
||||
// `warn_` must be special-cased because the attribute `warn` already has meaning and
|
||||
// so isn't used, despite the diagnostic API being named `warn`.
|
||||
"warn_" if type_matches_path(&info.ty, &["rustc_span", "Span"]) => Ok(self
|
||||
.add_spanned_subdiagnostic(binding, &Ident::new("warn", Span::call_site()), msg)),
|
||||
"warn_" if type_is_unit(&info.ty) => {
|
||||
Ok(self.add_subdiagnostic(&Ident::new("warn", Span::call_site()), msg))
|
||||
}
|
||||
"note" | "help" | "warn_" => report_type_error(attr, "`Span` or `()`")?,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
@ -260,10 +260,12 @@ pub mod fluent_generated {
|
||||
#generated
|
||||
|
||||
pub mod _subdiag {
|
||||
pub const note: crate::SubdiagnosticMessage =
|
||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("note"));
|
||||
pub const help: crate::SubdiagnosticMessage =
|
||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("help"));
|
||||
pub const note: crate::SubdiagnosticMessage =
|
||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("note"));
|
||||
pub const warn: crate::SubdiagnosticMessage =
|
||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("warn"));
|
||||
pub const label: crate::SubdiagnosticMessage =
|
||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("label"));
|
||||
pub const suggestion: crate::SubdiagnosticMessage =
|
||||
|
@ -37,6 +37,8 @@ enum SubdiagnosticKind {
|
||||
Note,
|
||||
/// `#[help(...)]`
|
||||
Help,
|
||||
/// `#[warn_(...)]`
|
||||
Warn,
|
||||
/// `#[suggestion{,_short,_hidden,_verbose}]`
|
||||
Suggestion(SubdiagnosticSuggestionKind),
|
||||
}
|
||||
@ -49,6 +51,7 @@ fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
"label" => Ok(SubdiagnosticKind::Label),
|
||||
"note" => Ok(SubdiagnosticKind::Note),
|
||||
"help" => Ok(SubdiagnosticKind::Help),
|
||||
"warn_" => Ok(SubdiagnosticKind::Warn),
|
||||
"suggestion" => Ok(SubdiagnosticKind::Suggestion(SubdiagnosticSuggestionKind::Normal)),
|
||||
"suggestion_short" => {
|
||||
Ok(SubdiagnosticKind::Suggestion(SubdiagnosticSuggestionKind::Short))
|
||||
@ -70,6 +73,7 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
SubdiagnosticKind::Label => write!(f, "label"),
|
||||
SubdiagnosticKind::Note => write!(f, "note"),
|
||||
SubdiagnosticKind::Help => write!(f, "help"),
|
||||
SubdiagnosticKind::Warn => write!(f, "warn"),
|
||||
SubdiagnosticKind::Suggestion(SubdiagnosticSuggestionKind::Normal) => {
|
||||
write!(f, "suggestion")
|
||||
}
|
||||
|
@ -130,8 +130,9 @@ pub fn fluent_messages(input: TokenStream) -> TokenStream {
|
||||
warning,
|
||||
error,
|
||||
lint,
|
||||
note,
|
||||
help,
|
||||
note,
|
||||
warn_,
|
||||
// field attributes
|
||||
skip_arg,
|
||||
primary_span,
|
||||
@ -148,8 +149,9 @@ pub fn fluent_messages(input: TokenStream) -> TokenStream {
|
||||
warning,
|
||||
error,
|
||||
lint,
|
||||
note,
|
||||
help,
|
||||
note,
|
||||
warn_,
|
||||
// field attributes
|
||||
skip_arg,
|
||||
primary_span,
|
||||
@ -166,6 +168,7 @@ pub fn fluent_messages(input: TokenStream) -> TokenStream {
|
||||
label,
|
||||
help,
|
||||
note,
|
||||
warn_,
|
||||
suggestion,
|
||||
suggestion_short,
|
||||
suggestion_hidden,
|
||||
|
@ -538,7 +538,7 @@ struct LabelWithTrailingList {
|
||||
|
||||
#[derive(SessionDiagnostic)]
|
||||
#[lint(typeck::ambiguous_lifetime_bound)]
|
||||
//~^ ERROR only `#[error(..)]` and `#[warn(..)]` are supported
|
||||
//~^ ERROR only `#[error(..)]` and `#[warning(..)]` are supported
|
||||
struct LintsBad {
|
||||
}
|
||||
|
||||
@ -559,3 +559,10 @@ struct ErrorWithMultiSpan {
|
||||
#[primary_span]
|
||||
span: MultiSpan,
|
||||
}
|
||||
|
||||
#[derive(SessionDiagnostic)]
|
||||
#[error(typeck::ambiguous_lifetime_bound, code = "E0123")]
|
||||
#[warn_]
|
||||
struct ErrorWithWarn {
|
||||
val: String,
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ error: `#[nonsense(...)]` is not a valid attribute
|
||||
LL | #[nonsense(typeck::ambiguous_lifetime_bound, code = "E0123")]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
|
||||
= help: only `error`, `warning`, `help` and `note` are valid attributes
|
||||
= help: only `error`, `warning`, `help`, `note` and `warn_` are valid attributes
|
||||
|
||||
error: diagnostic kind not specified
|
||||
--> $DIR/diagnostic-derive.rs:53:1
|
||||
@ -363,7 +363,7 @@ error: `#[label(...)]` is not a valid attribute
|
||||
LL | #[label(typeck::label, foo("..."))]
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: only `#[error(..)]` and `#[warn(..)]` are supported
|
||||
error: only `#[error(..)]` and `#[warning(..)]` are supported
|
||||
--> $DIR/diagnostic-derive.rs:540:1
|
||||
|
|
||||
LL | / #[lint(typeck::ambiguous_lifetime_bound)]
|
||||
|
@ -508,3 +508,15 @@ enum AX {
|
||||
span: Span,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(SessionSubdiagnostic)]
|
||||
#[warn_(parser::add_paren)]
|
||||
struct AY {
|
||||
}
|
||||
|
||||
#[derive(SessionSubdiagnostic)]
|
||||
#[warn_(parser::add_paren)]
|
||||
struct AZ {
|
||||
#[primary_span]
|
||||
span: Span,
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user