macros: use typed identifiers in diag derive
Using typed identifiers instead of strings with the Fluent identifier enables the diagnostic derive to benefit from the compile-time validation that comes with typed identifiers - use of a non-existent Fluent identifier will not compile. Signed-off-by: David Wood <david.wood@huawei.com>
This commit is contained in:
parent
fc96600bf6
commit
99bc979403
11 changed files with 689 additions and 471 deletions
|
@ -12,7 +12,9 @@ use proc_macro2::{Ident, TokenStream};
|
|||
use quote::{format_ident, quote};
|
||||
use std::collections::HashMap;
|
||||
use std::str::FromStr;
|
||||
use syn::{spanned::Spanned, Attribute, Meta, MetaList, MetaNameValue, Type};
|
||||
use syn::{
|
||||
parse_quote, spanned::Spanned, Attribute, Meta, MetaList, MetaNameValue, NestedMeta, Path, Type,
|
||||
};
|
||||
use synstructure::{BindingInfo, Structure};
|
||||
|
||||
/// The central struct for constructing the `into_diagnostic` method from an annotated struct.
|
||||
|
@ -118,23 +120,23 @@ impl<'a> SessionDiagnosticDerive<'a> {
|
|||
return SessionDiagnosticDeriveError::ErrorHandled.to_compile_error();
|
||||
}
|
||||
(Some((kind, _)), None) => {
|
||||
span_err(span, "`slug` not specified")
|
||||
.help(&format!("use the `#[{}(slug = \"...\")]` attribute to set this diagnostic's slug", kind.descr()))
|
||||
span_err(span, "diagnostic slug not specified")
|
||||
.help(&format!(
|
||||
"specify the slug as the first argument to the attribute, such as \
|
||||
`#[{}(typeck::example_error)]`",
|
||||
kind.descr()
|
||||
))
|
||||
.emit();
|
||||
return SessionDiagnosticDeriveError::ErrorHandled.to_compile_error();
|
||||
}
|
||||
(Some((SessionDiagnosticKind::Error, _)), Some((slug, _))) => {
|
||||
quote! {
|
||||
let mut #diag = #sess.struct_err(
|
||||
rustc_errors::DiagnosticMessage::new(#slug),
|
||||
);
|
||||
let mut #diag = #sess.struct_err(rustc_errors::fluent::#slug);
|
||||
}
|
||||
}
|
||||
(Some((SessionDiagnosticKind::Warn, _)), Some((slug, _))) => {
|
||||
quote! {
|
||||
let mut #diag = #sess.struct_warn(
|
||||
rustc_errors::DiagnosticMessage::new(#slug),
|
||||
);
|
||||
let mut #diag = #sess.struct_warn(rustc_errors::fluent::#slug);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -226,7 +228,7 @@ struct SessionDiagnosticDeriveBuilder {
|
|||
kind: Option<(SessionDiagnosticKind, proc_macro::Span)>,
|
||||
/// Slug is a mandatory part of the struct attribute as corresponds to the Fluent message that
|
||||
/// has the actual diagnostic message.
|
||||
slug: Option<(String, proc_macro::Span)>,
|
||||
slug: Option<(Path, proc_macro::Span)>,
|
||||
/// Error codes are a optional part of the struct attribute - this is only set to detect
|
||||
/// multiple specifications.
|
||||
code: Option<(String, proc_macro::Span)>,
|
||||
|
@ -240,50 +242,79 @@ impl HasFieldMap for SessionDiagnosticDeriveBuilder {
|
|||
|
||||
impl SessionDiagnosticDeriveBuilder {
|
||||
/// Establishes state in the `SessionDiagnosticDeriveBuilder` resulting from the struct
|
||||
/// attributes like `#[error(..)#`, such as the diagnostic kind and slug. Generates
|
||||
/// attributes like `#[error(..)`, such as the diagnostic kind and slug. Generates
|
||||
/// diagnostic builder calls for setting error code and creating note/help messages.
|
||||
fn generate_structure_code(
|
||||
&mut self,
|
||||
attr: &Attribute,
|
||||
) -> Result<TokenStream, SessionDiagnosticDeriveError> {
|
||||
let diag = &self.diag;
|
||||
let span = attr.span().unwrap();
|
||||
|
||||
let name = attr.path.segments.last().unwrap().ident.to_string();
|
||||
let name = name.as_str();
|
||||
let meta = attr.parse_meta()?;
|
||||
|
||||
if matches!(name, "help" | "note") && matches!(meta, Meta::Path(_) | Meta::NameValue(_)) {
|
||||
let diag = &self.diag;
|
||||
let id = match meta {
|
||||
Meta::Path(..) => quote! { #name },
|
||||
Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(s), .. }) => {
|
||||
quote! { #s }
|
||||
}
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let fn_name = proc_macro2::Ident::new(name, attr.span());
|
||||
|
||||
return Ok(quote! {
|
||||
#diag.#fn_name(rustc_errors::SubdiagnosticMessage::attr(#id));
|
||||
});
|
||||
}
|
||||
let is_help_or_note = matches!(name, "help" | "note");
|
||||
|
||||
let nested = match meta {
|
||||
// Most attributes are lists, like `#[error(..)]`/`#[warning(..)]` for most cases or
|
||||
// `#[help(..)]`/`#[note(..)]` when the user is specifying a alternative slug.
|
||||
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());
|
||||
return Ok(quote! { #diag.#fn_name(rustc_errors::fluent::_subdiag::#fn_name); });
|
||||
}
|
||||
_ => throw_invalid_attr!(attr, &meta),
|
||||
};
|
||||
|
||||
let kind = match name {
|
||||
"error" => SessionDiagnosticKind::Error,
|
||||
"warning" => SessionDiagnosticKind::Warn,
|
||||
// Check the kind before doing any further processing so that there aren't misleading
|
||||
// "no kind specified" errors if there are failures later.
|
||||
match name {
|
||||
"error" => self.kind.set_once((SessionDiagnosticKind::Error, span)),
|
||||
"warning" => self.kind.set_once((SessionDiagnosticKind::Warn, span)),
|
||||
"help" | "note" => (),
|
||||
_ => throw_invalid_attr!(attr, &meta, |diag| {
|
||||
diag.help("only `error` and `warning` are valid attributes")
|
||||
diag.help("only `error`, `warning`, `help` and `note` are valid attributes")
|
||||
}),
|
||||
};
|
||||
self.kind.set_once((kind, span));
|
||||
}
|
||||
|
||||
// First nested element should always be the path, e.g. `#[error(typeck::invalid)]` or
|
||||
// `#[help(typeck::another_help)]`.
|
||||
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() {
|
||||
throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
|
||||
diag.help("`help` and `note` struct attributes can only have one argument")
|
||||
});
|
||||
}
|
||||
|
||||
match nested_attr {
|
||||
NestedMeta::Meta(Meta::Path(path)) if is_help_or_note => {
|
||||
let fn_name = proc_macro2::Ident::new(name, attr.span());
|
||||
return Ok(quote! { #diag.#fn_name(rustc_errors::fluent::#path); });
|
||||
}
|
||||
NestedMeta::Meta(Meta::Path(path)) => {
|
||||
self.slug.set_once((path.clone(), span));
|
||||
}
|
||||
NestedMeta::Meta(meta @ Meta::NameValue(_))
|
||||
if !is_help_or_note
|
||||
&& meta.path().segments.last().unwrap().ident.to_string() == "code" =>
|
||||
{
|
||||
// don't error for valid follow-up attributes
|
||||
}
|
||||
nested_attr => throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
|
||||
diag.help("first argument of the attribute should be the diagnostic slug")
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
// Remaining attributes are optional, only `code = ".."` at the moment.
|
||||
let mut tokens = Vec::new();
|
||||
for nested_attr in nested {
|
||||
for nested_attr in nested_iter {
|
||||
let meta = match nested_attr {
|
||||
syn::NestedMeta::Meta(meta) => meta,
|
||||
_ => throw_invalid_nested_attr!(attr, &nested_attr),
|
||||
|
@ -291,28 +322,24 @@ impl SessionDiagnosticDeriveBuilder {
|
|||
|
||||
let path = meta.path();
|
||||
let nested_name = path.segments.last().unwrap().ident.to_string();
|
||||
match &meta {
|
||||
// Struct attributes are only allowed to be applied once, and the diagnostic
|
||||
// changes will be set in the initialisation code.
|
||||
Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(s), .. }) => {
|
||||
let span = s.span().unwrap();
|
||||
match nested_name.as_str() {
|
||||
"slug" => {
|
||||
self.slug.set_once((s.value(), span));
|
||||
}
|
||||
"code" => {
|
||||
self.code.set_once((s.value(), span));
|
||||
let (diag, code) = (&self.diag, &self.code.as_ref().map(|(v, _)| v));
|
||||
tokens.push(quote! {
|
||||
#diag.code(rustc_errors::DiagnosticId::Error(#code.to_string()));
|
||||
});
|
||||
}
|
||||
_ => invalid_nested_attr(attr, &nested_attr)
|
||||
.help("only `slug` and `code` are valid nested attributes")
|
||||
.emit(),
|
||||
// Struct attributes are only allowed to be applied once, and the diagnostic
|
||||
// changes will be set in the initialisation code.
|
||||
if let Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(s), .. }) = &meta {
|
||||
let span = s.span().unwrap();
|
||||
match nested_name.as_str() {
|
||||
"code" => {
|
||||
self.code.set_once((s.value(), span));
|
||||
let code = &self.code.as_ref().map(|(v, _)| v);
|
||||
tokens.push(quote! {
|
||||
#diag.code(rustc_errors::DiagnosticId::Error(#code.to_string()));
|
||||
});
|
||||
}
|
||||
_ => invalid_nested_attr(attr, &nested_attr)
|
||||
.help("only `code` is a valid nested attributes following the slug")
|
||||
.emit(),
|
||||
}
|
||||
_ => invalid_nested_attr(attr, &nested_attr).emit(),
|
||||
} else {
|
||||
invalid_nested_attr(attr, &nested_attr).emit()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -382,142 +409,215 @@ impl SessionDiagnosticDeriveBuilder {
|
|||
info: FieldInfo<'_>,
|
||||
binding: TokenStream,
|
||||
) -> Result<TokenStream, SessionDiagnosticDeriveError> {
|
||||
let meta = attr.parse_meta()?;
|
||||
match meta {
|
||||
Meta::Path(_) => self.generate_inner_field_code_path(attr, info, binding),
|
||||
Meta::List(MetaList { .. }) => self.generate_inner_field_code_list(attr, info, binding),
|
||||
_ => throw_invalid_attr!(attr, &meta),
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_inner_field_code_path(
|
||||
&mut self,
|
||||
attr: &Attribute,
|
||||
info: FieldInfo<'_>,
|
||||
binding: TokenStream,
|
||||
) -> Result<TokenStream, SessionDiagnosticDeriveError> {
|
||||
assert!(matches!(attr.parse_meta()?, Meta::Path(_)));
|
||||
let diag = &self.diag;
|
||||
|
||||
let meta = attr.parse_meta()?;
|
||||
|
||||
let ident = &attr.path.segments.last().unwrap().ident;
|
||||
let name = ident.to_string();
|
||||
let name = name.as_str();
|
||||
|
||||
let meta = attr.parse_meta()?;
|
||||
match meta {
|
||||
Meta::Path(_) => match name {
|
||||
"skip_arg" => {
|
||||
// Don't need to do anything - by virtue of the attribute existing, the
|
||||
// `set_arg` call will not be generated.
|
||||
Ok(quote! {})
|
||||
}
|
||||
"primary_span" => {
|
||||
report_error_if_not_applied_to_span(attr, &info)?;
|
||||
Ok(quote! {
|
||||
#diag.set_span(#binding);
|
||||
})
|
||||
}
|
||||
"label" => {
|
||||
report_error_if_not_applied_to_span(attr, &info)?;
|
||||
Ok(self.add_spanned_subdiagnostic(binding, ident, name))
|
||||
}
|
||||
"note" | "help" => {
|
||||
if type_matches_path(&info.ty, &["rustc_span", "Span"]) {
|
||||
Ok(self.add_spanned_subdiagnostic(binding, ident, name))
|
||||
} else if type_is_unit(&info.ty) {
|
||||
Ok(self.add_subdiagnostic(ident, name))
|
||||
} else {
|
||||
report_type_error(attr, "`Span` or `()`")?;
|
||||
}
|
||||
}
|
||||
"subdiagnostic" => Ok(quote! { #diag.subdiagnostic(#binding); }),
|
||||
_ => throw_invalid_attr!(attr, &meta, |diag| {
|
||||
diag
|
||||
.help("only `skip_arg`, `primary_span`, `label`, `note`, `help` and `subdiagnostic` are valid field attributes")
|
||||
}),
|
||||
},
|
||||
Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(ref s), .. }) => match name {
|
||||
"label" => {
|
||||
report_error_if_not_applied_to_span(attr, &info)?;
|
||||
Ok(self.add_spanned_subdiagnostic(binding, ident, &s.value()))
|
||||
}
|
||||
"note" | "help" => {
|
||||
if type_matches_path(&info.ty, &["rustc_span", "Span"]) {
|
||||
Ok(self.add_spanned_subdiagnostic(binding, ident, &s.value()))
|
||||
} else if type_is_unit(&info.ty) {
|
||||
Ok(self.add_subdiagnostic(ident, &s.value()))
|
||||
} else {
|
||||
report_type_error(attr, "`Span` or `()`")?;
|
||||
}
|
||||
}
|
||||
_ => throw_invalid_attr!(attr, &meta, |diag| {
|
||||
diag.help("only `label`, `note` and `help` are valid field attributes")
|
||||
}),
|
||||
},
|
||||
Meta::List(MetaList { ref path, ref nested, .. }) => {
|
||||
let name = path.segments.last().unwrap().ident.to_string();
|
||||
let name = name.as_ref();
|
||||
|
||||
match name {
|
||||
"suggestion" | "suggestion_short" | "suggestion_hidden"
|
||||
| "suggestion_verbose" => (),
|
||||
_ => throw_invalid_attr!(attr, &meta, |diag| {
|
||||
diag
|
||||
.help("only `suggestion{,_short,_hidden,_verbose}` are valid field attributes")
|
||||
}),
|
||||
match name {
|
||||
"skip_arg" => {
|
||||
// Don't need to do anything - by virtue of the attribute existing, the
|
||||
// `set_arg` call will not be generated.
|
||||
Ok(quote! {})
|
||||
}
|
||||
"primary_span" => {
|
||||
report_error_if_not_applied_to_span(attr, &info)?;
|
||||
Ok(quote! {
|
||||
#diag.set_span(#binding);
|
||||
})
|
||||
}
|
||||
"label" => {
|
||||
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 },
|
||||
_ => unreachable!(),
|
||||
};
|
||||
if type_matches_path(&info.ty, &["rustc_span", "Span"]) {
|
||||
Ok(self.add_spanned_subdiagnostic(binding, ident, path))
|
||||
} else if type_is_unit(&info.ty) {
|
||||
Ok(self.add_subdiagnostic(ident, path))
|
||||
} else {
|
||||
report_type_error(attr, "`Span` or `()`")?;
|
||||
}
|
||||
}
|
||||
"subdiagnostic" => Ok(quote! { #diag.subdiagnostic(#binding); }),
|
||||
_ => throw_invalid_attr!(attr, &meta, |diag| {
|
||||
diag.help(
|
||||
"only `skip_arg`, `primary_span`, `label`, `note`, `help` and `subdiagnostic` \
|
||||
are valid field attributes",
|
||||
)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
let (span_field, mut applicability) = self.span_and_applicability_of_ty(info)?;
|
||||
fn generate_inner_field_code_list(
|
||||
&mut self,
|
||||
attr: &Attribute,
|
||||
info: FieldInfo<'_>,
|
||||
binding: TokenStream,
|
||||
) -> Result<TokenStream, SessionDiagnosticDeriveError> {
|
||||
let meta = attr.parse_meta()?;
|
||||
let Meta::List(MetaList { ref path, ref nested, .. }) = meta else { unreachable!() };
|
||||
|
||||
let mut msg = None;
|
||||
let mut code = None;
|
||||
let ident = &attr.path.segments.last().unwrap().ident;
|
||||
let name = path.segments.last().unwrap().ident.to_string();
|
||||
let name = name.as_ref();
|
||||
match name {
|
||||
"suggestion" | "suggestion_short" | "suggestion_hidden" | "suggestion_verbose" => {
|
||||
return self.generate_inner_field_code_suggestion(attr, info);
|
||||
}
|
||||
"label" | "help" | "note" => (),
|
||||
_ => throw_invalid_attr!(attr, &meta, |diag| {
|
||||
diag.help(
|
||||
"only `label`, `note`, `help` or `suggestion{,_short,_hidden,_verbose}` are \
|
||||
valid field attributes",
|
||||
)
|
||||
}),
|
||||
}
|
||||
|
||||
for nested_attr in nested {
|
||||
let meta = match nested_attr {
|
||||
syn::NestedMeta::Meta(ref meta) => meta,
|
||||
syn::NestedMeta::Lit(_) => throw_invalid_nested_attr!(attr, &nested_attr),
|
||||
};
|
||||
// For `#[label(..)]`, `#[note(..)]` and `#[help(..)]`, the first nested element must be a
|
||||
// path, e.g. `#[label(typeck::label)]`.
|
||||
let mut nested_iter = nested.into_iter();
|
||||
let msg = match nested_iter.next() {
|
||||
Some(NestedMeta::Meta(Meta::Path(path))) => path.clone(),
|
||||
Some(nested_attr) => throw_invalid_nested_attr!(attr, &nested_attr),
|
||||
None => throw_invalid_attr!(attr, &meta),
|
||||
};
|
||||
|
||||
let nested_name = meta.path().segments.last().unwrap().ident.to_string();
|
||||
let nested_name = nested_name.as_str();
|
||||
match meta {
|
||||
Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(s), .. }) => {
|
||||
let span = meta.span().unwrap();
|
||||
match nested_name {
|
||||
"message" => {
|
||||
msg = Some(s.value());
|
||||
}
|
||||
"code" => {
|
||||
let formatted_str = self.build_format(&s.value(), s.span());
|
||||
code = Some(formatted_str);
|
||||
}
|
||||
"applicability" => {
|
||||
applicability = match applicability {
|
||||
Some(v) => {
|
||||
span_err(
|
||||
span,
|
||||
"applicability cannot be set in both the field and attribute"
|
||||
).emit();
|
||||
Some(v)
|
||||
}
|
||||
None => match Applicability::from_str(&s.value()) {
|
||||
Ok(v) => Some(quote! { #v }),
|
||||
Err(()) => {
|
||||
span_err(span, "invalid applicability").emit();
|
||||
None
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
_ => throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
|
||||
diag.help(
|
||||
"only `message`, `code` and `applicability` are valid field attributes",
|
||||
// None of these attributes should have anything following the slug.
|
||||
if nested_iter.next().is_some() {
|
||||
throw_invalid_attr!(attr, &meta);
|
||||
}
|
||||
|
||||
match name {
|
||||
"label" => {
|
||||
report_error_if_not_applied_to_span(attr, &info)?;
|
||||
Ok(self.add_spanned_subdiagnostic(binding, ident, msg))
|
||||
}
|
||||
"note" | "help" if type_matches_path(&info.ty, &["rustc_span", "Span"]) => {
|
||||
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 `()`")?;
|
||||
}
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
fn generate_inner_field_code_suggestion(
|
||||
&mut self,
|
||||
attr: &Attribute,
|
||||
info: FieldInfo<'_>,
|
||||
) -> Result<TokenStream, SessionDiagnosticDeriveError> {
|
||||
let diag = &self.diag;
|
||||
|
||||
let mut meta = attr.parse_meta()?;
|
||||
let Meta::List(MetaList { ref path, ref mut nested, .. }) = meta else { unreachable!() };
|
||||
|
||||
let (span_field, mut applicability) = self.span_and_applicability_of_ty(info)?;
|
||||
|
||||
let mut msg = None;
|
||||
let mut code = None;
|
||||
|
||||
let mut nested_iter = nested.into_iter().peekable();
|
||||
if let Some(nested_attr) = nested_iter.peek() {
|
||||
if let NestedMeta::Meta(Meta::Path(path)) = nested_attr {
|
||||
msg = Some(path.clone());
|
||||
}
|
||||
};
|
||||
// Move the iterator forward if a path was found (don't otherwise so that
|
||||
// code/applicability can be found or an error emitted).
|
||||
if msg.is_some() {
|
||||
let _ = nested_iter.next();
|
||||
}
|
||||
|
||||
for nested_attr in nested_iter {
|
||||
let meta = match nested_attr {
|
||||
syn::NestedMeta::Meta(ref meta) => meta,
|
||||
syn::NestedMeta::Lit(_) => throw_invalid_nested_attr!(attr, &nested_attr),
|
||||
};
|
||||
|
||||
let nested_name = meta.path().segments.last().unwrap().ident.to_string();
|
||||
let nested_name = nested_name.as_str();
|
||||
match meta {
|
||||
Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(s), .. }) => {
|
||||
let span = meta.span().unwrap();
|
||||
match nested_name {
|
||||
"code" => {
|
||||
let formatted_str = self.build_format(&s.value(), s.span());
|
||||
code = Some(formatted_str);
|
||||
}
|
||||
"applicability" => {
|
||||
applicability = match applicability {
|
||||
Some(v) => {
|
||||
span_err(
|
||||
span,
|
||||
"applicability cannot be set in both the field and \
|
||||
attribute",
|
||||
)
|
||||
}),
|
||||
.emit();
|
||||
Some(v)
|
||||
}
|
||||
None => match Applicability::from_str(&s.value()) {
|
||||
Ok(v) => Some(quote! { #v }),
|
||||
Err(()) => {
|
||||
span_err(span, "invalid applicability").emit();
|
||||
None
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
_ => throw_invalid_nested_attr!(attr, &nested_attr),
|
||||
_ => throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
|
||||
diag.help(
|
||||
"only `message`, `code` and `applicability` are valid field \
|
||||
attributes",
|
||||
)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
let applicability = applicability
|
||||
.unwrap_or_else(|| quote!(rustc_errors::Applicability::Unspecified));
|
||||
|
||||
let method = format_ident!("span_{}", name);
|
||||
|
||||
let msg = msg.as_deref().unwrap_or("suggestion");
|
||||
let msg = quote! { rustc_errors::SubdiagnosticMessage::attr(#msg) };
|
||||
let code = code.unwrap_or_else(|| quote! { String::new() });
|
||||
|
||||
Ok(quote! { #diag.#method(#span_field, #msg, #code, #applicability); })
|
||||
_ => throw_invalid_nested_attr!(attr, &nested_attr, |diag| {
|
||||
if matches!(meta, Meta::Path(_)) {
|
||||
diag.help("a diagnostic slug must be the first argument to the attribute")
|
||||
} else {
|
||||
diag
|
||||
}
|
||||
}),
|
||||
}
|
||||
_ => throw_invalid_attr!(attr, &meta),
|
||||
}
|
||||
|
||||
let applicability =
|
||||
applicability.unwrap_or_else(|| quote!(rustc_errors::Applicability::Unspecified));
|
||||
|
||||
let name = path.segments.last().unwrap().ident.to_string();
|
||||
let method = format_ident!("span_{}", name);
|
||||
|
||||
let msg = msg.unwrap_or_else(|| parse_quote! { _subdiag::suggestion });
|
||||
let msg = quote! { rustc_errors::fluent::#msg };
|
||||
let code = code.unwrap_or_else(|| quote! { String::new() });
|
||||
|
||||
Ok(quote! { #diag.#method(#span_field, #msg, #code, #applicability); })
|
||||
}
|
||||
|
||||
/// Adds a spanned subdiagnostic by generating a `diag.span_$kind` call with the current slug
|
||||
|
@ -526,24 +626,24 @@ impl SessionDiagnosticDeriveBuilder {
|
|||
&self,
|
||||
field_binding: TokenStream,
|
||||
kind: &Ident,
|
||||
fluent_attr_identifier: &str,
|
||||
fluent_attr_identifier: Path,
|
||||
) -> TokenStream {
|
||||
let diag = &self.diag;
|
||||
let fn_name = format_ident!("span_{}", kind);
|
||||
quote! {
|
||||
#diag.#fn_name(
|
||||
#field_binding,
|
||||
rustc_errors::SubdiagnosticMessage::attr(#fluent_attr_identifier)
|
||||
rustc_errors::fluent::#fluent_attr_identifier
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a subdiagnostic by generating a `diag.span_$kind` call with the current slug
|
||||
/// and `fluent_attr_identifier`.
|
||||
fn add_subdiagnostic(&self, kind: &Ident, fluent_attr_identifier: &str) -> TokenStream {
|
||||
fn add_subdiagnostic(&self, kind: &Ident, fluent_attr_identifier: Path) -> TokenStream {
|
||||
let diag = &self.diag;
|
||||
quote! {
|
||||
#diag.#kind(rustc_errors::SubdiagnosticMessage::attr(#fluent_attr_identifier));
|
||||
#diag.#kind(rustc_errors::fluent::#fluent_attr_identifier);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -569,7 +669,8 @@ impl SessionDiagnosticDeriveBuilder {
|
|||
} else {
|
||||
throw_span_err!(
|
||||
info.span.unwrap(),
|
||||
"type of field annotated with `#[suggestion(...)]` contains more than one `Span`"
|
||||
"type of field annotated with `#[suggestion(...)]` contains more \
|
||||
than one `Span`"
|
||||
);
|
||||
}
|
||||
} else if type_matches_path(elem, &["rustc_errors", "Applicability"]) {
|
||||
|
@ -578,7 +679,8 @@ impl SessionDiagnosticDeriveBuilder {
|
|||
} else {
|
||||
throw_span_err!(
|
||||
info.span.unwrap(),
|
||||
"type of field annotated with `#[suggestion(...)]` contains more than one Applicability"
|
||||
"type of field annotated with `#[suggestion(...)]` contains more \
|
||||
than one Applicability"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -595,12 +697,18 @@ impl SessionDiagnosticDeriveBuilder {
|
|||
}
|
||||
|
||||
throw_span_err!(info.span.unwrap(), "wrong types for suggestion", |diag| {
|
||||
diag.help("`#[suggestion(...)]` on a tuple field must be applied to fields of type `(Span, Applicability)`")
|
||||
diag.help(
|
||||
"`#[suggestion(...)]` on a tuple field must be applied to fields of type \
|
||||
`(Span, Applicability)`",
|
||||
)
|
||||
});
|
||||
}
|
||||
// If `ty` isn't a `Span` or `(Span, Applicability)` then emit an error.
|
||||
_ => throw_span_err!(info.span.unwrap(), "wrong field type for suggestion", |diag| {
|
||||
diag.help("`#[suggestion(...)]` should be applied to fields of type `Span` or `(Span, Applicability)`")
|
||||
diag.help(
|
||||
"`#[suggestion(...)]` should be applied to fields of type `Span` or \
|
||||
`(Span, Applicability)`",
|
||||
)
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,6 +39,19 @@ pub(crate) fn _throw_err(
|
|||
SessionDiagnosticDeriveError::ErrorHandled
|
||||
}
|
||||
|
||||
/// Helper function for printing `syn::Path` - doesn't handle arguments in paths and these are
|
||||
/// unlikely to come up much in use of the macro.
|
||||
fn path_to_string(path: &syn::Path) -> String {
|
||||
let mut out = String::new();
|
||||
for (i, segment) in path.segments.iter().enumerate() {
|
||||
if i > 0 || path.leading_colon.is_some() {
|
||||
out.push_str("::");
|
||||
}
|
||||
out.push_str(&segment.ident.to_string());
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// Returns an error diagnostic on span `span` with msg `msg`.
|
||||
pub(crate) fn span_err(span: impl MultiSpan, msg: &str) -> Diagnostic {
|
||||
Diagnostic::spanned(span, Level::Error, msg)
|
||||
|
@ -61,15 +74,13 @@ pub(crate) use throw_span_err;
|
|||
/// Returns an error diagnostic for an invalid attribute.
|
||||
pub(crate) fn invalid_attr(attr: &Attribute, meta: &Meta) -> Diagnostic {
|
||||
let span = attr.span().unwrap();
|
||||
let name = attr.path.segments.last().unwrap().ident.to_string();
|
||||
let name = name.as_str();
|
||||
|
||||
let path = path_to_string(&attr.path);
|
||||
match meta {
|
||||
Meta::Path(_) => span_err(span, &format!("`#[{}]` is not a valid attribute", name)),
|
||||
Meta::Path(_) => span_err(span, &format!("`#[{}]` is not a valid attribute", path)),
|
||||
Meta::NameValue(_) => {
|
||||
span_err(span, &format!("`#[{} = ...]` is not a valid attribute", name))
|
||||
span_err(span, &format!("`#[{} = ...]` is not a valid attribute", path))
|
||||
}
|
||||
Meta::List(_) => span_err(span, &format!("`#[{}(...)]` is not a valid attribute", name)),
|
||||
Meta::List(_) => span_err(span, &format!("`#[{}(...)]` is not a valid attribute", path)),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -101,18 +112,16 @@ pub(crate) fn invalid_nested_attr(attr: &Attribute, nested: &NestedMeta) -> Diag
|
|||
};
|
||||
|
||||
let span = meta.span().unwrap();
|
||||
let nested_name = meta.path().segments.last().unwrap().ident.to_string();
|
||||
let nested_name = nested_name.as_str();
|
||||
let path = path_to_string(meta.path());
|
||||
match meta {
|
||||
Meta::NameValue(..) => span_err(
|
||||
span,
|
||||
&format!("`#[{}({} = ...)]` is not a valid attribute", name, nested_name),
|
||||
),
|
||||
Meta::NameValue(..) => {
|
||||
span_err(span, &format!("`#[{}({} = ...)]` is not a valid attribute", name, path))
|
||||
}
|
||||
Meta::Path(..) => {
|
||||
span_err(span, &format!("`#[{}({})]` is not a valid attribute", name, nested_name))
|
||||
span_err(span, &format!("`#[{}({})]` is not a valid attribute", name, path))
|
||||
}
|
||||
Meta::List(..) => {
|
||||
span_err(span, &format!("`#[{}({}(...))]` is not a valid attribute", name, nested_name))
|
||||
span_err(span, &format!("`#[{}({}(...))]` is not a valid attribute", name, path))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -254,6 +254,17 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
|
|||
];
|
||||
|
||||
#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 label: crate::SubdiagnosticMessage =
|
||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("label"));
|
||||
pub const suggestion: crate::SubdiagnosticMessage =
|
||||
crate::SubdiagnosticMessage::FluentAttr(std::borrow::Cow::Borrowed("suggestion"));
|
||||
}
|
||||
}
|
||||
}
|
||||
.into()
|
||||
|
|
|
@ -22,14 +22,14 @@ use synstructure::Structure;
|
|||
/// # extern crate rust_middle;
|
||||
/// # use rustc_middle::ty::Ty;
|
||||
/// #[derive(SessionDiagnostic)]
|
||||
/// #[error(code = "E0505", slug = "borrowck-move-out-of-borrow")]
|
||||
/// #[error(borrowck::move_out_of_borrow, code = "E0505")]
|
||||
/// pub struct MoveOutOfBorrowError<'tcx> {
|
||||
/// pub name: Ident,
|
||||
/// pub ty: Ty<'tcx>,
|
||||
/// #[primary_span]
|
||||
/// #[label]
|
||||
/// pub span: Span,
|
||||
/// #[label = "first-borrow-label"]
|
||||
/// #[label(borrowck::first_borrow_label)]
|
||||
/// pub first_borrow_span: Span,
|
||||
/// #[suggestion(code = "{name}.clone()")]
|
||||
/// pub clone_sugg: Option<(Span, Applicability)>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue