generate set_arg
code inside generate_field_attrs_code
This commit is contained in:
parent
462c1c846b
commit
19e1f73085
1 changed files with 99 additions and 97 deletions
|
@ -71,90 +71,71 @@ impl<'a> SessionDiagnosticDerive<'a> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Keep track of which fields are subdiagnostics
|
// Keep track of which fields are subdiagnostics or have no attributes.
|
||||||
let mut subdiagnostics = std::collections::HashSet::new();
|
let mut subdiagnostics_or_empty = std::collections::HashSet::new();
|
||||||
|
|
||||||
// Generates calls to `span_label` and similar functions based on the attributes
|
// Generates calls to `span_label` and similar functions based on the attributes
|
||||||
// on fields. Code for suggestions uses formatting machinery and the value of
|
// on fields. Code for suggestions uses formatting machinery and the value of
|
||||||
// other fields - because any given field can be referenced multiple times, it
|
// 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
|
// should be accessed through a borrow. When passing fields to `add_subdiagnostic`
|
||||||
// happens below) for Fluent, we want to move the data, so that has to happen
|
// or `set_arg` (which happens below) for Fluent, we want to move the data, so that
|
||||||
// in a separate pass over the fields.
|
// has to happen in a separate pass over the fields.
|
||||||
let attrs = structure
|
let attrs = structure
|
||||||
.clone()
|
.clone()
|
||||||
// Remove the fields that have a `subdiagnostic` attribute.
|
|
||||||
.filter(|field_binding| {
|
.filter(|field_binding| {
|
||||||
field_binding.ast().attrs.iter().all(|attr| {
|
let attrs = &field_binding.ast().attrs;
|
||||||
"subdiagnostic" != attr.path.segments.last().unwrap().ident.to_string()
|
|
||||||
|
(!attrs.is_empty()
|
||||||
|
&& attrs.iter().all(|attr| {
|
||||||
|
"subdiagnostic"
|
||||||
|
!= attr.path.segments.last().unwrap().ident.to_string()
|
||||||
|
}))
|
||||||
|| {
|
|| {
|
||||||
subdiagnostics.insert(field_binding.binding.clone());
|
subdiagnostics_or_empty.insert(field_binding.binding.clone());
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
.each(|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,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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).
|
||||||
|
structure.bind_with(|_| synstructure::BindStyle::Move);
|
||||||
|
// 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| {
|
.each(|field_binding| {
|
||||||
let field = field_binding.ast();
|
let field = field_binding.ast();
|
||||||
let result = field.attrs.iter().map(|attr| {
|
let field_span = field.span();
|
||||||
builder
|
|
||||||
.generate_field_attr_code(
|
builder.generate_field_attrs_code(
|
||||||
attr,
|
&field.attrs,
|
||||||
FieldInfo {
|
FieldInfo {
|
||||||
vis: &field.vis,
|
vis: &field.vis,
|
||||||
binding: field_binding,
|
binding: field_binding,
|
||||||
ty: &field.ty,
|
ty: &field.ty,
|
||||||
span: &field.span(),
|
span: &field_span,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.unwrap_or_else(|v| v.to_compile_error())
|
|
||||||
});
|
|
||||||
|
|
||||||
quote! { #(#result);* }
|
|
||||||
});
|
|
||||||
|
|
||||||
// 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.
|
|
||||||
let tokens = 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! {}
|
|
||||||
};
|
|
||||||
// If this field had a subdiagnostic attribute, we generate the code here to
|
|
||||||
// avoid binding it twice.
|
|
||||||
if subdiagnostics.contains(&field_binding.binding) {
|
|
||||||
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);* #tokens }
|
|
||||||
} else {
|
|
||||||
tokens
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let span = ast.span().unwrap();
|
let span = ast.span().unwrap();
|
||||||
|
@ -383,24 +364,41 @@ impl SessionDiagnosticDeriveBuilder {
|
||||||
Ok(tokens.drain(..).collect())
|
Ok(tokens.drain(..).collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_field_attr_code(
|
fn generate_field_attrs_code<'a>(
|
||||||
&mut self,
|
&'a mut self,
|
||||||
attr: &syn::Attribute,
|
attrs: &'a [syn::Attribute],
|
||||||
info: FieldInfo<'_>,
|
info: FieldInfo<'a>,
|
||||||
) -> Result<TokenStream, SessionDiagnosticDeriveError> {
|
) -> TokenStream {
|
||||||
let field_binding = &info.binding.binding;
|
let field_binding = &info.binding.binding;
|
||||||
|
|
||||||
let inner_ty = FieldInnerTy::from_type(&info.ty);
|
let inner_ty = FieldInnerTy::from_type(&info.ty);
|
||||||
|
|
||||||
|
if attrs.is_empty() {
|
||||||
|
let diag = &self.diag;
|
||||||
|
let ident = info.binding.ast().ident.as_ref().unwrap();
|
||||||
|
quote! {
|
||||||
|
#diag.set_arg(
|
||||||
|
stringify!(#ident),
|
||||||
|
#field_binding
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
attrs
|
||||||
|
.iter()
|
||||||
|
.map(move |attr| {
|
||||||
let name = attr.path.segments.last().unwrap().ident.to_string();
|
let name = attr.path.segments.last().unwrap().ident.to_string();
|
||||||
let (binding, needs_destructure) = match (name.as_str(), &inner_ty) {
|
let (binding, needs_destructure) = match (name.as_str(), &inner_ty) {
|
||||||
// `primary_span` can accept a `Vec<Span>` so don't destructure that.
|
// `primary_span` can accept a `Vec<Span>` so don't destructure that.
|
||||||
("primary_span", FieldInnerTy::Vec(_)) => (quote! { #field_binding.clone() }, false),
|
("primary_span", FieldInnerTy::Vec(_)) => {
|
||||||
|
(quote! { #field_binding.clone() }, false)
|
||||||
|
}
|
||||||
// `subdiagnostics` are not derefed because they are bound by value.
|
// `subdiagnostics` are not derefed because they are bound by value.
|
||||||
("subdiagnostic", _) => (quote! { #field_binding }, true),
|
("subdiagnostic", _) => (quote! { #field_binding }, true),
|
||||||
_ => (quote! { *#field_binding }, true),
|
_ => (quote! { *#field_binding }, true),
|
||||||
};
|
};
|
||||||
|
|
||||||
let generated_code = self.generate_inner_field_code(
|
let generated_code = self
|
||||||
|
.generate_inner_field_code(
|
||||||
attr,
|
attr,
|
||||||
FieldInfo {
|
FieldInfo {
|
||||||
vis: info.vis,
|
vis: info.vis,
|
||||||
|
@ -409,12 +407,16 @@ impl SessionDiagnosticDeriveBuilder {
|
||||||
span: info.span,
|
span: info.span,
|
||||||
},
|
},
|
||||||
binding,
|
binding,
|
||||||
)?;
|
)
|
||||||
|
.unwrap_or_else(|v| v.to_compile_error());
|
||||||
|
|
||||||
if needs_destructure {
|
if needs_destructure {
|
||||||
Ok(inner_ty.with(field_binding, generated_code))
|
inner_ty.with(field_binding, generated_code)
|
||||||
} else {
|
} else {
|
||||||
Ok(generated_code)
|
generated_code
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue