Skip to content

Commit d839d2e

Browse files
committed
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`.
1 parent 7e9be92 commit d839d2e

File tree

3 files changed

+14
-46
lines changed

3 files changed

+14
-46
lines changed

compiler/rustc_macros/src/diagnostics/diagnostic.rs

+13-43
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use quote::{format_ident, quote};
1313
use std::collections::HashMap;
1414
use std::str::FromStr;
1515
use syn::{spanned::Spanned, Attribute, Meta, MetaList, MetaNameValue, Type};
16-
use synstructure::Structure;
16+
use synstructure::{BindingInfo, Structure};
1717

1818
/// The central struct for constructing the `into_diagnostic` method from an annotated struct.
1919
pub(crate) struct SessionDiagnosticDerive<'a> {
@@ -95,20 +95,7 @@ impl<'a> SessionDiagnosticDerive<'a> {
9595
false
9696
}
9797
})
98-
.each(|field_binding| {
99-
let field = field_binding.ast();
100-
let field_span = field.span();
101-
102-
builder.generate_field_attrs_code(
103-
&field.attrs,
104-
FieldInfo {
105-
vis: &field.vis,
106-
binding: field_binding,
107-
ty: &field.ty,
108-
span: &field_span,
109-
},
110-
)
111-
});
98+
.each(|field_binding| builder.generate_field_attrs_code(field_binding));
11299

113100
structure.bind_with(|_| synstructure::BindStyle::Move);
114101
// When a field has attributes like `#[label]` or `#[note]` then it doesn't
@@ -119,20 +106,7 @@ impl<'a> SessionDiagnosticDerive<'a> {
119106
.filter(|field_binding| {
120107
subdiagnostics_or_empty.contains(&field_binding.binding)
121108
})
122-
.each(|field_binding| {
123-
let field = field_binding.ast();
124-
let field_span = field.span();
125-
126-
builder.generate_field_attrs_code(
127-
&field.attrs,
128-
FieldInfo {
129-
vis: &field.vis,
130-
binding: field_binding,
131-
ty: &field.ty,
132-
span: &field_span,
133-
},
134-
)
135-
});
109+
.each(|field_binding| builder.generate_field_attrs_code(field_binding));
136110

137111
let span = ast.span().unwrap();
138112
let (diag, sess) = (&builder.diag, &builder.sess);
@@ -360,30 +334,27 @@ impl SessionDiagnosticDeriveBuilder {
360334
Ok(tokens.drain(..).collect())
361335
}
362336

363-
fn generate_field_attrs_code(
364-
&mut self,
365-
attrs: &[syn::Attribute],
366-
info: FieldInfo<'_>,
367-
) -> TokenStream {
368-
let field_binding = &info.binding.binding;
337+
fn generate_field_attrs_code(&mut self, binding_info: &BindingInfo<'_>) -> TokenStream {
338+
let field = binding_info.ast();
339+
let field_binding = &binding_info.binding;
369340

370-
let inner_ty = FieldInnerTy::from_type(&info.ty);
341+
let inner_ty = FieldInnerTy::from_type(&field.ty);
371342

372343
// When generating `set_arg` or `add_subdiagnostic` calls, move data rather than
373344
// borrow it to avoid requiring clones - this must therefore be the last use of
374345
// each field (for example, any formatting machinery that might refer to a field
375346
// should be generated already).
376-
if attrs.is_empty() {
347+
if field.attrs.is_empty() {
377348
let diag = &self.diag;
378-
let ident = info.binding.ast().ident.as_ref().unwrap();
349+
let ident = field.ident.as_ref().unwrap();
379350
quote! {
380351
#diag.set_arg(
381352
stringify!(#ident),
382353
#field_binding
383354
);
384355
}
385356
} else {
386-
attrs
357+
field.attrs
387358
.iter()
388359
.map(move |attr| {
389360
let name = attr.path.segments.last().unwrap().ident.to_string();
@@ -401,10 +372,9 @@ impl SessionDiagnosticDeriveBuilder {
401372
.generate_inner_field_code(
402373
attr,
403374
FieldInfo {
404-
vis: info.vis,
405-
binding: info.binding,
406-
ty: inner_ty.inner_type().unwrap_or(&info.ty),
407-
span: info.span,
375+
binding: binding_info,
376+
ty: inner_ty.inner_type().unwrap_or(&field.ty),
377+
span: &field.span(),
408378
},
409379
binding,
410380
)

compiler/rustc_macros/src/diagnostics/subdiagnostic.rs

-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ impl<'a> SessionSubdiagnosticDeriveBuilder<'a> {
303303

304304
let inner_ty = FieldInnerTy::from_type(&ast.ty);
305305
let info = FieldInfo {
306-
vis: &ast.vis,
307306
binding: binding,
308307
ty: inner_ty.inner_type().unwrap_or(&ast.ty),
309308
span: &ast.span(),

compiler/rustc_macros/src/diagnostics/utils.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use proc_macro2::TokenStream;
44
use quote::{format_ident, quote, ToTokens};
55
use std::collections::BTreeSet;
66
use std::str::FromStr;
7-
use syn::{spanned::Spanned, Attribute, Meta, Type, TypeTuple, Visibility};
7+
use syn::{spanned::Spanned, Attribute, Meta, Type, TypeTuple};
88
use synstructure::BindingInfo;
99

1010
/// Checks whether the type name of `ty` matches `name`.
@@ -158,7 +158,6 @@ impl<'ty> FieldInnerTy<'ty> {
158158
/// Field information passed to the builder. Deliberately omits attrs to discourage the
159159
/// `generate_*` methods from walking the attributes themselves.
160160
pub(crate) struct FieldInfo<'a> {
161-
pub(crate) vis: &'a Visibility,
162161
pub(crate) binding: &'a BindingInfo<'a>,
163162
pub(crate) ty: &'a Type,
164163
pub(crate) span: &'a proc_macro2::Span,

0 commit comments

Comments
 (0)