Skip to content

Commit

Permalink
Parse unnamed struct/union syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Sep 3, 2023
1 parent face31f commit e827aca
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
38 changes: 29 additions & 9 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,37 @@ pub(crate) mod parsing {
/// Parses a named (braced struct) field.
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
pub fn parse_named(input: ParseStream) -> Result<Self> {
let attrs = input.call(Attribute::parse_outer)?;
let vis: Visibility = input.parse()?;

let unnamed_field = cfg!(feature = "full") && input.peek(Token![_]);
let ident = if unnamed_field {
input.call(Ident::parse_any)
} else {
input.parse()
}?;

let colon_token: Token![:] = input.parse()?;

let ty: Type = if unnamed_field
&& (input.peek(Token![struct])
|| input.peek(Token![union]) && input.peek2(token::Brace))
{
let begin = input.fork();
input.call(Ident::parse_any)?;
input.parse::<FieldsNamed>()?;
Type::Verbatim(verbatim::between(&begin, input))
} else {
input.parse()?
};

Ok(Field {
attrs: input.call(Attribute::parse_outer)?,
vis: input.parse()?,
attrs,
vis,
mutability: FieldMutability::None,
ident: Some(if input.peek(Token![_]) {
input.call(Ident::parse_any)
} else {
input.parse()
}?),
colon_token: Some(input.parse()?),
ty: input.parse()?,
ident: Some(ident),
colon_token: Some(colon_token),
ty,
})
}

Expand Down
4 changes: 0 additions & 4 deletions tests/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ const REVISION: &str = "9f5fc1bd443f59583e7af0d94d289f95fe1e20c4";

#[rustfmt::skip]
static EXCLUDE_FILES: &[&str] = &[
// TODO: anonymous union and struct
// https://github.com/dtolnay/syn/issues/1049
"src/tools/rustfmt/tests/target/anonymous-types.rs",

// TODO: generic const items
// https://github.com/dtolnay/syn/issues/1497
"tests/rustdoc/generic-const-items.rs",
Expand Down

0 comments on commit e827aca

Please sign in to comment.