Skip to content

Commit

Permalink
Inherit span from enum to variants
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed May 15, 2020
1 parent 57b4e92 commit 3a49609
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
21 changes: 17 additions & 4 deletions impl/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl<'a> Input<'a> {
impl<'a> Struct<'a> {
fn from_syn(node: &'a DeriveInput, data: &'a DataStruct) -> Result<Self> {
let mut attrs = attr::get(&node.attrs)?;
let span = attrs.span();
let span = attrs.span().unwrap_or_else(Span::call_site);
let fields = Field::multiple_from_syn(&data.fields, span)?;
if let Some(display) = &mut attrs.display {
display.expand_shorthand(&fields);
Expand All @@ -74,11 +74,12 @@ impl<'a> Struct<'a> {
impl<'a> Enum<'a> {
fn from_syn(node: &'a DeriveInput, data: &'a DataEnum) -> Result<Self> {
let attrs = attr::get(&node.attrs)?;
let span = attrs.span().unwrap_or_else(Span::call_site);
let variants = data
.variants
.iter()
.map(|node| {
let mut variant = Variant::from_syn(node)?;
let mut variant = Variant::from_syn(node, span)?;
if let display @ None = &mut variant.attrs.display {
*display = attrs.display.clone();
}
Expand All @@ -101,9 +102,9 @@ impl<'a> Enum<'a> {
}

impl<'a> Variant<'a> {
fn from_syn(node: &'a syn::Variant) -> Result<Self> {
fn from_syn(node: &'a syn::Variant, span: Span) -> Result<Self> {
let attrs = attr::get(&node.attrs)?;
let span = attrs.span();
let span = attrs.span().unwrap_or(span);
Ok(Variant {
original: node,
attrs,
Expand Down Expand Up @@ -136,3 +137,15 @@ impl<'a> Field<'a> {
})
}
}

impl Attrs<'_> {
pub fn span(&self) -> Option<Span> {
if let Some(display) = &self.display {
Some(display.fmt.span())
} else if let Some(transparent) = &self.transparent {
Some(transparent.span)
} else {
None
}
}
}
12 changes: 0 additions & 12 deletions impl/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,3 @@ impl ToTokens for Display<'_> {
});
}
}

impl Attrs<'_> {
pub fn span(&self) -> Span {
if let Some(display) = &self.display {
display.fmt.span()
} else if let Some(transparent) = &self.transparent {
transparent.span
} else {
Span::call_site()
}
}
}

0 comments on commit 3a49609

Please sign in to comment.