use std::collections::HashMap;
use std::str::FromStr;
use syn::{spanned::Spanned, Attribute, Meta, MetaList, MetaNameValue, Type};
-use synstructure::Structure;
+use synstructure::{BindingInfo, Structure};
/// The central struct for constructing the `into_diagnostic` method from an annotated struct.
pub(crate) struct SessionDiagnosticDerive<'a> {
}
};
+ // Keep track of which fields are subdiagnostics or have no attributes.
+ let mut subdiagnostics_or_empty = std::collections::HashSet::new();
+
// Generates calls to `span_label` and similar functions based on the attributes
// on fields. Code for suggestions uses formatting machinery and the value of
// other fields - because any given field can be referenced multiple times, it
- // should be accessed through a borrow. When passing fields to `set_arg` (which
- // happens below) for Fluent, we want to move the data, so that has to happen
- // in a separate pass over the fields.
- let attrs = structure.each(|field_binding| {
- let field = field_binding.ast();
- let result = field.attrs.iter().map(|attr| {
- builder
- .generate_field_attr_code(
- attr,
- FieldInfo {
- vis: &field.vis,
- binding: field_binding,
- ty: &field.ty,
- span: &field.span(),
- },
- )
- .unwrap_or_else(|v| v.to_compile_error())
- });
-
- quote! { #(#result);* }
- });
+ // should be accessed through a borrow. When passing fields to `add_subdiagnostic`
+ // or `set_arg` (which happens below) for Fluent, we want to move the data, so that
+ // has to happen in a separate pass over the fields.
+ let attrs = structure
+ .clone()
+ .filter(|field_binding| {
+ let attrs = &field_binding.ast().attrs;
+
+ (!attrs.is_empty()
+ && attrs.iter().all(|attr| {
+ "subdiagnostic"
+ != attr.path.segments.last().unwrap().ident.to_string()
+ }))
+ || {
+ subdiagnostics_or_empty.insert(field_binding.binding.clone());
+ false
+ }
+ })
+ .each(|field_binding| builder.generate_field_attrs_code(field_binding));
- // When generating `set_arg` calls, move data rather than borrow it to avoid
- // requiring clones - this must therefore be the last use of each field (for
- // example, any formatting machinery that might refer to a field should be
- // generated already).
structure.bind_with(|_| synstructure::BindStyle::Move);
- let args = structure.each(|field_binding| {
- let field = field_binding.ast();
- // When a field has attributes like `#[label]` or `#[note]` then it doesn't
- // need to be passed as an argument to the diagnostic. But when a field has no
- // attributes then it must be passed as an argument to the diagnostic so that
- // it can be referred to by Fluent messages.
- if field.attrs.is_empty() {
- let diag = &builder.diag;
- let ident = field_binding.ast().ident.as_ref().unwrap();
- quote! {
- #diag.set_arg(
- stringify!(#ident),
- #field_binding
- );
- }
- } else {
- quote! {}
- }
- });
+ // When a field has attributes like `#[label]` or `#[note]` then it doesn't
+ // need to be passed as an argument to the diagnostic. But when a field has no
+ // attributes or a `#[subdiagnostic]` attribute then it must be passed as an
+ // argument to the diagnostic so that it can be referred to by Fluent messages.
+ let args = structure
+ .filter(|field_binding| {
+ subdiagnostics_or_empty.contains(&field_binding.binding)
+ })
+ .each(|field_binding| builder.generate_field_attrs_code(field_binding));
let span = ast.span().unwrap();
let (diag, sess) = (&builder.diag, &builder.sess);
(Some((SessionDiagnosticKind::Error, _)), Some((slug, _))) => {
quote! {
let mut #diag = #sess.struct_err(
- rustc_errors::DiagnosticMessage::fluent(#slug),
+ rustc_errors::DiagnosticMessage::new(#slug),
);
}
}
(Some((SessionDiagnosticKind::Warn, _)), Some((slug, _))) => {
quote! {
let mut #diag = #sess.struct_warn(
- rustc_errors::DiagnosticMessage::fluent(#slug),
+ rustc_errors::DiagnosticMessage::new(#slug),
);
}
}
if matches!(name, "help" | "note") && matches!(meta, Meta::Path(_) | Meta::NameValue(_)) {
let diag = &self.diag;
- let slug = match &self.slug {
- Some((slug, _)) => slug.as_str(),
- None => throw_span_err!(
- span,
- &format!(
- "`#[{}{}]` must come after `#[error(..)]` or `#[warn(..)]`",
- name,
- match meta {
- Meta::Path(_) => "",
- Meta::NameValue(_) => " = ...",
- _ => unreachable!(),
- }
- )
- ),
- };
let id = match meta {
Meta::Path(..) => quote! { #name },
Meta::NameValue(MetaNameValue { lit: syn::Lit::Str(s), .. }) => {
let fn_name = proc_macro2::Ident::new(name, attr.span());
return Ok(quote! {
- #diag.#fn_name(rustc_errors::DiagnosticMessage::fluent_attr(#slug, #id));
+ #diag.#fn_name(rustc_errors::SubdiagnosticMessage::attr(#id));
});
}
Ok(tokens.drain(..).collect())
}
- fn generate_field_attr_code(
- &mut self,
- attr: &syn::Attribute,
- info: FieldInfo<'_>,
- ) -> Result<TokenStream, SessionDiagnosticDeriveError> {
- let field_binding = &info.binding.binding;
-
- let inner_ty = FieldInnerTy::from_type(&info.ty);
- let name = attr.path.segments.last().unwrap().ident.to_string();
- let (binding, needs_destructure) = match (name.as_str(), &inner_ty) {
- // `primary_span` can accept a `Vec<Span>` so don't destructure that.
- ("primary_span", FieldInnerTy::Vec(_)) => (quote! { #field_binding.clone() }, false),
- _ => (quote! { *#field_binding }, true),
- };
+ fn generate_field_attrs_code(&mut self, binding_info: &BindingInfo<'_>) -> TokenStream {
+ let field = binding_info.ast();
+ let field_binding = &binding_info.binding;
- let generated_code = self.generate_inner_field_code(
- attr,
- FieldInfo {
- vis: info.vis,
- binding: info.binding,
- ty: inner_ty.inner_type().unwrap_or(&info.ty),
- span: info.span,
- },
- binding,
- )?;
+ let inner_ty = FieldInnerTy::from_type(&field.ty);
- if needs_destructure {
- Ok(inner_ty.with(field_binding, generated_code))
+ // When generating `set_arg` or `add_subdiagnostic` calls, move data rather than
+ // borrow it to avoid requiring clones - this must therefore be the last use of
+ // each field (for example, any formatting machinery that might refer to a field
+ // should be generated already).
+ if field.attrs.is_empty() {
+ let diag = &self.diag;
+ let ident = field.ident.as_ref().unwrap();
+ quote! {
+ #diag.set_arg(
+ stringify!(#ident),
+ #field_binding
+ );
+ }
} else {
- Ok(generated_code)
+ field
+ .attrs
+ .iter()
+ .map(move |attr| {
+ let name = attr.path.segments.last().unwrap().ident.to_string();
+ let (binding, needs_destructure) = match (name.as_str(), &inner_ty) {
+ // `primary_span` can accept a `Vec<Span>` so don't destructure that.
+ ("primary_span", FieldInnerTy::Vec(_)) => {
+ (quote! { #field_binding.clone() }, false)
+ }
+ // `subdiagnostics` are not derefed because they are bound by value.
+ ("subdiagnostic", _) => (quote! { #field_binding }, true),
+ _ => (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()
}
}
let method = format_ident!("span_{}", name);
- let slug = self
- .slug
- .as_ref()
- .map(|(slug, _)| slug.as_str())
- .unwrap_or_else(|| "missing-slug");
let msg = msg.as_deref().unwrap_or("suggestion");
- let msg = quote! { rustc_errors::DiagnosticMessage::fluent_attr(#slug, #msg) };
+ 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); })
fluent_attr_identifier: &str,
) -> TokenStream {
let diag = &self.diag;
-
- let slug =
- self.slug.as_ref().map(|(slug, _)| slug.as_str()).unwrap_or_else(|| "missing-slug");
let fn_name = format_ident!("span_{}", kind);
quote! {
#diag.#fn_name(
#field_binding,
- rustc_errors::DiagnosticMessage::fluent_attr(#slug, #fluent_attr_identifier)
+ rustc_errors::SubdiagnosticMessage::attr(#fluent_attr_identifier)
);
}
}
/// and `fluent_attr_identifier`.
fn add_subdiagnostic(&self, kind: &Ident, fluent_attr_identifier: &str) -> TokenStream {
let diag = &self.diag;
- let slug = self.slug.as_ref().map(|(slug, _)| slug.as_str()).unwrap_or("missing-slug");
quote! {
- #diag.#kind(rustc_errors::DiagnosticMessage::fluent_attr(#slug, #fluent_attr_identifier));
+ #diag.#kind(rustc_errors::SubdiagnosticMessage::attr(#fluent_attr_identifier));
}
}