From 1259995732470ede4b622d8ded6e3faa33370430 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Tue, 14 Mar 2023 23:15:38 -0700 Subject: [PATCH] Update to syn 2 --- gen/build/Cargo.toml | 2 +- gen/cmd/Cargo.toml | 2 +- gen/lib/Cargo.toml | 2 +- gen/src/file.rs | 6 +- macro/Cargo.toml | 2 +- macro/src/expand.rs | 14 +- syntax/attrs.rs | 151 ++++++++------- syntax/cfg.rs | 14 +- syntax/check.rs | 4 +- syntax/namespace.rs | 37 +++- syntax/parse.rs | 172 +++++++++--------- syntax/tokens.rs | 4 +- tests/ui/include.stderr | 4 +- third-party/BUCK | 155 ++++++---------- third-party/Cargo.lock | 4 +- third-party/Cargo.toml | 2 +- third-party/bazel/BUILD.bazel | 2 +- ...yn-1.0.109.bazel => BUILD.syn-2.0.0.bazel} | 52 +----- third-party/bazel/defs.bzl | 12 +- 19 files changed, 299 insertions(+), 342 deletions(-) rename third-party/bazel/{BUILD.syn-1.0.109.bazel => BUILD.syn-2.0.0.bazel} (55%) diff --git a/gen/build/Cargo.toml b/gen/build/Cargo.toml index c53568061..64c5dd130 100644 --- a/gen/build/Cargo.toml +++ b/gen/build/Cargo.toml @@ -25,7 +25,7 @@ once_cell = "1.9" proc-macro2 = { version = "1.0.39", default-features = false, features = ["span-locations"] } quote = { version = "1.0", default-features = false } scratch = "1.0" -syn = { version = "1.0.95", default-features = false, features = ["parsing", "printing", "clone-impls", "full"] } +syn = { version = "2.0.0", default-features = false, features = ["parsing", "printing", "clone-impls", "full"] } [dev-dependencies] cxx = { version = "1.0", path = "../.." } diff --git a/gen/cmd/Cargo.toml b/gen/cmd/Cargo.toml index dd0133308..03668456a 100644 --- a/gen/cmd/Cargo.toml +++ b/gen/cmd/Cargo.toml @@ -25,7 +25,7 @@ clap = { version = "4", default-features = false, features = ["error-context", " codespan-reporting = "0.11" proc-macro2 = { version = "1.0.39", default-features = false, features = ["span-locations"] } quote = { version = "1.0", default-features = false } -syn = { version = "1.0.95", default-features = false, features = ["parsing", "printing", "clone-impls", "full"] } +syn = { version = "2.0.0", default-features = false, features = ["parsing", "printing", "clone-impls", "full"] } [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] diff --git a/gen/lib/Cargo.toml b/gen/lib/Cargo.toml index af05b70ae..99751e01a 100644 --- a/gen/lib/Cargo.toml +++ b/gen/lib/Cargo.toml @@ -15,7 +15,7 @@ rust-version = "1.60" codespan-reporting = "0.11" proc-macro2 = { version = "1.0.39", default-features = false, features = ["span-locations"] } quote = { version = "1.0", default-features = false } -syn = { version = "1.0.95", default-features = false, features = ["parsing", "printing", "clone-impls", "full"] } +syn = { version = "2.0.0", default-features = false, features = ["parsing", "printing", "clone-impls", "full"] } [lib] doc-scrape-examples = false diff --git a/gen/src/file.rs b/gen/src/file.rs index 46616fbda..4e4259ef9 100644 --- a/gen/src/file.rs +++ b/gen/src/file.rs @@ -2,7 +2,7 @@ use crate::syntax::file::Module; use crate::syntax::namespace::Namespace; use syn::parse::discouraged::Speculative; use syn::parse::{Error, Parse, ParseStream, Result}; -use syn::{braced, Attribute, Ident, Item, Token, Visibility}; +use syn::{braced, Attribute, Ident, Item, Meta, Token, Visibility}; pub struct File { pub modules: Vec, @@ -23,7 +23,7 @@ fn parse(input: ParseStream, modules: &mut Vec) -> Result<()> { let mut namespace = Namespace::ROOT; let mut attrs = input.call(Attribute::parse_outer)?; for attr in &attrs { - let path = &attr.path.segments; + let path = &attr.path().segments; if path.len() == 2 && path[0].ident == "cxx" && path[1].ident == "bridge" { cxx_bridge = true; namespace = parse_args(attr)?; @@ -64,7 +64,7 @@ fn parse(input: ParseStream, modules: &mut Vec) -> Result<()> { } fn parse_args(attr: &Attribute) -> Result { - if attr.tokens.is_empty() { + if let Meta::Path(_) = attr.meta { Ok(Namespace::ROOT) } else { attr.parse_args_with(Namespace::parse_bridge_attr_namespace) diff --git a/macro/Cargo.toml b/macro/Cargo.toml index a98af2466..100eeb553 100644 --- a/macro/Cargo.toml +++ b/macro/Cargo.toml @@ -23,7 +23,7 @@ experimental-enum-variants-from-header = ["clang-ast", "flate2", "memmap", "serd [dependencies] proc-macro2 = "1.0.39" quote = "1.0.4" -syn = { version = "1.0.95", features = ["full"] } +syn = { version = "2.0.0", features = ["full"] } # optional dependencies: clang-ast = { version = "0.1", optional = true } diff --git a/macro/src/expand.rs b/macro/src/expand.rs index ea5af66a4..bd0a20637 100644 --- a/macro/src/expand.rs +++ b/macro/src/expand.rs @@ -1264,7 +1264,7 @@ fn expand_rust_box(key: NamedImplKey, types: &Types, explicit_impl: Option<&Impl let (impl_generics, ty_generics) = generics::split_for_impl(key, explicit_impl, resolve); let begin_span = explicit_impl.map_or(key.begin_span, |explicit| explicit.impl_token.span); - let end_span = explicit_impl.map_or(key.end_span, |explicit| explicit.brace_token.span); + let end_span = explicit_impl.map_or(key.end_span, |explicit| explicit.brace_token.span.join()); let unsafe_token = format_ident!("unsafe", span = begin_span); let prevent_unwind_drop_label = format!("::{} as Drop>::drop", ident); @@ -1322,7 +1322,7 @@ fn expand_rust_vec(key: NamedImplKey, types: &Types, explicit_impl: Option<&Impl let (impl_generics, ty_generics) = generics::split_for_impl(key, explicit_impl, resolve); let begin_span = explicit_impl.map_or(key.begin_span, |explicit| explicit.impl_token.span); - let end_span = explicit_impl.map_or(key.end_span, |explicit| explicit.brace_token.span); + let end_span = explicit_impl.map_or(key.end_span, |explicit| explicit.brace_token.span.join()); let unsafe_token = format_ident!("unsafe", span = begin_span); let prevent_unwind_drop_label = format!("::{} as Drop>::drop", elem); @@ -1416,7 +1416,7 @@ fn expand_unique_ptr( }; let begin_span = explicit_impl.map_or(key.begin_span, |explicit| explicit.impl_token.span); - let end_span = explicit_impl.map_or(key.end_span, |explicit| explicit.brace_token.span); + let end_span = explicit_impl.map_or(key.end_span, |explicit| explicit.brace_token.span.join()); let unsafe_token = format_ident!("unsafe", span = begin_span); quote_spanned! {end_span=> @@ -1501,7 +1501,7 @@ fn expand_shared_ptr( }; let begin_span = explicit_impl.map_or(key.begin_span, |explicit| explicit.impl_token.span); - let end_span = explicit_impl.map_or(key.end_span, |explicit| explicit.brace_token.span); + let end_span = explicit_impl.map_or(key.end_span, |explicit| explicit.brace_token.span.join()); let unsafe_token = format_ident!("unsafe", span = begin_span); quote_spanned! {end_span=> @@ -1556,7 +1556,7 @@ fn expand_weak_ptr(key: NamedImplKey, types: &Types, explicit_impl: Option<&Impl let (impl_generics, ty_generics) = generics::split_for_impl(key, explicit_impl, resolve); let begin_span = explicit_impl.map_or(key.begin_span, |explicit| explicit.impl_token.span); - let end_span = explicit_impl.map_or(key.end_span, |explicit| explicit.brace_token.span); + let end_span = explicit_impl.map_or(key.end_span, |explicit| explicit.brace_token.span.join()); let unsafe_token = format_ident!("unsafe", span = begin_span); quote_spanned! {end_span=> @@ -1629,7 +1629,7 @@ fn expand_cxx_vector( let (impl_generics, ty_generics) = generics::split_for_impl(key, explicit_impl, resolve); let begin_span = explicit_impl.map_or(key.begin_span, |explicit| explicit.impl_token.span); - let end_span = explicit_impl.map_or(key.end_span, |explicit| explicit.brace_token.span); + let end_span = explicit_impl.map_or(key.end_span, |explicit| explicit.brace_token.span.join()); let unsafe_token = format_ident!("unsafe", span = begin_span); let can_pass_element_by_value = types.is_maybe_trivial(elem); @@ -1810,7 +1810,7 @@ fn expand_extern_type(ty: &Type, types: &Types, proper: bool) -> TokenStream { } Type::SliceRef(ty) => { let span = ty.ampersand.span; - let rust_slice = Ident::new("RustSlice", ty.bracket.span); + let rust_slice = Ident::new("RustSlice", ty.bracket.span.join()); quote_spanned!(span=> ::cxx::private::#rust_slice) } _ => quote!(#ty), diff --git a/syntax/attrs.rs b/syntax/attrs.rs index 46d010e0a..1b8e579bd 100644 --- a/syntax/attrs.rs +++ b/syntax/attrs.rs @@ -5,8 +5,8 @@ use crate::syntax::Atom::{self, *}; use crate::syntax::{cfg, Derive, Doc, ForeignName}; use proc_macro2::{Ident, TokenStream}; use quote::ToTokens; -use syn::parse::{Nothing, Parse, ParseStream, Parser as _}; -use syn::{parenthesized, token, Attribute, Error, LitStr, Path, Result, Token}; +use syn::parse::ParseStream; +use syn::{Attribute, Error, Expr, Lit, LitStr, Meta, Path, Result, Token}; // Intended usage: // @@ -47,8 +47,9 @@ pub struct Parser<'a> { pub fn parse(cx: &mut Errors, attrs: Vec, mut parser: Parser) -> OtherAttrs { let mut passthrough_attrs = Vec::new(); for attr in attrs { - if attr.path.is_ident("doc") { - match parse_doc_attribute.parse2(attr.tokens.clone()) { + let attr_path = attr.path(); + if attr_path.is_ident("doc") { + match parse_doc_attribute(&attr.meta) { Ok(attr) => { if let Some(doc) = &mut parser.doc { match attr { @@ -63,7 +64,7 @@ pub fn parse(cx: &mut Errors, attrs: Vec, mut parser: Parser) -> Othe break; } } - } else if attr.path.is_ident("derive") { + } else if attr_path.is_ident("derive") { match attr.parse_args_with(|attr: ParseStream| parse_derive_attribute(cx, attr)) { Ok(attr) => { if let Some(derives) = &mut parser.derives { @@ -76,7 +77,7 @@ pub fn parse(cx: &mut Errors, attrs: Vec, mut parser: Parser) -> Othe break; } } - } else if attr.path.is_ident("repr") { + } else if attr_path.is_ident("repr") { match attr.parse_args_with(parse_repr_attribute) { Ok(attr) => { if let Some(repr) = &mut parser.repr { @@ -89,8 +90,8 @@ pub fn parse(cx: &mut Errors, attrs: Vec, mut parser: Parser) -> Othe break; } } - } else if attr.path.is_ident("namespace") { - match parse_namespace_attribute.parse2(attr.tokens.clone()) { + } else if attr_path.is_ident("namespace") { + match Namespace::parse_meta(&attr.meta) { Ok(attr) => { if let Some(namespace) = &mut parser.namespace { **namespace = attr; @@ -102,8 +103,8 @@ pub fn parse(cx: &mut Errors, attrs: Vec, mut parser: Parser) -> Othe break; } } - } else if attr.path.is_ident("cxx_name") { - match parse_cxx_name_attribute.parse2(attr.tokens.clone()) { + } else if attr_path.is_ident("cxx_name") { + match parse_cxx_name_attribute(&attr.meta) { Ok(attr) => { if let Some(cxx_name) = &mut parser.cxx_name { **cxx_name = Some(attr); @@ -115,8 +116,8 @@ pub fn parse(cx: &mut Errors, attrs: Vec, mut parser: Parser) -> Othe break; } } - } else if attr.path.is_ident("rust_name") { - match parse_rust_name_attribute.parse2(attr.tokens.clone()) { + } else if attr_path.is_ident("rust_name") { + match parse_rust_name_attribute(&attr.meta) { Ok(attr) => { if let Some(rust_name) = &mut parser.rust_name { **rust_name = Some(attr); @@ -128,8 +129,8 @@ pub fn parse(cx: &mut Errors, attrs: Vec, mut parser: Parser) -> Othe break; } } - } else if attr.path.is_ident("cfg") { - match cfg::parse_attribute.parse2(attr.tokens.clone()) { + } else if attr_path.is_ident("cfg") { + match cfg::parse_attribute(&attr) { Ok(cfg_expr) => { if let Some(cfg) = &mut parser.cfg { cfg.merge(cfg_expr); @@ -142,31 +143,31 @@ pub fn parse(cx: &mut Errors, attrs: Vec, mut parser: Parser) -> Othe break; } } - } else if attr.path.is_ident("variants_from_header") + } else if attr_path.is_ident("variants_from_header") && cfg!(feature = "experimental-enum-variants-from-header") { - if let Err(err) = Nothing::parse.parse2(attr.tokens.clone()) { + if let Err(err) = require_empty_attribute(&attr.meta) { cx.push(err); } if let Some(variants_from_header) = &mut parser.variants_from_header { **variants_from_header = Some(attr); continue; } - } else if attr.path.is_ident("allow") - || attr.path.is_ident("warn") - || attr.path.is_ident("deny") - || attr.path.is_ident("forbid") - || attr.path.is_ident("deprecated") - || attr.path.is_ident("must_use") + } else if attr_path.is_ident("allow") + || attr_path.is_ident("warn") + || attr_path.is_ident("deny") + || attr_path.is_ident("forbid") + || attr_path.is_ident("deprecated") + || attr_path.is_ident("must_use") { // https://doc.rust-lang.org/reference/attributes/diagnostics.html passthrough_attrs.push(attr); continue; - } else if attr.path.is_ident("serde") { + } else if attr_path.is_ident("serde") { passthrough_attrs.push(attr); continue; - } else if attr.path.segments.len() > 1 { - let tool = &attr.path.segments.first().unwrap().ident; + } else if attr_path.segments.len() > 1 { + let tool = &attr_path.segments.first().unwrap().ident; if tool == "rustfmt" { // Skip, rustfmt only needs to find it in the pre-expansion source file. continue; @@ -192,24 +193,26 @@ mod kw { syn::custom_keyword!(hidden); } -fn parse_doc_attribute(input: ParseStream) -> Result { - let lookahead = input.lookahead1(); - if lookahead.peek(Token![=]) { - input.parse::()?; - let lit: LitStr = input.parse()?; - Ok(DocAttribute::Doc(lit)) - } else if lookahead.peek(token::Paren) { - let content; - parenthesized!(content in input); - content.parse::()?; - Ok(DocAttribute::Hidden) - } else { - Err(lookahead.error()) +fn parse_doc_attribute(meta: &Meta) -> Result { + match meta { + Meta::NameValue(meta) => { + if let Expr::Lit(expr) = &meta.value { + if let Lit::Str(lit) = &expr.lit { + return Ok(DocAttribute::Doc(lit.clone())); + } + } + } + Meta::List(meta) => { + meta.parse_args::()?; + return Ok(DocAttribute::Hidden); + } + Meta::Path(_) => {} } + Err(Error::new_spanned(meta, "unsupported doc attribute")) } fn parse_derive_attribute(cx: &mut Errors, input: ParseStream) -> Result> { - let paths = input.parse_terminated::(Path::parse_mod_style)?; + let paths = input.parse_terminated(Path::parse_mod_style, Token![,])?; let mut derives = Vec::new(); for path in paths { @@ -241,31 +244,42 @@ fn parse_repr_attribute(input: ParseStream) -> Result { )) } -fn parse_namespace_attribute(input: ParseStream) -> Result { - input.parse::()?; - let namespace = input.parse::()?; - Ok(namespace) -} - -fn parse_cxx_name_attribute(input: ParseStream) -> Result { - input.parse::()?; - if input.peek(LitStr) { - let lit: LitStr = input.parse()?; - ForeignName::parse(&lit.value(), lit.span()) - } else { - let ident: Ident = input.parse()?; - ForeignName::parse(&ident.to_string(), ident.span()) +fn parse_cxx_name_attribute(meta: &Meta) -> Result { + if let Meta::NameValue(meta) = meta { + match &meta.value { + Expr::Lit(expr) => { + if let Lit::Str(lit) = &expr.lit { + return ForeignName::parse(&lit.value(), lit.span()); + } + } + Expr::Path(expr) => { + if let Some(ident) = expr.path.get_ident() { + return ForeignName::parse(&ident.to_string(), ident.span()); + } + } + _ => {} + } } + Err(Error::new_spanned(meta, "unsupported cxx_name attribute")) } -fn parse_rust_name_attribute(input: ParseStream) -> Result { - input.parse::()?; - if input.peek(LitStr) { - let lit: LitStr = input.parse()?; - lit.parse() - } else { - input.parse() +fn parse_rust_name_attribute(meta: &Meta) -> Result { + if let Meta::NameValue(meta) = meta { + match &meta.value { + Expr::Lit(expr) => { + if let Lit::Str(lit) = &expr.lit { + return lit.parse(); + } + } + Expr::Path(expr) => { + if let Some(ident) = expr.path.get_ident() { + return Ok(ident.clone()); + } + } + _ => {} + } } + Err(Error::new_spanned(meta, "unsupported rust_name attribute")) } #[derive(Clone)] @@ -288,15 +302,20 @@ impl ToTokens for OtherAttrs { pound_token, style, bracket_token, - path, - tokens: attr_tokens, + meta, } = attr; pound_token.to_tokens(tokens); let _ = style; // ignore; render outer and inner attrs both as outer - bracket_token.surround(tokens, |tokens| { - path.to_tokens(tokens); - attr_tokens.to_tokens(tokens); - }); + bracket_token.surround(tokens, |tokens| meta.to_tokens(tokens)); } } } + +fn require_empty_attribute(meta: &Meta) -> Result<()> { + let error_span = match meta { + Meta::Path(_) => return Ok(()), + Meta::List(meta) => meta.delimiter.span().open(), + Meta::NameValue(meta) => meta.eq_token.span, + }; + Err(Error::new(error_span, "unexpected token in cxx attribute")) +} diff --git a/syntax/cfg.rs b/syntax/cfg.rs index d486b9958..ce6f33895 100644 --- a/syntax/cfg.rs +++ b/syntax/cfg.rs @@ -1,7 +1,7 @@ use proc_macro2::Ident; use std::mem; use syn::parse::{Error, ParseStream, Result}; -use syn::{parenthesized, token, LitStr, Token}; +use syn::{parenthesized, token, Attribute, LitStr, Token}; #[derive(Clone)] pub enum CfgExpr { @@ -25,12 +25,12 @@ impl CfgExpr { } } -pub fn parse_attribute(input: ParseStream) -> Result { - let content; - parenthesized!(content in input); - let cfg_expr = content.call(parse_single)?; - content.parse::>()?; - Ok(cfg_expr) +pub fn parse_attribute(attr: &Attribute) -> Result { + attr.parse_args_with(|input: ParseStream| { + let cfg_expr = input.call(parse_single)?; + input.parse::>()?; + Ok(cfg_expr) + }) } fn parse_single(input: ParseStream) -> Result { diff --git a/syntax/check.rs b/syntax/check.rs index 66883be03..0770c8475 100644 --- a/syntax/check.rs +++ b/syntax/check.rs @@ -664,14 +664,14 @@ fn is_opaque_cxx(cx: &mut Check, ty: &Ident) -> bool { fn span_for_struct_error(strct: &Struct) -> TokenStream { let struct_token = strct.struct_token; let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new()); - brace_token.set_span(strct.brace_token.span); + brace_token.set_span(strct.brace_token.span.join()); quote!(#struct_token #brace_token) } fn span_for_enum_error(enm: &Enum) -> TokenStream { let enum_token = enm.enum_token; let mut brace_token = Group::new(Delimiter::Brace, TokenStream::new()); - brace_token.set_span(enm.brace_token.span); + brace_token.set_span(enm.brace_token.span.join()); quote!(#enum_token #brace_token) } diff --git a/syntax/namespace.rs b/syntax/namespace.rs index 07185e187..aae865ccf 100644 --- a/syntax/namespace.rs +++ b/syntax/namespace.rs @@ -3,8 +3,8 @@ use quote::IdentFragment; use std::fmt::{self, Display}; use std::iter::FromIterator; use std::slice::Iter; -use syn::parse::{Parse, ParseStream, Result}; -use syn::{Ident, Token}; +use syn::parse::{Error, Parse, ParseStream, Result}; +use syn::{Expr, Ident, Lit, Meta, Token}; mod kw { syn::custom_keyword!(namespace); @@ -24,7 +24,7 @@ impl Namespace { self.segments.iter() } - pub fn parse_bridge_attr_namespace(input: ParseStream) -> Result { + pub fn parse_bridge_attr_namespace(input: ParseStream) -> Result { if input.is_empty() { return Ok(Namespace::ROOT); } @@ -35,6 +35,37 @@ impl Namespace { input.parse::>()?; Ok(namespace) } + + pub fn parse_meta(meta: &Meta) -> Result { + if let Meta::NameValue(meta) = meta { + match &meta.value { + Expr::Lit(expr) => { + if let Lit::Str(lit) = &expr.lit { + let segments = QualifiedName::parse_quoted(lit)?.segments; + return Ok(Namespace { segments }); + } + } + Expr::Path(expr) + if expr.qself.is_none() + && expr + .path + .segments + .iter() + .all(|segment| segment.arguments.is_none()) => + { + let segments = expr + .path + .segments + .iter() + .map(|segment| segment.ident.clone()) + .collect(); + return Ok(Namespace { segments }); + } + _ => {} + } + } + Err(Error::new_spanned(meta, "unsupported namespace attribute")) + } } impl Default for &Namespace { diff --git a/syntax/parse.rs b/syntax/parse.rs index 1754c6006..c6fee5f86 100644 --- a/syntax/parse.rs +++ b/syntax/parse.rs @@ -489,11 +489,7 @@ fn parse_extern_type( let type_token = foreign_type.type_token; let visibility = visibility_pub(&foreign_type.vis, type_token.span); let name = pair(namespace, &foreign_type.ident, cxx_name, rust_name); - let generics = Lifetimes { - lt_token: None, - lifetimes: Punctuated::new(), - gt_token: None, - }; + let generics = extern_type_lifetimes(cx, foreign_type.generics); let colon_token = None; let bounds = Vec::new(); let semi_token = foreign_type.semi_token; @@ -611,7 +607,27 @@ fn parse_extern_fn( }); continue; } - return Err(Error::new_spanned(arg, "unsupported signature")); + if let Some(colon_token) = arg.colon_token { + let ty = parse_type(&arg.ty)?; + if let Type::Ref(reference) = ty { + if let Type::Ident(ident) = reference.inner { + receiver = Some(Receiver { + pinned: reference.pinned, + ampersand: reference.ampersand, + lifetime: reference.lifetime, + mutable: reference.mutable, + var: Token![self](ident.rust.span()), + colon_token, + ty: ident, + shorthand: false, + pin_tokens: reference.pin_tokens, + mutability: reference.mutability, + }); + continue; + } + } + } + return Err(Error::new_spanned(arg, "unsupported method receiver")); } FnArg::Typed(arg) => { let ident = match arg.pat.as_ref() { @@ -622,45 +638,24 @@ fn parse_extern_fn( _ => return Err(Error::new_spanned(arg, "unsupported signature")), }; let ty = parse_type(&arg.ty)?; - if ident != "self" { - let cfg = CfgExpr::Unconditional; - let doc = Doc::new(); - let attrs = OtherAttrs::none(); - let visibility = Token![pub](ident.span()); - let name = pair(Namespace::default(), &ident, None, None); - let colon_token = arg.colon_token; - args.push_value(Var { - cfg, - doc, - attrs, - visibility, - name, - colon_token, - ty, - }); - if let Some(comma) = comma { - args.push_punct(*comma); - } - continue; - } - if let Type::Ref(reference) = ty { - if let Type::Ident(ident) = reference.inner { - receiver = Some(Receiver { - pinned: reference.pinned, - ampersand: reference.ampersand, - lifetime: reference.lifetime, - mutable: reference.mutable, - var: Token![self](ident.rust.span()), - colon_token: arg.colon_token, - ty: ident, - shorthand: false, - pin_tokens: reference.pin_tokens, - mutability: reference.mutability, - }); - continue; - } + let cfg = CfgExpr::Unconditional; + let doc = Doc::new(); + let attrs = OtherAttrs::none(); + let visibility = Token![pub](ident.span()); + let name = pair(Namespace::default(), &ident, None, None); + let colon_token = arg.colon_token; + args.push_value(Var { + cfg, + doc, + attrs, + visibility, + name, + colon_token, + ty, + }); + if let Some(comma) = comma { + args.push_punct(*comma); } - return Err(Error::new_spanned(arg, "unsupported method receiver")); } } } @@ -756,6 +751,45 @@ fn parse_extern_verbatim_type( let type_token: Token![type] = input.parse()?; let ident: Ident = input.parse()?; let generics: Generics = input.parse()?; + let lifetimes = extern_type_lifetimes(cx, generics); + let lookahead = input.lookahead1(); + if lookahead.peek(Token![=]) { + // type Alias = crate::path::to::Type; + parse_type_alias( + cx, + unparsed_attrs, + visibility, + type_token, + ident, + lifetimes, + input, + lang, + extern_block_cfg, + namespace, + attrs, + ) + } else if lookahead.peek(Token![:]) { + // type Opaque: Bound2 + Bound2; + parse_extern_type_bounded( + cx, + unparsed_attrs, + visibility, + type_token, + ident, + lifetimes, + input, + lang, + trusted, + extern_block_cfg, + namespace, + attrs, + ) + } else { + Err(lookahead.error()) + } +} + +fn extern_type_lifetimes(cx: &mut Errors, generics: Generics) -> Lifetimes { let mut lifetimes = Punctuated::new(); let mut has_unsupported_generic_param = false; for pair in generics.params.into_pairs() { @@ -788,45 +822,10 @@ fn parse_extern_verbatim_type( } } } - let lifetimes = Lifetimes { + Lifetimes { lt_token: generics.lt_token, lifetimes, gt_token: generics.gt_token, - }; - let lookahead = input.lookahead1(); - if lookahead.peek(Token![=]) { - // type Alias = crate::path::to::Type; - parse_type_alias( - cx, - unparsed_attrs, - visibility, - type_token, - ident, - lifetimes, - input, - lang, - extern_block_cfg, - namespace, - attrs, - ) - } else if lookahead.peek(Token![:]) || lookahead.peek(Token![;]) { - // type Opaque: Bound2 + Bound2; - parse_extern_type_bounded( - cx, - unparsed_attrs, - visibility, - type_token, - ident, - lifetimes, - input, - lang, - trusted, - extern_block_cfg, - namespace, - attrs, - ) - } else { - Err(lookahead.error()) } } @@ -928,9 +927,7 @@ fn parse_extern_type_bounded( } else { false } => {} - bound @ TypeParamBound::Trait(_) | bound @ TypeParamBound::Lifetime(_) => { - cx.error(bound, "unsupported trait"); - } + bound => cx.error(bound, "unsupported trait"), } let lookahead = input.lookahead1(); @@ -1004,7 +1001,7 @@ fn parse_impl(cx: &mut Errors, imp: ItemImpl) -> Result { if !imp.items.is_empty() { let mut span = Group::new(Delimiter::Brace, TokenStream::new()); - span.set_span(imp.brace_token.span); + span.set_span(imp.brace_token.span.join()); return Err(Error::new_spanned(span, "expected an empty impl block")); } @@ -1151,7 +1148,7 @@ fn parse_type(ty: &RustType) -> Result { RustType::Path(ty) => parse_type_path(ty), RustType::Array(ty) => parse_type_array(ty), RustType::BareFn(ty) => parse_type_fn(ty), - RustType::Tuple(ty) if ty.elems.is_empty() => Ok(Type::Void(ty.paren_token.span)), + RustType::Tuple(ty) if ty.elems.is_empty() => Ok(Type::Void(ty.paren_token.span.join())), _ => Err(Error::new_spanned(ty, "unsupported type")), } } @@ -1387,7 +1384,7 @@ fn parse_type_fn(ty: &TypeBareFn) -> Result { let (ident, colon_token) = match &arg.name { Some((ident, colon_token)) => (ident.clone(), *colon_token), None => { - let fn_span = ty.paren_token.span; + let fn_span = ty.paren_token.span.join(); let ident = format_ident!("arg{}", i, span = fn_span); let colon_token = Token![:](fn_span); (ident, colon_token) @@ -1470,8 +1467,7 @@ fn parse_return_type( fn visibility_pub(vis: &Visibility, inherited: Span) -> Token![pub] { Token![pub](match vis { - Visibility::Public(vis) => vis.pub_token.span, - Visibility::Crate(vis) => vis.crate_token.span, + Visibility::Public(vis) => vis.span, Visibility::Restricted(vis) => vis.pub_token.span, Visibility::Inherited => inherited, }) diff --git a/syntax/tokens.rs b/syntax/tokens.rs index a9f42bd43..05eddc703 100644 --- a/syntax/tokens.rs +++ b/syntax/tokens.rs @@ -270,7 +270,7 @@ impl ToTokens for Signature { args.to_tokens(tokens); }); if let Some(ret) = ret { - Token![->](paren_token.span).to_tokens(tokens); + Token![->](paren_token.span.join()).to_tokens(tokens); if let Some((result, langle, rangle)) = throws_tokens { result.to_tokens(tokens); langle.to_tokens(tokens); @@ -280,7 +280,7 @@ impl ToTokens for Signature { ret.to_tokens(tokens); } } else if let Some((result, langle, rangle)) = throws_tokens { - Token![->](paren_token.span).to_tokens(tokens); + Token![->](paren_token.span.join()).to_tokens(tokens); result.to_tokens(tokens); langle.to_tokens(tokens); token::Paren(langle.span).surround(tokens, |_| ()); diff --git a/tests/ui/include.stderr b/tests/ui/include.stderr index 45cc55911..b801530e1 100644 --- a/tests/ui/include.stderr +++ b/tests/ui/include.stderr @@ -11,10 +11,10 @@ error: unexpected token | ^^^^ error: expected `>` - --> tests/ui/include.rs:6:17 + --> tests/ui/include.rs:6:26 | 6 | include!( tests/ui/include.rs:7:23 diff --git a/third-party/BUCK b/third-party/BUCK index cca5bf492..56c9c897f 100644 --- a/third-party/BUCK +++ b/third-party/BUCK @@ -378,69 +378,69 @@ third_party_rust_library( alias( name = "syn", - actual = ":syn-1.0.109", + actual = ":syn-2.0.0", visibility = ["PUBLIC"], ) third_party_rust_library( - name = "syn-1.0.109", + name = "syn-2.0.0", srcs = [ - "vendor/syn-1.0.109/src/attr.rs", - "vendor/syn-1.0.109/src/await.rs", - "vendor/syn-1.0.109/src/bigint.rs", - "vendor/syn-1.0.109/src/buffer.rs", - "vendor/syn-1.0.109/src/custom_keyword.rs", - "vendor/syn-1.0.109/src/custom_punctuation.rs", - "vendor/syn-1.0.109/src/data.rs", - "vendor/syn-1.0.109/src/derive.rs", - "vendor/syn-1.0.109/src/discouraged.rs", - "vendor/syn-1.0.109/src/drops.rs", - "vendor/syn-1.0.109/src/error.rs", - "vendor/syn-1.0.109/src/export.rs", - "vendor/syn-1.0.109/src/expr.rs", - "vendor/syn-1.0.109/src/ext.rs", - "vendor/syn-1.0.109/src/file.rs", - "vendor/syn-1.0.109/src/gen/clone.rs", - "vendor/syn-1.0.109/src/gen/debug.rs", - "vendor/syn-1.0.109/src/gen/eq.rs", - "vendor/syn-1.0.109/src/gen/fold.rs", - "vendor/syn-1.0.109/src/gen/hash.rs", - "vendor/syn-1.0.109/src/gen/visit.rs", - "vendor/syn-1.0.109/src/gen/visit_mut.rs", - "vendor/syn-1.0.109/src/gen_helper.rs", - "vendor/syn-1.0.109/src/generics.rs", - "vendor/syn-1.0.109/src/group.rs", - "vendor/syn-1.0.109/src/ident.rs", - "vendor/syn-1.0.109/src/item.rs", - "vendor/syn-1.0.109/src/lib.rs", - "vendor/syn-1.0.109/src/lifetime.rs", - "vendor/syn-1.0.109/src/lit.rs", - "vendor/syn-1.0.109/src/lookahead.rs", - "vendor/syn-1.0.109/src/mac.rs", - "vendor/syn-1.0.109/src/macros.rs", - "vendor/syn-1.0.109/src/op.rs", - "vendor/syn-1.0.109/src/parse.rs", - "vendor/syn-1.0.109/src/parse_macro_input.rs", - "vendor/syn-1.0.109/src/parse_quote.rs", - "vendor/syn-1.0.109/src/pat.rs", - "vendor/syn-1.0.109/src/path.rs", - "vendor/syn-1.0.109/src/print.rs", - "vendor/syn-1.0.109/src/punctuated.rs", - "vendor/syn-1.0.109/src/reserved.rs", - "vendor/syn-1.0.109/src/sealed.rs", - "vendor/syn-1.0.109/src/span.rs", - "vendor/syn-1.0.109/src/spanned.rs", - "vendor/syn-1.0.109/src/stmt.rs", - "vendor/syn-1.0.109/src/thread.rs", - "vendor/syn-1.0.109/src/token.rs", - "vendor/syn-1.0.109/src/tt.rs", - "vendor/syn-1.0.109/src/ty.rs", - "vendor/syn-1.0.109/src/verbatim.rs", - "vendor/syn-1.0.109/src/whitespace.rs", + "vendor/syn-2.0.0/src/attr.rs", + "vendor/syn-2.0.0/src/bigint.rs", + "vendor/syn-2.0.0/src/buffer.rs", + "vendor/syn-2.0.0/src/custom_keyword.rs", + "vendor/syn-2.0.0/src/custom_punctuation.rs", + "vendor/syn-2.0.0/src/data.rs", + "vendor/syn-2.0.0/src/derive.rs", + "vendor/syn-2.0.0/src/discouraged.rs", + "vendor/syn-2.0.0/src/drops.rs", + "vendor/syn-2.0.0/src/error.rs", + "vendor/syn-2.0.0/src/export.rs", + "vendor/syn-2.0.0/src/expr.rs", + "vendor/syn-2.0.0/src/ext.rs", + "vendor/syn-2.0.0/src/file.rs", + "vendor/syn-2.0.0/src/gen/clone.rs", + "vendor/syn-2.0.0/src/gen/debug.rs", + "vendor/syn-2.0.0/src/gen/eq.rs", + "vendor/syn-2.0.0/src/gen/fold.rs", + "vendor/syn-2.0.0/src/gen/hash.rs", + "vendor/syn-2.0.0/src/gen/visit.rs", + "vendor/syn-2.0.0/src/gen/visit_mut.rs", + "vendor/syn-2.0.0/src/gen_helper.rs", + "vendor/syn-2.0.0/src/generics.rs", + "vendor/syn-2.0.0/src/group.rs", + "vendor/syn-2.0.0/src/ident.rs", + "vendor/syn-2.0.0/src/item.rs", + "vendor/syn-2.0.0/src/lib.rs", + "vendor/syn-2.0.0/src/lifetime.rs", + "vendor/syn-2.0.0/src/lit.rs", + "vendor/syn-2.0.0/src/lookahead.rs", + "vendor/syn-2.0.0/src/mac.rs", + "vendor/syn-2.0.0/src/macros.rs", + "vendor/syn-2.0.0/src/meta.rs", + "vendor/syn-2.0.0/src/op.rs", + "vendor/syn-2.0.0/src/parse.rs", + "vendor/syn-2.0.0/src/parse_macro_input.rs", + "vendor/syn-2.0.0/src/parse_quote.rs", + "vendor/syn-2.0.0/src/pat.rs", + "vendor/syn-2.0.0/src/path.rs", + "vendor/syn-2.0.0/src/print.rs", + "vendor/syn-2.0.0/src/punctuated.rs", + "vendor/syn-2.0.0/src/restriction.rs", + "vendor/syn-2.0.0/src/sealed.rs", + "vendor/syn-2.0.0/src/span.rs", + "vendor/syn-2.0.0/src/spanned.rs", + "vendor/syn-2.0.0/src/stmt.rs", + "vendor/syn-2.0.0/src/thread.rs", + "vendor/syn-2.0.0/src/token.rs", + "vendor/syn-2.0.0/src/tt.rs", + "vendor/syn-2.0.0/src/ty.rs", + "vendor/syn-2.0.0/src/verbatim.rs", + "vendor/syn-2.0.0/src/whitespace.rs", ], crate = "syn", - crate_root = "vendor/syn-1.0.109/src/lib.rs", - edition = "2018", + crate_root = "vendor/syn-2.0.0/src/lib.rs", + edition = "2021", features = [ "clone-impls", "default", @@ -451,10 +451,7 @@ third_party_rust_library( "proc-macro", "quote", ], - rustc_flags = [ - "--cap-lints=allow", - "@$(location :syn-1.0.109-build-script-build-args)", - ], + rustc_flags = ["--cap-lints=allow"], visibility = [], deps = [ ":proc-macro2-1.0.52", @@ -463,44 +460,6 @@ third_party_rust_library( ], ) -rust_binary( - name = "syn-1.0.109-build-script-build", - srcs = ["vendor/syn-1.0.109/build.rs"], - crate = "build_script_build", - crate_root = "vendor/syn-1.0.109/build.rs", - edition = "2018", - features = [ - "clone-impls", - "default", - "derive", - "full", - "parsing", - "printing", - "proc-macro", - "quote", - ], - rustc_flags = ["--cap-lints=allow"], - visibility = [], -) - -buildscript_args( - name = "syn-1.0.109-build-script-build-args", - package_name = "syn", - buildscript_rule = ":syn-1.0.109-build-script-build", - features = [ - "clone-impls", - "default", - "derive", - "full", - "parsing", - "printing", - "proc-macro", - "quote", - ], - outfile = "args.txt", - version = "1.0.109", -) - third_party_rust_library( name = "termcolor-1.2.0", srcs = ["vendor/termcolor-1.2.0/src/lib.rs"], diff --git a/third-party/Cargo.lock b/third-party/Cargo.lock index eb9fdef40..c426a1ef2 100644 --- a/third-party/Cargo.lock +++ b/third-party/Cargo.lock @@ -81,9 +81,9 @@ checksum = "1792db035ce95be60c3f8853017b3999209281c24e2ba5bc8e59bf97a0c590c1" [[package]] name = "syn" -version = "1.0.109" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +checksum = "4cff13bb1732bccfe3b246f3fdb09edfd51c01d6f5299b7ccd9457c2e4e37774" dependencies = [ "proc-macro2", "quote", diff --git a/third-party/Cargo.toml b/third-party/Cargo.toml index 84657de58..ce3fc01e2 100644 --- a/third-party/Cargo.toml +++ b/third-party/Cargo.toml @@ -15,4 +15,4 @@ once_cell = "1.9" proc-macro2 = { version = "1.0.39", features = ["span-locations"] } quote = "1.0.4" scratch = "1" -syn = { version = "1.0.95", features = ["full"] } +syn = { version = "2.0.0", features = ["full"] } diff --git a/third-party/bazel/BUILD.bazel b/third-party/bazel/BUILD.bazel index 7a513ce9a..642c762fe 100644 --- a/third-party/bazel/BUILD.bazel +++ b/third-party/bazel/BUILD.bazel @@ -69,6 +69,6 @@ alias( alias( name = "syn", - actual = "@vendor__syn-1.0.109//:syn", + actual = "@vendor__syn-2.0.0//:syn", tags = ["manual"], ) diff --git a/third-party/bazel/BUILD.syn-1.0.109.bazel b/third-party/bazel/BUILD.syn-2.0.0.bazel similarity index 55% rename from third-party/bazel/BUILD.syn-1.0.109.bazel rename to third-party/bazel/BUILD.syn-2.0.0.bazel index eaf451347..9eba98722 100644 --- a/third-party/bazel/BUILD.syn-1.0.109.bazel +++ b/third-party/bazel/BUILD.syn-2.0.0.bazel @@ -6,7 +6,6 @@ # bazel run @//third-party:vendor ############################################################################### -load("@rules_rust//cargo:defs.bzl", "cargo_build_script") load("@rules_rust//rust:defs.bzl", "rust_library") package(default_visibility = ["//visibility:public"]) @@ -39,7 +38,7 @@ rust_library( "quote", ], crate_root = "src/lib.rs", - edition = "2018", + edition = "2021", rustc_flags = ["--cap-lints=allow"], tags = [ "cargo-bazel", @@ -48,57 +47,10 @@ rust_library( "noclippy", "norustfmt", ], - version = "1.0.109", + version = "2.0.0", deps = [ "@vendor__proc-macro2-1.0.52//:proc_macro2", "@vendor__quote-1.0.26//:quote", - "@vendor__syn-1.0.109//:build_script_build", "@vendor__unicode-ident-1.0.8//:unicode_ident", ], ) - -cargo_build_script( - name = "syn_build_script", - srcs = glob(["**/*.rs"]), - crate_features = [ - "clone-impls", - "default", - "derive", - "full", - "parsing", - "printing", - "proc-macro", - "quote", - ], - crate_name = "build_script_build", - crate_root = "build.rs", - data = glob( - include = ["**"], - exclude = [ - "**/* *", - "BUILD", - "BUILD.bazel", - "WORKSPACE", - "WORKSPACE.bazel", - ], - ), - edition = "2018", - rustc_flags = [ - "--cap-lints=allow", - ], - tags = [ - "cargo-bazel", - "crate-name=syn", - "manual", - "noclippy", - "norustfmt", - ], - version = "1.0.109", - visibility = ["//visibility:private"], -) - -alias( - name = "build_script_build", - actual = "syn_build_script", - tags = ["manual"], -) diff --git a/third-party/bazel/defs.bzl b/third-party/bazel/defs.bzl index 41de5067e..b295fd32b 100644 --- a/third-party/bazel/defs.bzl +++ b/third-party/bazel/defs.bzl @@ -298,7 +298,7 @@ _NORMAL_DEPENDENCIES = { "proc-macro2": "@vendor__proc-macro2-1.0.52//:proc_macro2", "quote": "@vendor__quote-1.0.26//:quote", "scratch": "@vendor__scratch-1.0.5//:scratch", - "syn": "@vendor__syn-1.0.109//:syn", + "syn": "@vendor__syn-2.0.0//:syn", }, }, } @@ -472,12 +472,12 @@ def crate_repositories(): maybe( http_archive, - name = "vendor__syn-1.0.109", - sha256 = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237", + name = "vendor__syn-2.0.0", + sha256 = "4cff13bb1732bccfe3b246f3fdb09edfd51c01d6f5299b7ccd9457c2e4e37774", type = "tar.gz", - urls = ["https://crates.io/api/v1/crates/syn/1.0.109/download"], - strip_prefix = "syn-1.0.109", - build_file = Label("@cxx.rs//third-party/bazel:BUILD.syn-1.0.109.bazel"), + urls = ["https://crates.io/api/v1/crates/syn/2.0.0/download"], + strip_prefix = "syn-2.0.0", + build_file = Label("@cxx.rs//third-party/bazel:BUILD.syn-2.0.0.bazel"), ) maybe(