diff options
author | Joel Galenson <jgalenson@google.com> | 2021-05-19 17:24:08 -0700 |
---|---|---|
committer | Joel Galenson <jgalenson@google.com> | 2021-05-19 17:24:08 -0700 |
commit | fdd477442016fa3579b7b97a42e52e2340149ba7 (patch) | |
tree | 55e4cdf9420132a0989e9bd5f7d78c533a2b95ec | |
parent | 378cd52597b55bd262c21722d1c2639c7a4e21a2 (diff) | |
download | platform_external_rust_crates_syn-fdd477442016fa3579b7b97a42e52e2340149ba7.tar.gz platform_external_rust_crates_syn-fdd477442016fa3579b7b97a42e52e2340149ba7.tar.bz2 platform_external_rust_crates_syn-fdd477442016fa3579b7b97a42e52e2340149ba7.zip |
Upgrade rust/crates/syn to 1.0.72
Test: make
Change-Id: Ie700357fdff8bb1a8c760adb67274f6b91c42eb3
-rw-r--r-- | .cargo_vcs_info.json | 2 | ||||
-rw-r--r-- | Android.bp | 6 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | Cargo.toml.orig | 2 | ||||
-rw-r--r-- | METADATA | 8 | ||||
-rw-r--r-- | README.md | 6 | ||||
-rw-r--r-- | TEST_MAPPING | 3 | ||||
-rw-r--r-- | src/attr.rs | 22 | ||||
-rw-r--r-- | src/data.rs | 51 | ||||
-rw-r--r-- | src/derive.rs | 20 | ||||
-rw-r--r-- | src/expr.rs | 49 | ||||
-rw-r--r-- | src/generics.rs | 15 | ||||
-rw-r--r-- | src/item.rs | 87 | ||||
-rw-r--r-- | src/lib.rs | 4 | ||||
-rw-r--r-- | src/lit.rs | 2 | ||||
-rw-r--r-- | src/path.rs | 12 | ||||
-rw-r--r-- | src/ty.rs | 11 | ||||
-rw-r--r-- | tests/common/eq.rs | 9 | ||||
-rw-r--r-- | tests/repo/mod.rs | 6 | ||||
-rw-r--r-- | tests/test_expr.rs | 18 | ||||
-rw-r--r-- | tests/test_item.rs | 33 |
21 files changed, 234 insertions, 134 deletions
diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json index 36a487f8..a9421b03 100644 --- a/.cargo_vcs_info.json +++ b/.cargo_vcs_info.json @@ -1,5 +1,5 @@ { "git": { - "sha1": "c80b47159a47340d20f5774d2fe1c09bd0994a13" + "sha1": "c3f9c67ee210f00e8119d7d1485455dad40f2e7b" } } @@ -55,9 +55,7 @@ rust_library_host { "visit", "visit-mut", ], - flags: [ - "--cfg syn_disable_nightly_tests", - ], + cfgs: ["syn_disable_nightly_tests"], rustlibs: [ "libproc_macro2", "libquote", @@ -69,4 +67,4 @@ rust_library_host { // dependent_library ["feature_list"] // proc-macro2-1.0.26 "default,proc-macro" // quote-1.0.9 "default,proc-macro" -// unicode-xid-0.2.1 "default" +// unicode-xid-0.2.2 "default" @@ -13,7 +13,7 @@ [package] edition = "2018" name = "syn" -version = "1.0.69" +version = "1.0.72" authors = ["David Tolnay <dtolnay@gmail.com>"] include = ["/benches/**", "/build.rs", "/Cargo.toml", "/LICENSE-APACHE", "/LICENSE-MIT", "/README.md", "/src/**", "/tests/**"] description = "Parser for Rust source code" diff --git a/Cargo.toml.orig b/Cargo.toml.orig index d29b93db..d2ba383b 100644 --- a/Cargo.toml.orig +++ b/Cargo.toml.orig @@ -1,6 +1,6 @@ [package] name = "syn" -version = "1.0.69" # don't forget to update html_root_url and syn.json +version = "1.0.72" # don't forget to update html_root_url and syn.json authors = ["David Tolnay <dtolnay@gmail.com>"] license = "MIT OR Apache-2.0" description = "Parser for Rust source code" @@ -7,13 +7,13 @@ third_party { } url { type: ARCHIVE - value: "https://static.crates.io/crates/syn/syn-1.0.69.crate" + value: "https://static.crates.io/crates/syn/syn-1.0.72.crate" } - version: "1.0.69" + version: "1.0.72" license_type: NOTICE last_upgrade_date { year: 2021 - month: 4 - day: 13 + month: 5 + day: 19 } } @@ -150,7 +150,7 @@ By tracking span information all the way through the expansion of a procedural macro as shown in the `heapsize` example, token-based macros in Syn are able to trigger errors that directly pinpoint the source of the problem. -``` +```console error[E0277]: the trait bound `std::thread::Thread: HeapSize` is not satisfied --> src/main.rs:7:5 | @@ -171,7 +171,7 @@ Syn's parsing API. The example reimplements the popular `lazy_static` crate from crates.io as a procedural macro. -``` +```rust lazy_static! { static ref USERNAME: Regex = Regex::new("^[a-z0-9_-]{3,16}$").unwrap(); } @@ -180,7 +180,7 @@ lazy_static! { The implementation shows how to trigger custom warnings and error messages on the macro input. -``` +```console warning: come on, pick a more creative name --> src/main.rs:10:16 | diff --git a/TEST_MAPPING b/TEST_MAPPING index ff7187ba..32f62ad6 100644 --- a/TEST_MAPPING +++ b/TEST_MAPPING @@ -56,6 +56,9 @@ "name": "keystore2_crypto_test_rust" }, { + "name": "keystore2_selinux_concurrency_test" + }, + { "name": "keystore2_selinux_test" }, { diff --git a/src/attr.rs b/src/attr.rs index 794a3104..505ffeb7 100644 --- a/src/attr.rs +++ b/src/attr.rs @@ -250,9 +250,7 @@ impl Attribute { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] pub fn parse_inner(input: ParseStream) -> Result<Vec<Self>> { let mut attrs = Vec::new(); - while input.peek(Token![#]) && input.peek2(Token![!]) { - attrs.push(input.call(parsing::single_parse_inner)?); - } + parsing::parse_inner(input, &mut attrs)?; Ok(attrs) } } @@ -503,8 +501,13 @@ pub mod parsing { use super::*; use crate::ext::IdentExt; use crate::parse::{Parse, ParseStream, Result}; - #[cfg(feature = "full")] - use crate::private; + + pub fn parse_inner(input: ParseStream, attrs: &mut Vec<Attribute>) -> Result<()> { + while input.peek(Token![#]) && input.peek2(Token![!]) { + attrs.push(input.call(parsing::single_parse_inner)?); + } + Ok(()) + } pub fn single_parse_inner(input: ParseStream) -> Result<Attribute> { let content; @@ -528,15 +531,6 @@ pub mod parsing { }) } - #[cfg(feature = "full")] - impl private { - pub(crate) fn attrs(outer: Vec<Attribute>, inner: Vec<Attribute>) -> Vec<Attribute> { - let mut attrs = outer; - attrs.extend(inner); - attrs - } - } - // Like Path::parse_mod_style but accepts keywords in the path. fn parse_meta_path(input: ParseStream) -> Result<Path> { Ok(Path { diff --git a/src/data.rs b/src/data.rs index 731f5a0a..b0258ca0 100644 --- a/src/data.rs +++ b/src/data.rs @@ -246,29 +246,29 @@ pub mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for Variant { fn parse(input: ParseStream) -> Result<Self> { - let attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let _visibility: Visibility = input.parse()?; + let ident: Ident = input.parse()?; + let fields = if input.peek(token::Brace) { + let fields = parse_braced(input, &mut attrs)?; + Fields::Named(fields) + } else if input.peek(token::Paren) { + Fields::Unnamed(input.parse()?) + } else { + Fields::Unit + }; + let discriminant = if input.peek(Token![=]) { + let eq_token: Token![=] = input.parse()?; + let discriminant: Expr = input.parse()?; + Some((eq_token, discriminant)) + } else { + None + }; Ok(Variant { attrs, - ident: input.parse()?, - fields: { - if input.peek(token::Brace) { - Fields::Named(input.parse()?) - } else if input.peek(token::Paren) { - Fields::Unnamed(input.parse()?) - } else { - Fields::Unit - } - }, - discriminant: { - if input.peek(Token![=]) { - let eq_token: Token![=] = input.parse()?; - let discriminant: Expr = input.parse()?; - Some((eq_token, discriminant)) - } else { - None - } - }, + ident, + fields, + discriminant, }) } } @@ -295,6 +295,17 @@ pub mod parsing { } } + pub(crate) fn parse_braced( + input: ParseStream, + attrs: &mut Vec<Attribute>, + ) -> Result<FieldsNamed> { + let content; + let brace_token = braced!(content in input); + attr::parsing::parse_inner(&content, attrs)?; + let named = content.parse_terminated(Field::parse_named)?; + Ok(FieldsNamed { brace_token, named }) + } + impl Field { /// Parses a named (braced struct) field. #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] diff --git a/src/derive.rs b/src/derive.rs index af9bb91b..17387e45 100644 --- a/src/derive.rs +++ b/src/derive.rs @@ -95,7 +95,7 @@ pub mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for DeriveInput { fn parse(input: ParseStream) -> Result<Self> { - let attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let vis = input.parse::<Visibility>()?; let lookahead = input.lookahead1(); @@ -103,7 +103,7 @@ pub mod parsing { let struct_token = input.parse::<Token![struct]>()?; let ident = input.parse::<Ident>()?; let generics = input.parse::<Generics>()?; - let (where_clause, fields, semi) = data_struct(input)?; + let (where_clause, fields, semi) = data_struct(input, &mut attrs)?; Ok(DeriveInput { attrs, vis, @@ -122,7 +122,7 @@ pub mod parsing { let enum_token = input.parse::<Token![enum]>()?; let ident = input.parse::<Ident>()?; let generics = input.parse::<Generics>()?; - let (where_clause, brace, variants) = data_enum(input)?; + let (where_clause, brace, variants) = data_enum(input, &mut attrs)?; Ok(DeriveInput { attrs, vis, @@ -141,7 +141,7 @@ pub mod parsing { let union_token = input.parse::<Token![union]>()?; let ident = input.parse::<Ident>()?; let generics = input.parse::<Generics>()?; - let (where_clause, fields) = data_union(input)?; + let (where_clause, fields) = data_union(input, &mut attrs)?; Ok(DeriveInput { attrs, vis, @@ -163,6 +163,7 @@ pub mod parsing { pub fn data_struct( input: ParseStream, + attrs: &mut Vec<Attribute>, ) -> Result<(Option<WhereClause>, Fields, Option<Token![;]>)> { let mut lookahead = input.lookahead1(); let mut where_clause = None; @@ -187,7 +188,7 @@ pub mod parsing { Err(lookahead.error()) } } else if lookahead.peek(token::Brace) { - let fields = input.parse()?; + let fields = data::parsing::parse_braced(input, attrs)?; Ok((where_clause, Fields::Named(fields), None)) } else if lookahead.peek(Token![;]) { let semi = input.parse()?; @@ -199,6 +200,7 @@ pub mod parsing { pub fn data_enum( input: ParseStream, + attrs: &mut Vec<Attribute>, ) -> Result<( Option<WhereClause>, token::Brace, @@ -208,14 +210,18 @@ pub mod parsing { let content; let brace = braced!(content in input); + attr::parsing::parse_inner(&content, attrs)?; let variants = content.parse_terminated(Variant::parse)?; Ok((where_clause, brace, variants)) } - pub fn data_union(input: ParseStream) -> Result<(Option<WhereClause>, FieldsNamed)> { + pub fn data_union( + input: ParseStream, + attrs: &mut Vec<Attribute>, + ) -> Result<(Option<WhereClause>, FieldsNamed)> { let where_clause = input.parse()?; - let fields = input.parse()?; + let fields = data::parsing::parse_braced(input, attrs)?; Ok((where_clause, fields)) } } diff --git a/src/expr.rs b/src/expr.rs index a35532d4..ef629ecd 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -1322,6 +1322,7 @@ pub(crate) mod parsing { let rhs = if input.is_empty() || input.peek(Token![,]) || input.peek(Token![;]) + || input.peek(Token![.]) && !input.peek(Token![..]) || !allow_struct.0 && input.peek(token::Brace) { None @@ -1523,7 +1524,7 @@ pub(crate) mod parsing { // <atom> ? ... #[cfg(feature = "full")] fn trailer_expr( - outer_attrs: Vec<Attribute>, + mut attrs: Vec<Attribute>, input: ParseStream, allow_struct: AllowStruct, ) -> Result<Expr> { @@ -1531,7 +1532,7 @@ pub(crate) mod parsing { let mut e = trailer_helper(input, atom)?; let inner_attrs = e.replace_attrs(Vec::new()); - let attrs = private::attrs(outer_attrs, inner_attrs); + attrs.extend(inner_attrs); e.replace_attrs(attrs); Ok(e) } @@ -1547,7 +1548,13 @@ pub(crate) mod parsing { paren_token: parenthesized!(content in input), args: content.parse_terminated(Expr::parse)?, }); - } else if input.peek(Token![.]) && !input.peek(Token![..]) { + } else if input.peek(Token![.]) + && !input.peek(Token![..]) + && match e { + Expr::Range(_) => false, + _ => true, + } + { let mut dot_token: Token![.] = input.parse()?; let await_token: Option<token::Await> = input.parse()?; @@ -2134,7 +2141,7 @@ pub(crate) mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for ExprForLoop { fn parse(input: ParseStream) -> Result<Self> { - let outer_attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let label: Option<Label> = input.parse()?; let for_token: Token![for] = input.parse()?; @@ -2145,11 +2152,11 @@ pub(crate) mod parsing { let content; let brace_token = braced!(content in input); - let inner_attrs = content.call(Attribute::parse_inner)?; + attr::parsing::parse_inner(&content, &mut attrs)?; let stmts = content.call(Block::parse_within)?; Ok(ExprForLoop { - attrs: private::attrs(outer_attrs, inner_attrs), + attrs, label, for_token, pat, @@ -2164,17 +2171,17 @@ pub(crate) mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for ExprLoop { fn parse(input: ParseStream) -> Result<Self> { - let outer_attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let label: Option<Label> = input.parse()?; let loop_token: Token![loop] = input.parse()?; let content; let brace_token = braced!(content in input); - let inner_attrs = content.call(Attribute::parse_inner)?; + attr::parsing::parse_inner(&content, &mut attrs)?; let stmts = content.call(Block::parse_within)?; Ok(ExprLoop { - attrs: private::attrs(outer_attrs, inner_attrs), + attrs, label, loop_token, body: Block { brace_token, stmts }, @@ -2186,13 +2193,13 @@ pub(crate) mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for ExprMatch { fn parse(input: ParseStream) -> Result<Self> { - let outer_attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let match_token: Token![match] = input.parse()?; let expr = Expr::parse_without_eager_brace(input)?; let content; let brace_token = braced!(content in input); - let inner_attrs = content.call(Attribute::parse_inner)?; + attr::parsing::parse_inner(&content, &mut attrs)?; let mut arms = Vec::new(); while !content.is_empty() { @@ -2200,7 +2207,7 @@ pub(crate) mod parsing { } Ok(ExprMatch { - attrs: private::attrs(outer_attrs, inner_attrs), + attrs, match_token, expr: Box::new(expr), brace_token, @@ -2480,18 +2487,18 @@ pub(crate) mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for ExprWhile { fn parse(input: ParseStream) -> Result<Self> { - let outer_attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let label: Option<Label> = input.parse()?; let while_token: Token![while] = input.parse()?; let cond = Expr::parse_without_eager_brace(input)?; let content; let brace_token = braced!(content in input); - let inner_attrs = content.call(Attribute::parse_inner)?; + attr::parsing::parse_inner(&content, &mut attrs)?; let stmts = content.call(Block::parse_within)?; Ok(ExprWhile { - attrs: private::attrs(outer_attrs, inner_attrs), + attrs, label, while_token, cond: Box::new(cond), @@ -2620,13 +2627,12 @@ pub(crate) mod parsing { #[cfg(feature = "full")] fn expr_struct_helper( input: ParseStream, - outer_attrs: Vec<Attribute>, + mut attrs: Vec<Attribute>, path: Path, ) -> Result<ExprStruct> { let content; let brace_token = braced!(content in input); - let inner_attrs = content.call(Attribute::parse_inner)?; - let attrs = private::attrs(outer_attrs, inner_attrs); + attr::parsing::parse_inner(&content, &mut attrs)?; let mut fields = Punctuated::new(); while !content.is_empty() { @@ -2699,16 +2705,16 @@ pub(crate) mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for ExprBlock { fn parse(input: ParseStream) -> Result<Self> { - let outer_attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let label: Option<Label> = input.parse()?; let content; let brace_token = braced!(content in input); - let inner_attrs = content.call(Attribute::parse_inner)?; + attr::parsing::parse_inner(&content, &mut attrs)?; let stmts = content.call(Block::parse_within)?; Ok(ExprBlock { - attrs: private::attrs(outer_attrs, inner_attrs), + attrs, label, block: Block { brace_token, stmts }, }) @@ -2725,6 +2731,7 @@ pub(crate) mod parsing { if input.is_empty() || input.peek(Token![,]) || input.peek(Token![;]) + || input.peek(Token![.]) && !input.peek(Token![..]) || !allow_struct.0 && input.peek(token::Brace) { None diff --git a/src/generics.rs b/src/generics.rs index de0772a7..0950c1cb 100644 --- a/src/generics.rs +++ b/src/generics.rs @@ -172,17 +172,10 @@ impl Generics { /// Initializes an empty `where`-clause if there is not one present already. pub fn make_where_clause(&mut self) -> &mut WhereClause { - // This is Option::get_or_insert_with in Rust 1.20. - if self.where_clause.is_none() { - self.where_clause = Some(WhereClause { - where_token: <Token![where]>::default(), - predicates: Punctuated::new(), - }); - } - match &mut self.where_clause { - Some(where_clause) => where_clause, - None => unreachable!(), - } + self.where_clause.get_or_insert_with(|| WhereClause { + where_token: <Token![where]>::default(), + predicates: Punctuated::new(), + }) } } diff --git a/src/item.rs b/src/item.rs index 10479ab4..28ccf8dd 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1100,8 +1100,8 @@ pub mod parsing { { input.parse().map(Item::Trait) } else if lookahead.peek(Token![impl]) { - let allow_const_impl = true; - if let Some(item) = parse_impl(input, allow_const_impl)? { + let allow_verbatim_impl = true; + if let Some(item) = parse_impl(input, allow_verbatim_impl)? { Ok(Item::Impl(item)) } else { Ok(Item::Verbatim(verbatim::between(begin, input))) @@ -1138,8 +1138,8 @@ pub mod parsing { } else if lookahead.peek(Token![impl]) || lookahead.peek(Token![default]) && !ahead.peek2(Token![!]) { - let allow_const_impl = true; - if let Some(item) = parse_impl(input, allow_const_impl)? { + let allow_verbatim_impl = true; + if let Some(item) = parse_impl(input, allow_verbatim_impl)? { Ok(Item::Impl(item)) } else { Ok(Item::Verbatim(verbatim::between(begin, input))) @@ -1533,17 +1533,17 @@ pub mod parsing { fn parse_rest_of_fn( input: ParseStream, - outer_attrs: Vec<Attribute>, + mut attrs: Vec<Attribute>, vis: Visibility, sig: Signature, ) -> Result<ItemFn> { let content; let brace_token = braced!(content in input); - let inner_attrs = content.call(Attribute::parse_inner)?; + attr::parsing::parse_inner(&content, &mut attrs)?; let stmts = content.call(Block::parse_within)?; Ok(ItemFn { - attrs: private::attrs(outer_attrs, inner_attrs), + attrs, vis, sig, block: Box::new(Block { brace_token, stmts }), @@ -1669,7 +1669,7 @@ pub mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for ItemMod { fn parse(input: ParseStream) -> Result<Self> { - let outer_attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let vis: Visibility = input.parse()?; let mod_token: Token![mod] = input.parse()?; let ident: Ident = input.parse()?; @@ -1677,7 +1677,7 @@ pub mod parsing { let lookahead = input.lookahead1(); if lookahead.peek(Token![;]) { Ok(ItemMod { - attrs: outer_attrs, + attrs, vis, mod_token, ident, @@ -1687,7 +1687,7 @@ pub mod parsing { } else if lookahead.peek(token::Brace) { let content; let brace_token = braced!(content in input); - let inner_attrs = content.call(Attribute::parse_inner)?; + attr::parsing::parse_inner(&content, &mut attrs)?; let mut items = Vec::new(); while !content.is_empty() { @@ -1695,7 +1695,7 @@ pub mod parsing { } Ok(ItemMod { - attrs: private::attrs(outer_attrs, inner_attrs), + attrs, vis, mod_token, ident, @@ -1711,19 +1711,19 @@ pub mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for ItemForeignMod { fn parse(input: ParseStream) -> Result<Self> { - let outer_attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let abi: Abi = input.parse()?; let content; let brace_token = braced!(content in input); - let inner_attrs = content.call(Attribute::parse_inner)?; + attr::parsing::parse_inner(&content, &mut attrs)?; let mut items = Vec::new(); while !content.is_empty() { items.push(content.parse()?); } Ok(ItemForeignMod { - attrs: private::attrs(outer_attrs, inner_attrs), + attrs, abi, brace_token, items, @@ -2009,12 +2009,13 @@ pub mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for ItemStruct { fn parse(input: ParseStream) -> Result<Self> { - let attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let vis = input.parse::<Visibility>()?; let struct_token = input.parse::<Token![struct]>()?; let ident = input.parse::<Ident>()?; let generics = input.parse::<Generics>()?; - let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?; + let (where_clause, fields, semi_token) = + derive::parsing::data_struct(input, &mut attrs)?; Ok(ItemStruct { attrs, vis, @@ -2033,12 +2034,13 @@ pub mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for ItemEnum { fn parse(input: ParseStream) -> Result<Self> { - let attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let vis = input.parse::<Visibility>()?; let enum_token = input.parse::<Token![enum]>()?; let ident = input.parse::<Ident>()?; let generics = input.parse::<Generics>()?; - let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?; + let (where_clause, brace_token, variants) = + derive::parsing::data_enum(input, &mut attrs)?; Ok(ItemEnum { attrs, vis, @@ -2057,12 +2059,12 @@ pub mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for ItemUnion { fn parse(input: ParseStream) -> Result<Self> { - let attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let vis = input.parse::<Visibility>()?; let union_token = input.parse::<Token![union]>()?; let ident = input.parse::<Ident>()?; let generics = input.parse::<Generics>()?; - let (where_clause, fields) = derive::parsing::data_union(input)?; + let (where_clause, fields) = derive::parsing::data_union(input, &mut attrs)?; Ok(ItemUnion { attrs, vis, @@ -2130,7 +2132,7 @@ pub mod parsing { fn parse_rest_of_trait( input: ParseStream, - outer_attrs: Vec<Attribute>, + mut attrs: Vec<Attribute>, vis: Visibility, unsafety: Option<Token![unsafe]>, auto_token: Option<Token![auto]>, @@ -2158,14 +2160,14 @@ pub mod parsing { let content; let brace_token = braced!(content in input); - let inner_attrs = content.call(Attribute::parse_inner)?; + attr::parsing::parse_inner(&content, &mut attrs)?; let mut items = Vec::new(); while !content.is_empty() { items.push(content.parse()?); } Ok(ItemTrait { - attrs: private::attrs(outer_attrs, inner_attrs), + attrs, vis, unsafety, auto_token, @@ -2330,25 +2332,25 @@ pub mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for TraitItemMethod { fn parse(input: ParseStream) -> Result<Self> { - let outer_attrs = input.call(Attribute::parse_outer)?; + let mut attrs = input.call(Attribute::parse_outer)?; let sig: Signature = input.parse()?; let lookahead = input.lookahead1(); - let (brace_token, inner_attrs, stmts, semi_token) = if lookahead.peek(token::Brace) { + let (brace_token, stmts, semi_token) = if lookahead.peek(token::Brace) { let content; let brace_token = braced!(content in input); - let inner_attrs = content.call(Attribute::parse_inner)?; + attr::parsing::parse_inner(&content, &mut attrs)?; let stmts = content.call(Block::parse_within)?; - (Some(brace_token), inner_attrs, stmts, None) + (Some(brace_token), stmts, None) } else if lookahead.peek(Token![;]) { let semi_token: Token![;] = input.parse()?; - (None, Vec::new(), Vec::new(), Some(semi_token)) + (None, Vec::new(), Some(semi_token)) } else { return Err(lookahead.error()); }; Ok(TraitItemMethod { - attrs: private::attrs(outer_attrs, inner_attrs), + attrs, sig, default: brace_token.map(|brace_token| Block { brace_token, stmts }), semi_token, @@ -2449,13 +2451,14 @@ pub mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for ItemImpl { fn parse(input: ParseStream) -> Result<Self> { - let allow_const_impl = false; - parse_impl(input, allow_const_impl).map(Option::unwrap) + let allow_verbatim_impl = false; + parse_impl(input, allow_verbatim_impl).map(Option::unwrap) } } - fn parse_impl(input: ParseStream, allow_const_impl: bool) -> Result<Option<ItemImpl>> { - let outer_attrs = input.call(Attribute::parse_outer)?; + fn parse_impl(input: ParseStream, allow_verbatim_impl: bool) -> Result<Option<ItemImpl>> { + let mut attrs = input.call(Attribute::parse_outer)?; + let has_visibility = allow_verbatim_impl && input.parse::<Visibility>()?.is_some(); let defaultness: Option<Token![default]> = input.parse()?; let unsafety: Option<Token![unsafe]> = input.parse()?; let impl_token: Token![impl] = input.parse()?; @@ -2466,7 +2469,8 @@ pub mod parsing { || (input.peek2(Ident) || input.peek2(Lifetime)) && (input.peek3(Token![:]) || input.peek3(Token![,]) - || input.peek3(Token![>])) + || input.peek3(Token![>]) + || input.peek3(Token![=])) || input.peek2(Token![const])); let mut generics: Generics = if has_generics { input.parse()? @@ -2474,7 +2478,7 @@ pub mod parsing { Generics::default() }; - let is_const_impl = allow_const_impl + let is_const_impl = allow_verbatim_impl && (input.peek(Token![const]) || input.peek(Token![?]) && input.peek2(Token![const])); if is_const_impl { input.parse::<Option<Token![?]>>()?; @@ -2488,6 +2492,8 @@ pub mod parsing { None }; + #[cfg(not(feature = "printing"))] + let first_ty_span = input.span(); let mut first_ty: Type = input.parse()?; let self_ty: Type; let trait_; @@ -2508,6 +2514,11 @@ pub mod parsing { } else { unreachable!() } + } else if !allow_verbatim_impl { + #[cfg(feature = "printing")] + return Err(Error::new_spanned(first_ty_ref, "expected trait path")); + #[cfg(not(feature = "printing"))] + return Err(Error::new(first_ty_span, "expected trait path")); } else { trait_ = None; } @@ -2525,18 +2536,18 @@ pub mod parsing { let content; let brace_token = braced!(content in input); - let inner_attrs = content.call(Attribute::parse_inner)?; + attr::parsing::parse_inner(&content, &mut attrs)?; let mut items = Vec::new(); while !content.is_empty() { items.push(content.parse()?); } - if is_const_impl || is_impl_for && trait_.is_none() { + if has_visibility || is_const_impl || is_impl_for && trait_.is_none() { Ok(None) } else { Ok(Some(ItemImpl { - attrs: private::attrs(outer_attrs, inner_attrs), + attrs, defaultness, unsafety, impl_token, @@ -250,7 +250,7 @@ //! dynamic library libproc_macro from rustc toolchain. // Syn types in rustdoc of other crates get linked to here. -#![doc(html_root_url = "https://docs.rs/syn/1.0.69")] +#![doc(html_root_url = "https://docs.rs/syn/1.0.72")] #![cfg_attr(doc_cfg, feature(doc_cfg))] #![allow(non_camel_case_types)] // Ignored clippy lints. @@ -274,6 +274,8 @@ // Ignored clippy_pedantic lints. #![allow( clippy::cast_possible_truncation, + // clippy bug: https://github.com/rust-lang/rust-clippy/issues/7127 + clippy::cloned_instead_of_copied, clippy::default_trait_access, clippy::empty_enum, clippy::expl_impl_clone_on_copy, @@ -40,7 +40,7 @@ ast_enum_of_structs! { /// A floating point literal: `1f64` or `1.0e10f64`. /// - /// Must be finite. May not be infinte or NaN. + /// Must be finite. May not be infinite or NaN. Float(LitFloat), /// A boolean literal: `true` or `false`. diff --git a/src/path.rs b/src/path.rs index df1788db..37e41bc4 100644 --- a/src/path.rs +++ b/src/path.rs @@ -292,6 +292,18 @@ pub mod parsing { return Ok(Expr::Lit(lit)); } + #[cfg(feature = "full")] + { + if input.peek(Ident) { + let ident: Ident = input.parse()?; + return Ok(Expr::Path(ExprPath { + attrs: Vec::new(), + qself: None, + path: Path::from(ident), + })); + } + } + if input.peek(token::Brace) { #[cfg(feature = "full")] { @@ -341,7 +341,6 @@ pub mod parsing { use crate::parse::{Parse, ParseStream, Result}; use crate::path; use proc_macro2::{Punct, Spacing, TokenTree}; - use std::iter::FromIterator; #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for Type { @@ -1009,12 +1008,14 @@ pub mod parsing { TokenTree::Punct(Punct::new('.', Spacing::Joint)), TokenTree::Punct(Punct::new('.', Spacing::Alone)), ]; - let tokens = TokenStream::from_iter(args.into_iter().zip(&dot3.spans).map( - |(mut arg, span)| { + let tokens: TokenStream = args + .into_iter() + .zip(&dot3.spans) + .map(|(mut arg, span)| { arg.set_span(*span); arg - }, - )); + }) + .collect(); Type::Verbatim(tokens) } else if allow_mut_self && input.peek(Token![mut]) && input.peek2(Token![self]) { has_mut_self = true; diff --git a/tests/common/eq.rs b/tests/common/eq.rs index 944d5851..d632db83 100644 --- a/tests/common/eq.rs +++ b/tests/common/eq.rs @@ -22,7 +22,10 @@ use rustc_ast::ast::{ }; use rustc_ast::ptr::P; use rustc_ast::token::{self, CommentKind, DelimToken, Nonterminal, Token, TokenKind}; -use rustc_ast::tokenstream::{DelimSpan, LazyTokenStream, TokenStream, TokenTree}; +use rustc_ast::tokenstream::{ + AttrAnnotatedTokenStream, AttrAnnotatedTokenTree, AttributesData, DelimSpan, LazyTokenStream, + Spacing, TokenStream, TokenTree, +}; use rustc_data_structures::sync::Lrc; use rustc_data_structures::thin_vec::ThinVec; use rustc_span::source_map::Spanned; @@ -110,6 +113,7 @@ spanless_eq_true!(DelimSpan); spanless_eq_true!(AttrId); spanless_eq_true!(NodeId); spanless_eq_true!(SyntaxContext); +spanless_eq_true!(Spacing); macro_rules! spanless_eq_partial_eq { ($name:ty) => { @@ -285,8 +289,10 @@ spanless_eq_struct!(AngleBracketedArgs; span args); spanless_eq_struct!(AnonConst; id value); spanless_eq_struct!(Arm; attrs pat guard body span id is_placeholder); spanless_eq_struct!(AssocTyConstraint; id ident gen_args kind span); +spanless_eq_struct!(AttrAnnotatedTokenStream; 0); spanless_eq_struct!(AttrItem; path args tokens); spanless_eq_struct!(Attribute; kind id style span); +spanless_eq_struct!(AttributesData; attrs tokens); spanless_eq_struct!(BareFnTy; unsafety ext generic_params decl); spanless_eq_struct!(Block; stmts id rules span tokens); spanless_eq_struct!(Crate; attrs items span proc_macros); @@ -342,6 +348,7 @@ spanless_eq_enum!(AngleBracketedArg; Arg(0) Constraint(0)); spanless_eq_enum!(AssocItemKind; Const(0 1 2) Fn(0) TyAlias(0) MacCall(0)); spanless_eq_enum!(AssocTyConstraintKind; Equality(ty) Bound(bounds)); spanless_eq_enum!(Async; Yes(span closure_id return_impl_trait_id) No); +spanless_eq_enum!(AttrAnnotatedTokenTree; Token(0) Delimited(0 1 2) Attributes(0)); spanless_eq_enum!(AttrStyle; Outer Inner); spanless_eq_enum!(BinOpKind; Add Sub Mul Div Rem And Or BitXor BitAnd BitOr Shl Shr Eq Lt Le Ne Ge Gt); spanless_eq_enum!(BindingMode; ByRef(0) ByValue(0)); diff --git a/tests/repo/mod.rs b/tests/repo/mod.rs index 24b620c5..3964176d 100644 --- a/tests/repo/mod.rs +++ b/tests/repo/mod.rs @@ -8,10 +8,14 @@ use std::path::Path; use tar::Archive; use walkdir::DirEntry; -const REVISION: &str = "52e3dffa50cfffdcfa145c0cc0ba48b49abc0c07"; +const REVISION: &str = "716394d6581b60c75cfdd88b8e5b876f2db88b62"; #[rustfmt::skip] static EXCLUDE: &[&str] = &[ + // Rustc loses some attributes + // https://github.com/rust-lang/rust/issues/84879 + "src/test/ui/proc-macro/issue-81555.rs", + // Compile-fail expr parameter in const generic position: f::<1 + 2>() "src/test/ui/const-generics/closing-args-token.rs", "src/test/ui/const-generics/const-expression-parameter.rs", diff --git a/tests/test_expr.rs b/tests/test_expr.rs index b2b65a25..8c1cc73a 100644 --- a/tests/test_expr.rs +++ b/tests/test_expr.rs @@ -300,3 +300,21 @@ fn test_macro_variable_match_arm() { } "###); } + +// https://github.com/dtolnay/syn/issues/1019 +#[test] +fn test_closure_vs_rangefull() { + #[rustfmt::skip] // rustfmt bug: https://github.com/rust-lang/rustfmt/issues/4808 + let tokens = quote!(|| .. .method()); + snapshot!(tokens as Expr, @r###" + Expr::MethodCall { + receiver: Expr::Closure { + output: Default, + body: Expr::Range { + limits: HalfOpen, + }, + }, + method: "method", + } + "###); +} diff --git a/tests/test_item.rs b/tests/test_item.rs index 712c6c6f..a991e62d 100644 --- a/tests/test_item.rs +++ b/tests/test_item.rs @@ -266,3 +266,36 @@ fn test_type_empty_bounds() { } "###); } + +#[test] +fn test_impl_visibility() { + let tokens = quote! { + pub default unsafe impl union {} + }; + + snapshot!(tokens as Item, @"Verbatim(`pub default unsafe impl union { }`)"); +} + +#[test] +fn test_impl_type_parameter_defaults() { + #[cfg(any())] + impl<T = ()> () {} + let tokens = quote! { + impl<T = ()> () {} + }; + snapshot!(tokens as Item, @r###" + Item::Impl { + generics: Generics { + lt_token: Some, + params: [ + Type(TypeParam { + ident: "T", + eq_token: Some, + default: Some(Type::Tuple), + }), + ], + gt_token: Some, + }, + self_ty: Type::Tuple, + }"###); +} |