1
Fork 0

let generate_field_attrs_code create FieldInfo

this simplifies the code inside the `structure.each` closure argument
and allows to remove the `vis` field from `FieldInfo`.
This commit is contained in:
Christian Poveda 2022-05-19 00:47:45 -05:00
parent 7e9be9240c
commit d839d2ecc2
No known key found for this signature in database
GPG key ID: 27525EF5E7420A50
3 changed files with 14 additions and 46 deletions

View file

@ -13,7 +13,7 @@ use quote::{format_ident, quote};
use std::collections::HashMap; use std::collections::HashMap;
use std::str::FromStr; use std::str::FromStr;
use syn::{spanned::Spanned, Attribute, Meta, MetaList, MetaNameValue, Type}; 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. /// The central struct for constructing the `into_diagnostic` method from an annotated struct.
pub(crate) struct SessionDiagnosticDerive<'a> { pub(crate) struct SessionDiagnosticDerive<'a> {
@ -95,20 +95,7 @@ impl<'a> SessionDiagnosticDerive<'a> {
false false
} }
}) })
.each(|field_binding| { .each(|field_binding| builder.generate_field_attrs_code(field_binding));
let field = field_binding.ast();
let field_span = field.span();
builder.generate_field_attrs_code(
&field.attrs,
FieldInfo {
vis: &field.vis,
binding: field_binding,
ty: &field.ty,
span: &field_span,
},
)
});
structure.bind_with(|_| synstructure::BindStyle::Move); structure.bind_with(|_| synstructure::BindStyle::Move);
// When a field has attributes like `#[label]` or `#[note]` then it doesn't // When a field has attributes like `#[label]` or `#[note]` then it doesn't
@ -119,20 +106,7 @@ impl<'a> SessionDiagnosticDerive<'a> {
.filter(|field_binding| { .filter(|field_binding| {
subdiagnostics_or_empty.contains(&field_binding.binding) subdiagnostics_or_empty.contains(&field_binding.binding)
}) })
.each(|field_binding| { .each(|field_binding| builder.generate_field_attrs_code(field_binding));
let field = field_binding.ast();
let field_span = field.span();
builder.generate_field_attrs_code(
&field.attrs,
FieldInfo {
vis: &field.vis,
binding: field_binding,
ty: &field.ty,
span: &field_span,
},
)
});
let span = ast.span().unwrap(); let span = ast.span().unwrap();
let (diag, sess) = (&builder.diag, &builder.sess); let (diag, sess) = (&builder.diag, &builder.sess);
@ -360,22 +334,19 @@ impl SessionDiagnosticDeriveBuilder {
Ok(tokens.drain(..).collect()) Ok(tokens.drain(..).collect())
} }
fn generate_field_attrs_code( fn generate_field_attrs_code(&mut self, binding_info: &BindingInfo<'_>) -> TokenStream {
&mut self, let field = binding_info.ast();
attrs: &[syn::Attribute], let field_binding = &binding_info.binding;
info: FieldInfo<'_>,
) -> TokenStream {
let field_binding = &info.binding.binding;
let inner_ty = FieldInnerTy::from_type(&info.ty); let inner_ty = FieldInnerTy::from_type(&field.ty);
// When generating `set_arg` or `add_subdiagnostic` calls, move data rather than // 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 // 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 // each field (for example, any formatting machinery that might refer to a field
// should be generated already). // should be generated already).
if attrs.is_empty() { if field.attrs.is_empty() {
let diag = &self.diag; let diag = &self.diag;
let ident = info.binding.ast().ident.as_ref().unwrap(); let ident = field.ident.as_ref().unwrap();
quote! { quote! {
#diag.set_arg( #diag.set_arg(
stringify!(#ident), stringify!(#ident),
@ -383,7 +354,7 @@ impl SessionDiagnosticDeriveBuilder {
); );
} }
} else { } else {
attrs field.attrs
.iter() .iter()
.map(move |attr| { .map(move |attr| {
let name = attr.path.segments.last().unwrap().ident.to_string(); let name = attr.path.segments.last().unwrap().ident.to_string();
@ -401,10 +372,9 @@ impl SessionDiagnosticDeriveBuilder {
.generate_inner_field_code( .generate_inner_field_code(
attr, attr,
FieldInfo { FieldInfo {
vis: info.vis, binding: binding_info,
binding: info.binding, ty: inner_ty.inner_type().unwrap_or(&field.ty),
ty: inner_ty.inner_type().unwrap_or(&info.ty), span: &field.span(),
span: info.span,
}, },
binding, binding,
) )

View file

@ -303,7 +303,6 @@ impl<'a> SessionSubdiagnosticDeriveBuilder<'a> {
let inner_ty = FieldInnerTy::from_type(&ast.ty); let inner_ty = FieldInnerTy::from_type(&ast.ty);
let info = FieldInfo { let info = FieldInfo {
vis: &ast.vis,
binding: binding, binding: binding,
ty: inner_ty.inner_type().unwrap_or(&ast.ty), ty: inner_ty.inner_type().unwrap_or(&ast.ty),
span: &ast.span(), span: &ast.span(),

View file

@ -4,7 +4,7 @@ use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens}; use quote::{format_ident, quote, ToTokens};
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::str::FromStr; use std::str::FromStr;
use syn::{spanned::Spanned, Attribute, Meta, Type, TypeTuple, Visibility}; use syn::{spanned::Spanned, Attribute, Meta, Type, TypeTuple};
use synstructure::BindingInfo; use synstructure::BindingInfo;
/// Checks whether the type name of `ty` matches `name`. /// Checks whether the type name of `ty` matches `name`.
@ -158,7 +158,6 @@ impl<'ty> FieldInnerTy<'ty> {
/// Field information passed to the builder. Deliberately omits attrs to discourage the /// Field information passed to the builder. Deliberately omits attrs to discourage the
/// `generate_*` methods from walking the attributes themselves. /// `generate_*` methods from walking the attributes themselves.
pub(crate) struct FieldInfo<'a> { pub(crate) struct FieldInfo<'a> {
pub(crate) vis: &'a Visibility,
pub(crate) binding: &'a BindingInfo<'a>, pub(crate) binding: &'a BindingInfo<'a>,
pub(crate) ty: &'a Type, pub(crate) ty: &'a Type,
pub(crate) span: &'a proc_macro2::Span, pub(crate) span: &'a proc_macro2::Span,