2022-06-30 07:57:45 +00:00
|
|
|
#![deny(unused_must_use)]
|
|
|
|
|
|
|
|
use crate::diagnostics::error::{
|
2022-09-23 11:49:02 +00:00
|
|
|
invalid_nested_attr, span_err, throw_invalid_attr, throw_invalid_nested_attr, throw_span_err,
|
|
|
|
DiagnosticDeriveError,
|
2022-06-30 07:57:45 +00:00
|
|
|
};
|
|
|
|
use crate::diagnostics::utils::{
|
2022-09-23 11:49:02 +00:00
|
|
|
bind_style_of_field, build_field_mapping, report_error_if_not_applied_to_span,
|
|
|
|
report_type_error, should_generate_set_arg, type_is_unit, type_matches_path, FieldInfo,
|
|
|
|
FieldInnerTy, FieldMap, HasFieldMap, SetOnce, SpannedOption, SubdiagnosticKind,
|
2022-06-30 07:57:45 +00:00
|
|
|
};
|
2022-07-11 17:46:24 +00:00
|
|
|
use proc_macro2::{Ident, Span, TokenStream};
|
2022-06-30 07:57:45 +00:00
|
|
|
use quote::{format_ident, quote};
|
|
|
|
use syn::{
|
2022-09-23 11:49:02 +00:00
|
|
|
parse_quote, spanned::Spanned, Attribute, Meta, MetaList, MetaNameValue, NestedMeta, Path, Type,
|
2022-06-30 07:57:45 +00:00
|
|
|
};
|
2022-09-23 11:49:02 +00:00
|
|
|
use synstructure::{BindingInfo, Structure, VariantInfo};
|
2022-06-30 07:57:45 +00:00
|
|
|
|
2022-08-19 13:02:10 +00:00
|
|
|
/// What kind of diagnostic is being derived - a fatal/error/warning or a lint?
|
2022-10-03 13:24:17 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2022-06-30 07:57:45 +00:00
|
|
|
pub(crate) enum DiagnosticDeriveKind {
|
2022-10-03 13:24:17 +00:00
|
|
|
Diagnostic { handler: syn::Ident },
|
2022-08-19 13:02:10 +00:00
|
|
|
LintDiagnostic,
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 11:49:02 +00:00
|
|
|
/// Tracks persistent information required for the entire type when building up individual calls to
|
|
|
|
/// diagnostic methods for generated diagnostic derives - both `Diagnostic` for
|
|
|
|
/// fatal/errors/warnings and `LintDiagnostic` for lints.
|
2022-06-30 07:57:45 +00:00
|
|
|
pub(crate) struct DiagnosticDeriveBuilder {
|
|
|
|
/// The identifier to use for the generated `DiagnosticBuilder` instance.
|
|
|
|
pub diag: syn::Ident,
|
2022-09-23 11:49:02 +00:00
|
|
|
/// Kind of diagnostic that should be derived.
|
|
|
|
pub kind: DiagnosticDeriveKind,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Tracks persistent information required for a specific variant when building up individual calls
|
|
|
|
/// to diagnostic methods for generated diagnostic derives - both `Diagnostic` for
|
|
|
|
/// fatal/errors/warnings and `LintDiagnostic` for lints.
|
|
|
|
pub(crate) struct DiagnosticDeriveVariantBuilder<'parent> {
|
|
|
|
/// The parent builder for the entire type.
|
|
|
|
pub parent: &'parent DiagnosticDeriveBuilder,
|
|
|
|
|
|
|
|
/// Span of the struct or the enum variant.
|
|
|
|
pub span: proc_macro::Span,
|
2022-06-30 07:57:45 +00:00
|
|
|
|
|
|
|
/// Store a map of field name to its corresponding field. This is built on construction of the
|
|
|
|
/// derive builder.
|
2022-09-23 11:49:02 +00:00
|
|
|
pub field_map: FieldMap,
|
2022-06-30 07:57:45 +00:00
|
|
|
|
|
|
|
/// Slug is a mandatory part of the struct attribute as corresponds to the Fluent message that
|
|
|
|
/// has the actual diagnostic message.
|
2022-09-11 16:30:18 +00:00
|
|
|
pub slug: SpannedOption<Path>,
|
2022-06-30 07:57:45 +00:00
|
|
|
/// Error codes are a optional part of the struct attribute - this is only set to detect
|
|
|
|
/// multiple specifications.
|
2022-09-14 15:19:40 +00:00
|
|
|
pub code: SpannedOption<()>,
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 11:49:02 +00:00
|
|
|
impl<'a> HasFieldMap for DiagnosticDeriveVariantBuilder<'a> {
|
2022-06-30 07:57:45 +00:00
|
|
|
fn get_field_binding(&self, field: &String) -> Option<&TokenStream> {
|
2022-09-23 11:49:02 +00:00
|
|
|
self.field_map.get(field)
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiagnosticDeriveBuilder {
|
2022-09-23 11:49:02 +00:00
|
|
|
/// Call `f` for the struct or for each variant of the enum, returning a `TokenStream` with the
|
|
|
|
/// tokens from `f` wrapped in an `match` expression. Emits errors for use of derive on unions
|
|
|
|
/// or attributes on the type itself when input is an enum.
|
|
|
|
pub fn each_variant<'s, F>(&mut self, structure: &mut Structure<'s>, f: F) -> TokenStream
|
|
|
|
where
|
|
|
|
F: for<'a, 'v> Fn(DiagnosticDeriveVariantBuilder<'a>, &VariantInfo<'v>) -> TokenStream,
|
|
|
|
{
|
2022-06-30 07:57:45 +00:00
|
|
|
let ast = structure.ast();
|
2022-09-23 11:49:02 +00:00
|
|
|
let span = ast.span().unwrap();
|
|
|
|
match ast.data {
|
|
|
|
syn::Data::Struct(..) | syn::Data::Enum(..) => (),
|
|
|
|
syn::Data::Union(..) => {
|
|
|
|
span_err(span, "diagnostic derives can only be used on structs and enums");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if matches!(ast.data, syn::Data::Enum(..)) {
|
|
|
|
for attr in &ast.attrs {
|
|
|
|
span_err(
|
|
|
|
attr.span().unwrap(),
|
|
|
|
"unsupported type attribute for diagnostic derive enum",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for variant in structure.variants_mut() {
|
|
|
|
// First, change the binding style of each field based on the code that will be
|
|
|
|
// generated for the field - e.g. `set_arg` calls needs by-move bindings, whereas
|
|
|
|
// `set_primary_span` only needs by-ref.
|
|
|
|
variant.bind_with(|bi| bind_style_of_field(bi.ast()).0);
|
|
|
|
|
|
|
|
// Then, perform a stable sort on bindings which generates code for by-ref bindings
|
|
|
|
// before code generated for by-move bindings. Any code generated for the by-ref
|
|
|
|
// bindings which creates a reference to the by-move fields will happen before the
|
|
|
|
// by-move bindings move those fields and make them inaccessible.
|
|
|
|
variant.bindings_mut().sort_by_cached_key(|bi| bind_style_of_field(bi.ast()));
|
|
|
|
}
|
|
|
|
|
|
|
|
let variants = structure.each_variant(|variant| {
|
|
|
|
let span = match structure.ast().data {
|
|
|
|
syn::Data::Struct(..) => span,
|
|
|
|
// There isn't a good way to get the span of the variant, so the variant's
|
|
|
|
// name will need to do.
|
|
|
|
_ => variant.ast().ident.span().unwrap(),
|
|
|
|
};
|
|
|
|
let builder = DiagnosticDeriveVariantBuilder {
|
|
|
|
parent: &self,
|
|
|
|
span,
|
|
|
|
field_map: build_field_mapping(variant),
|
|
|
|
slug: None,
|
|
|
|
code: None,
|
|
|
|
};
|
|
|
|
f(builder, variant)
|
|
|
|
});
|
|
|
|
|
|
|
|
quote! {
|
|
|
|
match self {
|
|
|
|
#variants
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> DiagnosticDeriveVariantBuilder<'a> {
|
|
|
|
/// Generates calls to `code` and similar functions based on the attributes on the type or
|
|
|
|
/// variant.
|
|
|
|
pub fn preamble<'s>(&mut self, variant: &VariantInfo<'s>) -> TokenStream {
|
|
|
|
let ast = variant.ast();
|
2022-06-30 07:57:45 +00:00
|
|
|
let attrs = &ast.attrs;
|
|
|
|
let preamble = attrs.iter().map(|attr| {
|
|
|
|
self.generate_structure_code_for_attr(attr).unwrap_or_else(|v| v.to_compile_error())
|
|
|
|
});
|
|
|
|
|
|
|
|
quote! {
|
|
|
|
#(#preamble)*;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 11:49:02 +00:00
|
|
|
/// Generates calls to `span_label` and similar functions based on the attributes on fields or
|
|
|
|
/// calls to `set_arg` when no attributes are present.
|
|
|
|
///
|
|
|
|
/// Expects use of `Self::each_variant` which will have sorted bindings so that by-ref bindings
|
|
|
|
/// (which may create references to by-move bindings) have their code generated first -
|
|
|
|
/// necessary as code for suggestions uses formatting machinery and the value of other fields
|
|
|
|
/// (any given field can be referenced multiple times, so must be accessed through a borrow);
|
|
|
|
/// and when passing fields to `add_subdiagnostic` or `set_arg` for Fluent, fields must be
|
|
|
|
/// accessed by-move.
|
|
|
|
pub fn body<'s>(&mut self, variant: &VariantInfo<'s>) -> TokenStream {
|
|
|
|
let mut body = quote! {};
|
|
|
|
for binding in variant.bindings() {
|
|
|
|
body.extend(self.generate_field_attrs_code(binding));
|
|
|
|
}
|
|
|
|
body
|
2022-07-11 16:15:31 +00:00
|
|
|
}
|
|
|
|
|
2022-09-23 11:49:02 +00:00
|
|
|
/// Parse a `SubdiagnosticKind` from an `Attribute`.
|
2022-09-14 15:19:40 +00:00
|
|
|
fn parse_subdiag_attribute(
|
|
|
|
&self,
|
|
|
|
attr: &Attribute,
|
|
|
|
) -> Result<(SubdiagnosticKind, Path), DiagnosticDeriveError> {
|
|
|
|
let (subdiag, slug) = SubdiagnosticKind::from_attr(attr, self)?;
|
|
|
|
|
|
|
|
if let SubdiagnosticKind::MultipartSuggestion { .. } = subdiag {
|
|
|
|
let meta = attr.parse_meta()?;
|
|
|
|
throw_invalid_attr!(attr, &meta, |diag| diag
|
|
|
|
.help("consider creating a `Subdiagnostic` instead"));
|
|
|
|
}
|
|
|
|
|
|
|
|
let slug = slug.unwrap_or_else(|| match subdiag {
|
|
|
|
SubdiagnosticKind::Label => parse_quote! { _subdiag::label },
|
|
|
|
SubdiagnosticKind::Note => parse_quote! { _subdiag::note },
|
|
|
|
SubdiagnosticKind::Help => parse_quote! { _subdiag::help },
|
|
|
|
SubdiagnosticKind::Warn => parse_quote! { _subdiag::warn },
|
|
|
|
SubdiagnosticKind::Suggestion { .. } => parse_quote! { _subdiag::suggestion },
|
|
|
|
SubdiagnosticKind::MultipartSuggestion { .. } => unreachable!(),
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok((subdiag, slug))
|
|
|
|
}
|
|
|
|
|
2022-06-30 07:57:45 +00:00
|
|
|
/// Establishes state in the `DiagnosticDeriveBuilder` resulting from the struct
|
2022-08-19 13:02:10 +00:00
|
|
|
/// attributes like `#[diag(..)]`, such as the slug and error code. Generates
|
2022-06-30 07:57:45 +00:00
|
|
|
/// diagnostic builder calls for setting error code and creating note/help messages.
|
|
|
|
fn generate_structure_code_for_attr(
|
|
|
|
&mut self,
|
|
|
|
attr: &Attribute,
|
|
|
|
) -> Result<TokenStream, DiagnosticDeriveError> {
|
2022-09-23 11:49:02 +00:00
|
|
|
let diag = &self.parent.diag;
|
2022-06-30 07:57:45 +00:00
|
|
|
|
|
|
|
let name = attr.path.segments.last().unwrap().ident.to_string();
|
|
|
|
let name = name.as_str();
|
|
|
|
let meta = attr.parse_meta()?;
|
|
|
|
|
2022-09-14 15:19:40 +00:00
|
|
|
if name == "diag" {
|
|
|
|
let Meta::List(MetaList { ref nested, .. }) = meta else {
|
|
|
|
throw_invalid_attr!(
|
|
|
|
attr,
|
|
|
|
&meta
|
|
|
|
);
|
|
|
|
};
|
2022-06-30 07:57:45 +00:00
|
|
|
|
2022-09-14 15:19:40 +00:00
|
|
|
let mut nested_iter = nested.into_iter().peekable();
|
2022-06-30 07:57:45 +00:00
|
|
|
|
2022-09-14 15:19:40 +00:00
|
|
|
match nested_iter.peek() {
|
|
|
|
Some(NestedMeta::Meta(Meta::Path(slug))) => {
|
|
|
|
self.slug.set_once(slug.clone(), slug.span().unwrap());
|
|
|
|
nested_iter.next();
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
2022-09-14 15:19:40 +00:00
|
|
|
Some(NestedMeta::Meta(Meta::NameValue { .. })) => {}
|
|
|
|
Some(nested_attr) => throw_invalid_nested_attr!(attr, &nested_attr, |diag| diag
|
|
|
|
.help("a diagnostic slug is required as the first argument")),
|
|
|
|
None => throw_invalid_attr!(attr, &meta, |diag| diag
|
|
|
|
.help("a diagnostic slug is required as the first argument")),
|
2022-06-30 07:57:45 +00:00
|
|
|
};
|
|
|
|
|
2022-09-14 15:19:40 +00:00
|
|
|
// Remaining attributes are optional, only `code = ".."` at the moment.
|
|
|
|
let mut tokens = TokenStream::new();
|
|
|
|
for nested_attr in nested_iter {
|
|
|
|
let (value, path) = match nested_attr {
|
|
|
|
NestedMeta::Meta(Meta::NameValue(MetaNameValue {
|
|
|
|
lit: syn::Lit::Str(value),
|
|
|
|
path,
|
|
|
|
..
|
|
|
|
})) => (value, path),
|
|
|
|
NestedMeta::Meta(Meta::Path(_)) => {
|
|
|
|
invalid_nested_attr(attr, &nested_attr)
|
|
|
|
.help("diagnostic slug must be the first argument")
|
|
|
|
.emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
invalid_nested_attr(attr, &nested_attr).emit();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2022-06-30 07:57:45 +00:00
|
|
|
|
2022-09-14 15:19:40 +00:00
|
|
|
let nested_name = path.segments.last().unwrap().ident.to_string();
|
|
|
|
// Struct attributes are only allowed to be applied once, and the diagnostic
|
|
|
|
// changes will be set in the initialisation code.
|
|
|
|
let span = value.span().unwrap();
|
2022-06-30 07:57:45 +00:00
|
|
|
match nested_name.as_str() {
|
|
|
|
"code" => {
|
2022-09-14 15:19:40 +00:00
|
|
|
self.code.set_once((), span);
|
|
|
|
|
|
|
|
let code = value.value();
|
|
|
|
tokens.extend(quote! {
|
2022-06-30 07:57:45 +00:00
|
|
|
#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(),
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 15:19:40 +00:00
|
|
|
return Ok(tokens);
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-14 15:19:40 +00:00
|
|
|
let (subdiag, slug) = self.parse_subdiag_attribute(attr)?;
|
|
|
|
let fn_ident = format_ident!("{}", subdiag);
|
|
|
|
match subdiag {
|
|
|
|
SubdiagnosticKind::Note | SubdiagnosticKind::Help | SubdiagnosticKind::Warn => {
|
|
|
|
Ok(self.add_subdiagnostic(&fn_ident, slug))
|
|
|
|
}
|
|
|
|
SubdiagnosticKind::Label | SubdiagnosticKind::Suggestion { .. } => {
|
|
|
|
throw_invalid_attr!(attr, &meta, |diag| diag
|
|
|
|
.help("`#[label]` and `#[suggestion]` can only be applied to fields"));
|
|
|
|
}
|
|
|
|
SubdiagnosticKind::MultipartSuggestion { .. } => unreachable!(),
|
|
|
|
}
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_field_attrs_code(&mut self, binding_info: &BindingInfo<'_>) -> TokenStream {
|
|
|
|
let field = binding_info.ast();
|
|
|
|
let field_binding = &binding_info.binding;
|
|
|
|
|
2022-09-23 11:49:02 +00:00
|
|
|
if should_generate_set_arg(&field) {
|
|
|
|
let diag = &self.parent.diag;
|
2022-06-30 07:57:45 +00:00
|
|
|
let ident = field.ident.as_ref().unwrap();
|
2022-09-14 18:12:22 +00:00
|
|
|
// strip `r#` prefix, if present
|
|
|
|
let ident = format_ident!("{}", ident);
|
2022-07-11 16:15:31 +00:00
|
|
|
return quote! {
|
2022-06-30 07:57:45 +00:00
|
|
|
#diag.set_arg(
|
|
|
|
stringify!(#ident),
|
|
|
|
#field_binding
|
|
|
|
);
|
2022-07-11 16:15:31 +00:00
|
|
|
};
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
2022-07-11 16:15:31 +00:00
|
|
|
|
2022-09-23 11:49:02 +00:00
|
|
|
let needs_move = bind_style_of_field(&field).is_move();
|
2022-07-11 16:15:31 +00:00
|
|
|
let inner_ty = FieldInnerTy::from_type(&field.ty);
|
|
|
|
|
|
|
|
field
|
|
|
|
.attrs
|
|
|
|
.iter()
|
|
|
|
.map(move |attr| {
|
|
|
|
let name = attr.path.segments.last().unwrap().ident.to_string();
|
|
|
|
let needs_clone =
|
|
|
|
name == "primary_span" && matches!(inner_ty, FieldInnerTy::Vec(_));
|
|
|
|
let (binding, needs_destructure) = if needs_clone {
|
|
|
|
// `primary_span` can accept a `Vec<Span>` so don't destructure that.
|
|
|
|
(quote! { #field_binding.clone() }, false)
|
|
|
|
} else if needs_move {
|
|
|
|
(quote! { #field_binding }, true)
|
|
|
|
} else {
|
|
|
|
(quote! { *#field_binding }, true)
|
|
|
|
};
|
|
|
|
|
|
|
|
let generated_code = self
|
|
|
|
.generate_inner_field_code(
|
|
|
|
attr,
|
|
|
|
FieldInfo {
|
|
|
|
binding: binding_info,
|
|
|
|
ty: inner_ty.inner_type().unwrap_or(&field.ty),
|
|
|
|
span: &field.span(),
|
|
|
|
},
|
|
|
|
binding,
|
|
|
|
)
|
|
|
|
.unwrap_or_else(|v| v.to_compile_error());
|
|
|
|
|
|
|
|
if needs_destructure {
|
|
|
|
inner_ty.with(field_binding, generated_code)
|
|
|
|
} else {
|
|
|
|
generated_code
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_inner_field_code(
|
|
|
|
&mut self,
|
|
|
|
attr: &Attribute,
|
|
|
|
info: FieldInfo<'_>,
|
|
|
|
binding: TokenStream,
|
|
|
|
) -> Result<TokenStream, DiagnosticDeriveError> {
|
2022-09-23 11:49:02 +00:00
|
|
|
let diag = &self.parent.diag;
|
2022-06-30 07:57:45 +00:00
|
|
|
let meta = attr.parse_meta()?;
|
|
|
|
|
2022-10-03 13:24:17 +00:00
|
|
|
let ident = &attr.path.segments.last().unwrap().ident;
|
|
|
|
let name = ident.to_string();
|
|
|
|
match (&meta, name.as_str()) {
|
|
|
|
// Don't need to do anything - by virtue of the attribute existing, the
|
|
|
|
// `set_arg` call will not be generated.
|
|
|
|
(Meta::Path(_), "skip_arg") => return Ok(quote! {}),
|
|
|
|
(Meta::Path(_), "primary_span") => {
|
|
|
|
match self.parent.kind {
|
|
|
|
DiagnosticDeriveKind::Diagnostic { .. } => {
|
2022-08-19 13:04:34 +00:00
|
|
|
report_error_if_not_applied_to_span(attr, &info)?;
|
2022-08-19 13:02:10 +00:00
|
|
|
|
2022-09-14 15:19:40 +00:00
|
|
|
return Ok(quote! {
|
2022-08-19 13:04:34 +00:00
|
|
|
#diag.set_span(#binding);
|
2022-09-14 15:19:40 +00:00
|
|
|
});
|
2022-08-19 13:04:34 +00:00
|
|
|
}
|
|
|
|
DiagnosticDeriveKind::LintDiagnostic => {
|
|
|
|
throw_invalid_attr!(attr, &meta, |diag| {
|
|
|
|
diag.help("the `primary_span` field attribute is not valid for lint diagnostics")
|
|
|
|
})
|
|
|
|
}
|
2022-10-03 13:24:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
(Meta::Path(_), "subdiagnostic") => {
|
|
|
|
return Ok(quote! { #diag.subdiagnostic(#binding); });
|
|
|
|
}
|
|
|
|
(Meta::NameValue(_), "subdiagnostic") => {
|
|
|
|
throw_invalid_attr!(attr, &meta, |diag| {
|
|
|
|
diag.help("`eager` is the only supported nested attribute for `subdiagnostic`")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
(Meta::List(MetaList { ref nested, .. }), "subdiagnostic") => {
|
|
|
|
if nested.len() != 1 {
|
|
|
|
throw_invalid_attr!(attr, &meta, |diag| {
|
|
|
|
diag.help(
|
|
|
|
"`eager` is the only supported nested attribute for `subdiagnostic`",
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let handler = match &self.parent.kind {
|
|
|
|
DiagnosticDeriveKind::Diagnostic { handler } => handler,
|
|
|
|
DiagnosticDeriveKind::LintDiagnostic => {
|
|
|
|
throw_invalid_attr!(attr, &meta, |diag| {
|
|
|
|
diag.help("eager subdiagnostics are not supported on lints")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let nested_attr = nested.first().expect("pop failed for single element list");
|
|
|
|
match nested_attr {
|
|
|
|
NestedMeta::Meta(meta @ Meta::Path(_))
|
|
|
|
if meta.path().segments.last().unwrap().ident.to_string().as_str()
|
|
|
|
== "eager" =>
|
|
|
|
{
|
|
|
|
return Ok(quote! { #diag.eager_subdiagnostic(#handler, #binding); });
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
throw_invalid_nested_attr!(attr, nested_attr, |diag| {
|
|
|
|
diag.help("`eager` is the only supported nested attribute for `subdiagnostic`")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
2022-10-03 13:24:17 +00:00
|
|
|
_ => (),
|
2022-09-14 15:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let (subdiag, slug) = self.parse_subdiag_attribute(attr)?;
|
|
|
|
|
|
|
|
let fn_ident = format_ident!("{}", subdiag);
|
|
|
|
match subdiag {
|
|
|
|
SubdiagnosticKind::Label => {
|
2022-06-30 07:57:45 +00:00
|
|
|
report_error_if_not_applied_to_span(attr, &info)?;
|
2022-09-14 15:19:40 +00:00
|
|
|
Ok(self.add_spanned_subdiagnostic(binding, &fn_ident, slug))
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
2022-09-14 15:19:40 +00:00
|
|
|
SubdiagnosticKind::Note | SubdiagnosticKind::Help | SubdiagnosticKind::Warn => {
|
2022-06-30 07:57:45 +00:00
|
|
|
if type_matches_path(&info.ty, &["rustc_span", "Span"]) {
|
2022-09-14 15:19:40 +00:00
|
|
|
Ok(self.add_spanned_subdiagnostic(binding, &fn_ident, slug))
|
2022-06-30 07:57:45 +00:00
|
|
|
} else if type_is_unit(&info.ty) {
|
2022-09-14 15:19:40 +00:00
|
|
|
Ok(self.add_subdiagnostic(&fn_ident, slug))
|
2022-06-30 07:57:45 +00:00
|
|
|
} else {
|
|
|
|
report_type_error(attr, "`Span` or `()`")?
|
|
|
|
}
|
|
|
|
}
|
2022-09-14 15:19:40 +00:00
|
|
|
SubdiagnosticKind::Suggestion {
|
|
|
|
suggestion_kind,
|
|
|
|
applicability: static_applicability,
|
|
|
|
code,
|
|
|
|
} => {
|
|
|
|
let (span_field, mut applicability) = self.span_and_applicability_of_ty(info)?;
|
|
|
|
|
|
|
|
if let Some((static_applicability, span)) = static_applicability {
|
|
|
|
applicability.set_once(quote! { #static_applicability }, span);
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
2022-09-14 15:19:40 +00:00
|
|
|
|
|
|
|
let applicability = applicability
|
|
|
|
.value()
|
|
|
|
.unwrap_or_else(|| quote! { rustc_errors::Applicability::Unspecified });
|
|
|
|
let style = suggestion_kind.to_suggestion_style();
|
|
|
|
|
|
|
|
Ok(quote! {
|
|
|
|
#diag.span_suggestion_with_style(
|
|
|
|
#span_field,
|
|
|
|
rustc_errors::fluent::#slug,
|
|
|
|
#code,
|
|
|
|
#applicability,
|
|
|
|
#style
|
|
|
|
);
|
|
|
|
})
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
2022-09-14 15:19:40 +00:00
|
|
|
SubdiagnosticKind::MultipartSuggestion { .. } => unreachable!(),
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Adds a spanned subdiagnostic by generating a `diag.span_$kind` call with the current slug
|
|
|
|
/// and `fluent_attr_identifier`.
|
|
|
|
fn add_spanned_subdiagnostic(
|
|
|
|
&self,
|
|
|
|
field_binding: TokenStream,
|
|
|
|
kind: &Ident,
|
|
|
|
fluent_attr_identifier: Path,
|
|
|
|
) -> TokenStream {
|
2022-09-23 11:49:02 +00:00
|
|
|
let diag = &self.parent.diag;
|
2022-06-30 07:57:45 +00:00
|
|
|
let fn_name = format_ident!("span_{}", kind);
|
|
|
|
quote! {
|
|
|
|
#diag.#fn_name(
|
|
|
|
#field_binding,
|
|
|
|
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: Path) -> TokenStream {
|
2022-09-23 11:49:02 +00:00
|
|
|
let diag = &self.parent.diag;
|
2022-06-30 07:57:45 +00:00
|
|
|
quote! {
|
|
|
|
#diag.#kind(rustc_errors::fluent::#fluent_attr_identifier);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn span_and_applicability_of_ty(
|
|
|
|
&self,
|
|
|
|
info: FieldInfo<'_>,
|
2022-09-11 16:30:18 +00:00
|
|
|
) -> Result<(TokenStream, SpannedOption<TokenStream>), DiagnosticDeriveError> {
|
2022-06-30 07:57:45 +00:00
|
|
|
match &info.ty {
|
|
|
|
// If `ty` is `Span` w/out applicability, then use `Applicability::Unspecified`.
|
|
|
|
ty @ Type::Path(..) if type_matches_path(ty, &["rustc_span", "Span"]) => {
|
|
|
|
let binding = &info.binding.binding;
|
|
|
|
Ok((quote!(*#binding), None))
|
|
|
|
}
|
|
|
|
// If `ty` is `(Span, Applicability)` then return tokens accessing those.
|
|
|
|
Type::Tuple(tup) => {
|
|
|
|
let mut span_idx = None;
|
|
|
|
let mut applicability_idx = None;
|
|
|
|
|
2022-09-10 12:43:07 +00:00
|
|
|
fn type_err(span: &Span) -> Result<!, DiagnosticDeriveError> {
|
|
|
|
span_err(span.unwrap(), "wrong types for suggestion")
|
|
|
|
.help(
|
|
|
|
"`#[suggestion(...)]` on a tuple field must be applied to fields \
|
|
|
|
of type `(Span, Applicability)`",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
Err(DiagnosticDeriveError::ErrorHandled)
|
|
|
|
}
|
|
|
|
|
2022-06-30 07:57:45 +00:00
|
|
|
for (idx, elem) in tup.elems.iter().enumerate() {
|
|
|
|
if type_matches_path(elem, &["rustc_span", "Span"]) {
|
2022-09-11 16:30:18 +00:00
|
|
|
span_idx.set_once(syn::Index::from(idx), elem.span().unwrap());
|
2022-06-30 07:57:45 +00:00
|
|
|
} else if type_matches_path(elem, &["rustc_errors", "Applicability"]) {
|
2022-09-11 16:30:18 +00:00
|
|
|
applicability_idx.set_once(syn::Index::from(idx), elem.span().unwrap());
|
2022-09-10 12:43:07 +00:00
|
|
|
} else {
|
|
|
|
type_err(&elem.span())?;
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-10 12:43:07 +00:00
|
|
|
let Some((span_idx, _)) = span_idx else {
|
|
|
|
type_err(&tup.span())?;
|
|
|
|
};
|
2022-09-10 12:48:01 +00:00
|
|
|
let Some((applicability_idx, applicability_span)) = applicability_idx else {
|
2022-09-10 12:43:07 +00:00
|
|
|
type_err(&tup.span())?;
|
|
|
|
};
|
|
|
|
let binding = &info.binding.binding;
|
|
|
|
let span = quote!(#binding.#span_idx);
|
|
|
|
let applicability = quote!(#binding.#applicability_idx);
|
2022-06-30 07:57:45 +00:00
|
|
|
|
2022-09-10 12:48:01 +00:00
|
|
|
Ok((span, Some((applicability, applicability_span))))
|
2022-06-30 07:57:45 +00:00
|
|
|
}
|
|
|
|
// 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)`",
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|