diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:20:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:20:39 +0000 |
commit | 1376c5a617be5c25655d0d7cb63e3beaa5a6e026 (patch) | |
tree | 3bb8d61aee02bc7a15eab3f36e3b921afc2075d0 /vendor/syn/src/ty.rs | |
parent | Releasing progress-linux version 1.69.0+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.tar.xz rustc-1376c5a617be5c25655d0d7cb63e3beaa5a6e026.zip |
Merging upstream version 1.70.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/syn/src/ty.rs')
-rw-r--r-- | vendor/syn/src/ty.rs | 405 |
1 files changed, 142 insertions, 263 deletions
diff --git a/vendor/syn/src/ty.rs b/vendor/syn/src/ty.rs index 8c841e2f7..9b341dafa 100644 --- a/vendor/syn/src/ty.rs +++ b/vendor/syn/src/ty.rs @@ -5,16 +5,13 @@ use proc_macro2::TokenStream; ast_enum_of_structs! { /// The possible types that a Rust value could have. /// - /// *This type is available only if Syn is built with the `"derive"` or `"full"` - /// feature.* - /// /// # Syntax tree enum /// /// This type is a [syntax tree enum]. /// /// [syntax tree enum]: Expr#syntax-tree-enums #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] - #[cfg_attr(not(syn_no_non_exhaustive), non_exhaustive)] + #[non_exhaustive] pub enum Type { /// A fixed size array type: `[T; n]`. Array(TypeArray), @@ -64,8 +61,6 @@ ast_enum_of_structs! { /// Tokens in type position not interpreted by Syn. Verbatim(TokenStream), - // Not public API. - // // For testing exhaustiveness in downstream code, use the following idiom: // // match ty { @@ -82,17 +77,11 @@ ast_enum_of_structs! { // a variant. You will be notified by a test failure when a variant is // added, so that you can add code to handle it, but your library will // continue to compile and work for downstream users in the interim. - #[cfg(syn_no_non_exhaustive)] - #[doc(hidden)] - __NonExhaustive, } } ast_struct! { /// A fixed size array type: `[T; n]`. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypeArray { pub bracket_token: token::Bracket, @@ -104,9 +93,6 @@ ast_struct! { ast_struct! { /// A bare function type: `fn(usize) -> bool`. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypeBareFn { pub lifetimes: Option<BoundLifetimes>, @@ -115,16 +101,13 @@ ast_struct! { pub fn_token: Token![fn], pub paren_token: token::Paren, pub inputs: Punctuated<BareFnArg, Token![,]>, - pub variadic: Option<Variadic>, + pub variadic: Option<BareVariadic>, pub output: ReturnType, } } ast_struct! { /// A type contained within invisible delimiters. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypeGroup { pub group_token: token::Group, @@ -135,9 +118,6 @@ ast_struct! { ast_struct! { /// An `impl Bound1 + Bound2 + Bound3` type where `Bound` is a trait or /// a lifetime. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypeImplTrait { pub impl_token: Token![impl], @@ -147,9 +127,6 @@ ast_struct! { ast_struct! { /// Indication that a type should be inferred by the compiler: `_`. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypeInfer { pub underscore_token: Token![_], @@ -158,9 +135,6 @@ ast_struct! { ast_struct! { /// A macro in the type position. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypeMacro { pub mac: Macro, @@ -169,9 +143,6 @@ ast_struct! { ast_struct! { /// The never type: `!`. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypeNever { pub bang_token: Token![!], @@ -180,9 +151,6 @@ ast_struct! { ast_struct! { /// A parenthesized type equivalent to the inner type. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypeParen { pub paren_token: token::Paren, @@ -193,9 +161,6 @@ ast_struct! { ast_struct! { /// A path like `std::slice::Iter`, optionally qualified with a /// self-type as in `<Vec<T> as SomeTrait>::Associated`. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypePath { pub qself: Option<QSelf>, @@ -205,9 +170,6 @@ ast_struct! { ast_struct! { /// A raw pointer type: `*const T` or `*mut T`. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypePtr { pub star_token: Token![*], @@ -219,9 +181,6 @@ ast_struct! { ast_struct! { /// A reference type: `&'a T` or `&'a mut T`. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypeReference { pub and_token: Token![&], @@ -233,9 +192,6 @@ ast_struct! { ast_struct! { /// A dynamically sized slice type: `[T]`. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypeSlice { pub bracket_token: token::Bracket, @@ -246,9 +202,6 @@ ast_struct! { ast_struct! { /// A trait object type `dyn Bound1 + Bound2 + Bound3` where `Bound` is a /// trait or a lifetime. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypeTraitObject { pub dyn_token: Option<Token![dyn]>, @@ -258,9 +211,6 @@ ast_struct! { ast_struct! { /// A tuple type: `(A, B, C, String)`. - /// - /// *This type is available only if Syn is built with the `"derive"` or - /// `"full"` feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct TypeTuple { pub paren_token: token::Paren, @@ -270,9 +220,6 @@ ast_struct! { ast_struct! { /// The binary interface of a function: `extern "C"`. - /// - /// *This type is available only if Syn is built with the `"derive"` or `"full"` - /// feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct Abi { pub extern_token: Token![extern], @@ -282,9 +229,6 @@ ast_struct! { ast_struct! { /// An argument in a function type: the `usize` in `fn(usize) -> bool`. - /// - /// *This type is available only if Syn is built with the `"derive"` or `"full"` - /// feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub struct BareFnArg { pub attrs: Vec<Attribute>, @@ -294,32 +238,18 @@ ast_struct! { } ast_struct! { - /// The variadic argument of a foreign function. - /// - /// ```rust - /// # struct c_char; - /// # struct c_int; - /// # - /// extern "C" { - /// fn printf(format: *const c_char, ...) -> c_int; - /// // ^^^ - /// } - /// ``` - /// - /// *This type is available only if Syn is built with the `"derive"` or `"full"` - /// feature.* + /// The variadic argument of a function pointer like `fn(usize, ...)`. #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] - pub struct Variadic { + pub struct BareVariadic { pub attrs: Vec<Attribute>, + pub name: Option<(Ident, Token![:])>, pub dots: Token![...], + pub comma: Option<Token![,]>, } } ast_enum! { /// Return type of a function signature. - /// - /// *This type is available only if Syn is built with the `"derive"` or `"full"` - /// feature.* #[cfg_attr(doc_cfg, doc(cfg(any(feature = "full", feature = "derive"))))] pub enum ReturnType { /// Return type is not specified. @@ -332,12 +262,12 @@ ast_enum! { } #[cfg(feature = "parsing")] -pub mod parsing { +pub(crate) mod parsing { use super::*; use crate::ext::IdentExt; use crate::parse::{Parse, ParseStream, Result}; use crate::path; - use proc_macro2::{Punct, Spacing, Span, TokenTree}; + use proc_macro2::Span; #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for Type { @@ -392,7 +322,7 @@ pub mod parsing { { if let Type::Path(mut ty) = *group.elem { let arguments = &mut ty.path.segments.last_mut().unwrap().arguments; - if let PathArguments::None = arguments { + if arguments.is_none() { *arguments = PathArguments::AngleBracketed(input.parse()?); Path::parse_rest(input, &mut ty.path, false)?; return Ok(Type::Path(ty)); @@ -503,7 +433,8 @@ pub mod parsing { ..trait_bound }) } - other @ TypeParamBound::Lifetime(_) => other, + other @ (TypeParamBound::Lifetime(_) + | TypeParamBound::Verbatim(_)) => other, } } _ => break, @@ -530,13 +461,9 @@ pub mod parsing { || lookahead.peek(Token![unsafe]) || lookahead.peek(Token![extern]) { - let allow_mut_self = true; - if let Some(mut bare_fn) = parse_bare_fn(input, allow_mut_self)? { - bare_fn.lifetimes = lifetimes; - Ok(Type::BareFn(bare_fn)) - } else { - Ok(Type::Verbatim(verbatim::between(begin, input))) - } + let mut bare_fn: TypeBareFn = input.parse()?; + bare_fn.lifetimes = lifetimes; + Ok(Type::BareFn(bare_fn)) } else if lookahead.peek(Ident) || input.peek(Token![super]) || input.peek(Token![self]) @@ -545,49 +472,22 @@ pub mod parsing { || lookahead.peek(Token![::]) || lookahead.peek(Token![<]) { - let dyn_token: Option<Token![dyn]> = input.parse()?; - if let Some(dyn_token) = dyn_token { - let dyn_span = dyn_token.span; - let star_token: Option<Token![*]> = input.parse()?; - let bounds = TypeTraitObject::parse_bounds(dyn_span, input, allow_plus)?; - return Ok(if star_token.is_some() { - Type::Verbatim(verbatim::between(begin, input)) - } else { - Type::TraitObject(TypeTraitObject { - dyn_token: Some(dyn_token), - bounds, - }) - }); - } - let ty: TypePath = input.parse()?; if ty.qself.is_some() { return Ok(Type::Path(ty)); } - if input.peek(Token![!]) && !input.peek(Token![!=]) { - let mut contains_arguments = false; - for segment in &ty.path.segments { - match segment.arguments { - PathArguments::None => {} - PathArguments::AngleBracketed(_) | PathArguments::Parenthesized(_) => { - contains_arguments = true; - } - } - } - - if !contains_arguments { - let bang_token: Token![!] = input.parse()?; - let (delimiter, tokens) = mac::parse_delimiter(input)?; - return Ok(Type::Macro(TypeMacro { - mac: Macro { - path: ty.path, - bang_token, - delimiter, - tokens, - }, - })); - } + if input.peek(Token![!]) && !input.peek(Token![!=]) && ty.path.is_mod_style() { + let bang_token: Token![!] = input.parse()?; + let (delimiter, tokens) = mac::parse_delimiter(input)?; + return Ok(Type::Macro(TypeMacro { + mac: Macro { + path: ty.path, + bang_token, + delimiter, + tokens, + }, + })); } if lifetimes.is_some() || allow_plus && input.peek(Token![+]) { @@ -619,6 +519,19 @@ pub mod parsing { } Ok(Type::Path(ty)) + } else if lookahead.peek(Token![dyn]) { + let dyn_token: Token![dyn] = input.parse()?; + let dyn_span = dyn_token.span; + let star_token: Option<Token![*]> = input.parse()?; + let bounds = TypeTraitObject::parse_bounds(dyn_span, input, allow_plus)?; + return Ok(if star_token.is_some() { + Type::Verbatim(verbatim::between(begin, input)) + } else { + Type::TraitObject(TypeTraitObject { + dyn_token: Some(dyn_token), + bounds, + }) + }); } else if lookahead.peek(token::Bracket) { let content; let bracket_token = bracketed!(content in input); @@ -716,61 +629,47 @@ pub mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for TypeBareFn { fn parse(input: ParseStream) -> Result<Self> { - let allow_mut_self = false; - parse_bare_fn(input, allow_mut_self).map(Option::unwrap) - } - } - - fn parse_bare_fn(input: ParseStream, allow_mut_self: bool) -> Result<Option<TypeBareFn>> { - let args; - let mut variadic = None; - let mut has_mut_self = false; - - let bare_fn = TypeBareFn { - lifetimes: input.parse()?, - unsafety: input.parse()?, - abi: input.parse()?, - fn_token: input.parse()?, - paren_token: parenthesized!(args in input), - inputs: { - let mut inputs = Punctuated::new(); - - while !args.is_empty() { - let attrs = args.call(Attribute::parse_outer)?; - - if inputs.empty_or_trailing() && args.peek(Token![...]) { - variadic = Some(Variadic { - attrs, - dots: args.parse()?, - }); - break; - } + let args; + let mut variadic = None; + + Ok(TypeBareFn { + lifetimes: input.parse()?, + unsafety: input.parse()?, + abi: input.parse()?, + fn_token: input.parse()?, + paren_token: parenthesized!(args in input), + inputs: { + let mut inputs = Punctuated::new(); + + while !args.is_empty() { + let attrs = args.call(Attribute::parse_outer)?; + + if inputs.empty_or_trailing() + && (args.peek(Token![...]) + || args.peek(Ident) + && args.peek2(Token![:]) + && args.peek3(Token![...])) + { + variadic = Some(parse_bare_variadic(&args, attrs)?); + break; + } - if let Some(arg) = parse_bare_fn_arg(&args, allow_mut_self)? { + let allow_self = inputs.is_empty(); + let arg = parse_bare_fn_arg(&args, allow_self)?; inputs.push_value(BareFnArg { attrs, ..arg }); - } else { - has_mut_self = true; - } - if args.is_empty() { - break; - } + if args.is_empty() { + break; + } - let comma = args.parse()?; - if !has_mut_self { + let comma = args.parse()?; inputs.push_punct(comma); } - } - inputs - }, - variadic, - output: input.call(ReturnType::without_plus)?, - }; - - if has_mut_self { - Ok(None) - } else { - Ok(Some(bare_fn)) + inputs + }, + variadic, + output: input.call(ReturnType::without_plus)?, + }) } } @@ -838,29 +737,7 @@ pub mod parsing { impl Parse for TypePath { fn parse(input: ParseStream) -> Result<Self> { let expr_style = false; - let (qself, mut path) = path::parsing::qpath(input, expr_style)?; - - while path.segments.last().unwrap().arguments.is_empty() - && (input.peek(token::Paren) || input.peek(Token![::]) && input.peek3(token::Paren)) - { - input.parse::<Option<Token![::]>>()?; - let args: ParenthesizedGenericArguments = input.parse()?; - let allow_associated_type = cfg!(feature = "full") - && match &args.output { - ReturnType::Default => true, - ReturnType::Type(_, ty) => match **ty { - // TODO: probably some of the other kinds allow this too. - Type::Paren(_) => true, - _ => false, - }, - }; - let parenthesized = PathArguments::Parenthesized(args); - path.segments.last_mut().unwrap().arguments = parenthesized; - if allow_associated_type { - Path::parse_rest(input, &mut path, expr_style)?; - } - } - + let (qself, path) = path::parsing::qpath(input, expr_style)?; Ok(TypePath { qself, path }) } } @@ -928,7 +805,7 @@ pub mod parsing { let mut at_least_one_trait = false; for bound in &bounds { match bound { - TypeParamBound::Trait(_) => { + TypeParamBound::Trait(_) | TypeParamBound::Verbatim(_) => { at_least_one_trait = true; break; } @@ -968,7 +845,7 @@ pub mod parsing { let mut at_least_one_trait = false; for bound in &bounds { match bound { - TypeParamBound::Trait(_) => { + TypeParamBound::Trait(_) | TypeParamBound::Verbatim(_) => { at_least_one_trait = true; break; } @@ -1024,75 +901,72 @@ pub mod parsing { #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] impl Parse for BareFnArg { fn parse(input: ParseStream) -> Result<Self> { - let allow_mut_self = false; - parse_bare_fn_arg(input, allow_mut_self).map(Option::unwrap) + let allow_self = false; + parse_bare_fn_arg(input, allow_self) } } - fn parse_bare_fn_arg( - input: ParseStream, - mut allow_mut_self: bool, - ) -> Result<Option<BareFnArg>> { - let mut has_mut_self = false; - let arg = BareFnArg { - attrs: input.call(Attribute::parse_outer)?, - name: { - if (input.peek(Ident) || input.peek(Token![_]) || input.peek(Token![self])) - && input.peek2(Token![:]) - && !input.peek2(Token![::]) - { - let name = input.call(Ident::parse_any)?; - let colon: Token![:] = input.parse()?; - Some((name, colon)) - } else if allow_mut_self - && input.peek(Token![mut]) - && input.peek2(Token![self]) - && input.peek3(Token![:]) - && !input.peek3(Token![::]) - { - has_mut_self = true; - allow_mut_self = false; - input.parse::<Token![mut]>()?; - input.parse::<Token![self]>()?; - input.parse::<Token![:]>()?; - None - } else { - None - } - }, - ty: if !has_mut_self && input.peek(Token![...]) { - let dot3 = input.parse::<Token![...]>()?; - let args = vec![ - TokenTree::Punct(Punct::new('.', Spacing::Joint)), - TokenTree::Punct(Punct::new('.', Spacing::Joint)), - TokenTree::Punct(Punct::new('.', Spacing::Alone)), - ]; - 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; - input.parse::<Token![mut]>()?; - Type::Path(TypePath { - qself: None, - path: input.parse::<Token![self]>()?.into(), - }) - } else { - input.parse()? - }, - }; + fn parse_bare_fn_arg(input: ParseStream, allow_self: bool) -> Result<BareFnArg> { + let attrs = input.call(Attribute::parse_outer)?; + + let begin = input.fork(); + let has_mut_self = allow_self && input.peek(Token![mut]) && input.peek2(Token![self]); if has_mut_self { - Ok(None) - } else { - Ok(Some(arg)) + input.parse::<Token![mut]>()?; } + + let mut has_self = false; + let mut name = if (input.peek(Ident) || input.peek(Token![_]) || { + has_self = allow_self && input.peek(Token![self]); + has_self + }) && input.peek2(Token![:]) + && !input.peek2(Token![::]) + { + let name = input.call(Ident::parse_any)?; + let colon: Token![:] = input.parse()?; + Some((name, colon)) + } else { + has_self = false; + None + }; + + let ty = if allow_self && !has_self && input.peek(Token![mut]) && input.peek2(Token![self]) + { + input.parse::<Token![mut]>()?; + input.parse::<Token![self]>()?; + None + } else if has_mut_self && name.is_none() { + input.parse::<Token![self]>()?; + None + } else { + Some(input.parse()?) + }; + + let ty = match ty { + Some(ty) if !has_mut_self => ty, + _ => { + name = None; + Type::Verbatim(verbatim::between(begin, input)) + } + }; + + Ok(BareFnArg { attrs, name, ty }) + } + + fn parse_bare_variadic(input: ParseStream, attrs: Vec<Attribute>) -> Result<BareVariadic> { + Ok(BareVariadic { + attrs, + name: if input.peek(Ident) || input.peek(Token![_]) { + let name = input.call(Ident::parse_any)?; + let colon: Token![:] = input.parse()?; + Some((name, colon)) + } else { + None + }, + dots: input.parse()?, + comma: input.parse()?, + }) } #[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))] @@ -1287,10 +1161,15 @@ mod printing { } #[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))] - impl ToTokens for Variadic { + impl ToTokens for BareVariadic { fn to_tokens(&self, tokens: &mut TokenStream) { tokens.append_all(self.attrs.outer()); + if let Some((name, colon)) = &self.name { + name.to_tokens(tokens); + colon.to_tokens(tokens); + } self.dots.to_tokens(tokens); + self.comma.to_tokens(tokens); } } |