Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 897f0de

Browse files
authored
Merge pull request #1498 from dtolnay/unnamedfields
Parse unnamed struct/union syntax
2 parents face31f + e827aca commit 897f0de

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/data.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,37 @@ pub(crate) mod parsing {
214214
/// Parses a named (braced struct) field.
215215
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
216216
pub fn parse_named(input: ParseStream) -> Result<Self> {
217+
let attrs = input.call(Attribute::parse_outer)?;
218+
let vis: Visibility = input.parse()?;
219+
220+
let unnamed_field = cfg!(feature = "full") && input.peek(Token![_]);
221+
let ident = if unnamed_field {
222+
input.call(Ident::parse_any)
223+
} else {
224+
input.parse()
225+
}?;
226+
227+
let colon_token: Token![:] = input.parse()?;
228+
229+
let ty: Type = if unnamed_field
230+
&& (input.peek(Token![struct])
231+
|| input.peek(Token![union]) && input.peek2(token::Brace))
232+
{
233+
let begin = input.fork();
234+
input.call(Ident::parse_any)?;
235+
input.parse::<FieldsNamed>()?;
236+
Type::Verbatim(verbatim::between(&begin, input))
237+
} else {
238+
input.parse()?
239+
};
240+
217241
Ok(Field {
218-
attrs: input.call(Attribute::parse_outer)?,
219-
vis: input.parse()?,
242+
attrs,
243+
vis,
220244
mutability: FieldMutability::None,
221-
ident: Some(if input.peek(Token![_]) {
222-
input.call(Ident::parse_any)
223-
} else {
224-
input.parse()
225-
}?),
226-
colon_token: Some(input.parse()?),
227-
ty: input.parse()?,
245+
ident: Some(ident),
246+
colon_token: Some(colon_token),
247+
ty,
228248
})
229249
}
230250

tests/repo/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ const REVISION: &str = "9f5fc1bd443f59583e7af0d94d289f95fe1e20c4";
1717

1818
#[rustfmt::skip]
1919
static EXCLUDE_FILES: &[&str] = &[
20-
// TODO: anonymous union and struct
21-
// https://github.com/dtolnay/syn/issues/1049
22-
"src/tools/rustfmt/tests/target/anonymous-types.rs",
23-
2420
// TODO: generic const items
2521
// https://github.com/dtolnay/syn/issues/1497
2622
"tests/rustdoc/generic-const-items.rs",

0 commit comments

Comments
 (0)