Defer expansion of fmt shorthand

This will make it possible to distinguish between our expansion
heuristic for tuple vs struct variants, as well as local fields vs
statics.
This commit is contained in:
David Tolnay 2019-11-30 13:23:29 -08:00
parent bfc7f8a4b1
commit 886baece01
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 10 additions and 5 deletions

View File

@ -52,8 +52,12 @@ impl<'a> Input<'a> {
impl<'a> Struct<'a> { impl<'a> Struct<'a> {
fn from_syn(node: &'a DeriveInput, data: &'a DataStruct) -> Result<Self> { fn from_syn(node: &'a DeriveInput, data: &'a DataStruct) -> Result<Self> {
let mut attrs = attr::get(&node.attrs)?;
if let Some(display) = &mut attrs.display {
display.expand_shorthand();
}
Ok(Struct { Ok(Struct {
attrs: attr::get(&node.attrs)?, attrs,
ident: node.ident.clone(), ident: node.ident.clone(),
generics: &node.generics, generics: &node.generics,
fields: Field::multiple_from_syn(&data.fields)?, fields: Field::multiple_from_syn(&data.fields)?,
@ -72,6 +76,9 @@ impl<'a> Enum<'a> {
if let display @ None = &mut variant.attrs.display { if let display @ None = &mut variant.attrs.display {
*display = attrs.display.clone(); *display = attrs.display.clone();
} }
if let Some(display) = &mut variant.attrs.display {
display.expand_shorthand();
}
Ok(variant) Ok(variant)
}) })
.collect::<Result<_>>()?; .collect::<Result<_>>()?;

View File

@ -70,15 +70,13 @@ pub fn get(input: &[Attribute]) -> Result<Attrs> {
fn parse_display(attr: &Attribute) -> Result<Display> { fn parse_display(attr: &Attribute) -> Result<Display> {
attr.parse_args_with(|input: ParseStream| { attr.parse_args_with(|input: ParseStream| {
let mut display = Display { Ok(Display {
original: attr, original: attr,
fmt: input.parse()?, fmt: input.parse()?,
args: parse_token_expr(input, false)?, args: parse_token_expr(input, false)?,
was_shorthand: false, was_shorthand: false,
has_bonus_display: false, has_bonus_display: false,
}; })
display.expand_shorthand();
Ok(display)
}) })
} }