From 1376c5a617be5c25655d0d7cb63e3beaa5a6e026 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:20:39 +0200 Subject: Merging upstream version 1.70.0+dfsg1. Signed-off-by: Daniel Baumann --- vendor/syn/src/lit.rs | 252 ++++++++++++++++++++++++++++---------------------- 1 file changed, 144 insertions(+), 108 deletions(-) (limited to 'vendor/syn/src/lit.rs') diff --git a/vendor/syn/src/lit.rs b/vendor/syn/src/lit.rs index 130b40ed1..662ef8b22 100644 --- a/vendor/syn/src/lit.rs +++ b/vendor/syn/src/lit.rs @@ -19,6 +19,7 @@ ast_enum_of_structs! { /// This type is a [syntax tree enum]. /// /// [syntax tree enum]: crate::Expr#syntax-tree-enums + #[non_exhaustive] pub enum Lit { /// A UTF-8 string literal: `"foo"`. Str(LitStr), @@ -142,8 +143,7 @@ impl LitStr { /// # Example /// /// ``` - /// use proc_macro2::Span; - /// use syn::{Attribute, Error, Ident, Lit, Meta, MetaNameValue, Path, Result}; + /// use syn::{Attribute, Error, Expr, Lit, Meta, Path, Result}; /// /// // Parses the path from an attribute that looks like: /// // @@ -151,19 +151,20 @@ impl LitStr { /// // /// // or returns `None` if the input is some other attribute. /// fn get_path(attr: &Attribute) -> Result> { - /// if !attr.path.is_ident("path") { + /// if !attr.path().is_ident("path") { /// return Ok(None); /// } /// - /// match attr.parse_meta()? { - /// Meta::NameValue(MetaNameValue { lit: Lit::Str(lit_str), .. }) => { - /// lit_str.parse().map(Some) - /// } - /// _ => { - /// let message = "expected #[path = \"...\"]"; - /// Err(Error::new_spanned(attr, message)) + /// if let Meta::NameValue(meta) = &attr.meta { + /// if let Expr::Lit(expr) = &meta.value { + /// if let Lit::Str(lit_str) = &expr.lit { + /// return lit_str.parse().map(Some); + /// } /// } /// } + /// + /// let message = "expected #[path = \"...\"]"; + /// Err(Error::new_spanned(attr, message)) /// } /// ``` #[cfg(feature = "parsing")] @@ -359,11 +360,7 @@ impl LitInt { None => panic!("Not an integer literal: `{}`", repr), }; - let mut token = match value::to_literal(repr, &digits, &suffix) { - Some(token) => token, - None => panic!("Unsupported integer literal: `{}`", repr), - }; - + let mut token: Literal = repr.parse().unwrap(); token.set_span(span); LitInt { repr: Box::new(LitIntRepr { @@ -457,11 +454,7 @@ impl LitFloat { None => panic!("Not a float literal: `{}`", repr), }; - let mut token = match value::to_literal(repr, &digits, &suffix) { - Some(token) => token, - None => panic!("Unsupported float literal: `{}`", repr), - }; - + let mut token: Literal = repr.parse().unwrap(); token.set_span(span); LitFloat { repr: Box::new(LitFloatRepr { @@ -557,70 +550,133 @@ mod debug_impls { #[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))] impl Debug for LitStr { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter - .debug_struct("LitStr") - .field("token", &format_args!("{}", self.repr.token)) - .finish() + impl LitStr { + pub(crate) fn debug( + &self, + formatter: &mut fmt::Formatter, + name: &str, + ) -> fmt::Result { + formatter + .debug_struct(name) + .field("token", &format_args!("{}", self.repr.token)) + .finish() + } + } + self.debug(formatter, "LitStr") } } #[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))] impl Debug for LitByteStr { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter - .debug_struct("LitByteStr") - .field("token", &format_args!("{}", self.repr.token)) - .finish() + impl LitByteStr { + pub(crate) fn debug( + &self, + formatter: &mut fmt::Formatter, + name: &str, + ) -> fmt::Result { + formatter + .debug_struct(name) + .field("token", &format_args!("{}", self.repr.token)) + .finish() + } + } + self.debug(formatter, "LitByteStr") } } #[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))] impl Debug for LitByte { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter - .debug_struct("LitByte") - .field("token", &format_args!("{}", self.repr.token)) - .finish() + impl LitByte { + pub(crate) fn debug( + &self, + formatter: &mut fmt::Formatter, + name: &str, + ) -> fmt::Result { + formatter + .debug_struct(name) + .field("token", &format_args!("{}", self.repr.token)) + .finish() + } + } + self.debug(formatter, "LitByte") } } #[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))] impl Debug for LitChar { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter - .debug_struct("LitChar") - .field("token", &format_args!("{}", self.repr.token)) - .finish() + impl LitChar { + pub(crate) fn debug( + &self, + formatter: &mut fmt::Formatter, + name: &str, + ) -> fmt::Result { + formatter + .debug_struct(name) + .field("token", &format_args!("{}", self.repr.token)) + .finish() + } + } + self.debug(formatter, "LitChar") } } #[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))] impl Debug for LitInt { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter - .debug_struct("LitInt") - .field("token", &format_args!("{}", self.repr.token)) - .finish() + impl LitInt { + pub(crate) fn debug( + &self, + formatter: &mut fmt::Formatter, + name: &str, + ) -> fmt::Result { + formatter + .debug_struct(name) + .field("token", &format_args!("{}", self.repr.token)) + .finish() + } + } + self.debug(formatter, "LitInt") } } #[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))] impl Debug for LitFloat { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter - .debug_struct("LitFloat") - .field("token", &format_args!("{}", self.repr.token)) - .finish() + impl LitFloat { + pub(crate) fn debug( + &self, + formatter: &mut fmt::Formatter, + name: &str, + ) -> fmt::Result { + formatter + .debug_struct(name) + .field("token", &format_args!("{}", self.repr.token)) + .finish() + } + } + self.debug(formatter, "LitFloat") } } #[cfg_attr(doc_cfg, doc(cfg(feature = "extra-traits")))] impl Debug for LitBool { fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter - .debug_struct("LitBool") - .field("value", &self.value) - .finish() + impl LitBool { + pub(crate) fn debug( + &self, + formatter: &mut fmt::Formatter, + name: &str, + ) -> fmt::Result { + formatter + .debug_struct(name) + .field("value", &self.value) + .finish() + } + } + self.debug(formatter, "LitBool") } } } @@ -735,7 +791,7 @@ pub fn Lit(marker: lookahead::TokenMarker) -> Lit { } #[cfg(feature = "parsing")] -pub mod parsing { +pub(crate) mod parsing { use super::*; use crate::buffer::Cursor; use crate::parse::{Parse, ParseStream, Result}; @@ -783,23 +839,22 @@ pub mod parsing { repr.insert(0, '-'); if let Some((digits, suffix)) = value::parse_lit_int(&repr) { - if let Some(mut token) = value::to_literal(&repr, &digits, &suffix) { - token.set_span(span); - return Some(( - Lit::Int(LitInt { - repr: Box::new(LitIntRepr { - token, - digits, - suffix, - }), + let mut token: Literal = repr.parse().unwrap(); + token.set_span(span); + return Some(( + Lit::Int(LitInt { + repr: Box::new(LitIntRepr { + token, + digits, + suffix, }), - rest, - )); - } + }), + rest, + )); } let (digits, suffix) = value::parse_lit_float(&repr)?; - let mut token = value::to_literal(&repr, &digits, &suffix)?; + let mut token: Literal = repr.parse().unwrap(); token.set_span(span); Some(( Lit::Float(LitFloat { @@ -959,6 +1014,7 @@ mod value { let repr = token.to_string(); match byte(&repr, 0) { + // "...", r"...", r#"..."# b'"' | b'r' => { let (_, suffix) = parse_lit_str(&repr); return Lit::Str(LitStr { @@ -966,12 +1022,14 @@ mod value { }); } b'b' => match byte(&repr, 1) { + // b"...", br"...", br#"...#" b'"' | b'r' => { let (_, suffix) = parse_lit_byte_str(&repr); return Lit::ByteStr(LitByteStr { repr: Box::new(LitRepr { token, suffix }), }); } + // b'...' b'\'' => { let (_, suffix) = parse_lit_byte(&repr); return Lit::Byte(LitByte { @@ -980,6 +1038,7 @@ mod value { } _ => {} }, + // '...' b'\'' => { let (_, suffix) = parse_lit_char(&repr); return Lit::Char(LitChar { @@ -987,6 +1046,7 @@ mod value { }); } b'0'..=b'9' | b'-' => { + // 0, 123, 0xFF, 0o77, 0b11 if let Some((digits, suffix)) = parse_lit_int(&repr) { return Lit::Int(LitInt { repr: Box::new(LitIntRepr { @@ -996,6 +1056,7 @@ mod value { }), }); } + // 1.0, 1e-1, 1e+1 if let Some((digits, suffix)) = parse_lit_float(&repr) { return Lit::Float(LitFloat { repr: Box::new(LitFloatRepr { @@ -1006,6 +1067,7 @@ mod value { }); } } + // true, false b't' | b'f' => { if repr == "true" || repr == "false" { return Lit::Bool(LitBool { @@ -1014,6 +1076,9 @@ mod value { }); } } + // c"...", cr"...", cr#"..."# + // TODO: add a Lit::CStr variant? + b'c' => return Lit::Verbatim(token), _ => {} } @@ -1061,7 +1126,7 @@ mod value { /// Get the byte at offset idx, or a default of `b'\0'` if we're looking /// past the end of the input buffer. - pub fn byte + ?Sized>(s: &S, idx: usize) -> u8 { + pub(crate) fn byte + ?Sized>(s: &S, idx: usize) -> u8 { let s = s.as_ref(); if idx < s.len() { s[idx] @@ -1075,7 +1140,7 @@ mod value { } // Returns (content, suffix). - pub fn parse_lit_str(s: &str) -> (Box, Box) { + pub(crate) fn parse_lit_str(s: &str) -> (Box, Box) { match byte(s, 0) { b'"' => parse_lit_str_cooked(s), b'r' => parse_lit_str_raw(s), @@ -1117,11 +1182,10 @@ mod value { b'\'' => '\'', b'"' => '"', b'\r' | b'\n' => loop { - let ch = next_chr(s); - if ch.is_whitespace() { - s = &s[ch.len_utf8()..]; - } else { - continue 'outer; + let b = byte(s, 0); + match b { + b' ' | b'\t' | b'\n' | b'\r' => s = &s[1..], + _ => continue 'outer, } }, b => panic!("unexpected byte {:?} after \\ character in byte literal", b), @@ -1167,7 +1231,7 @@ mod value { } // Returns (content, suffix). - pub fn parse_lit_byte_str(s: &str) -> (Vec, Box) { + pub(crate) fn parse_lit_byte_str(s: &str) -> (Vec, Box) { assert_eq!(byte(s, 0), b'b'); match byte(s, 1) { b'"' => parse_lit_byte_str_cooked(s), @@ -1244,7 +1308,7 @@ mod value { } // Returns (value, suffix). - pub fn parse_lit_byte(s: &str) -> (u8, Box) { + pub(crate) fn parse_lit_byte(s: &str) -> (u8, Box) { assert_eq!(byte(s, 0), b'b'); assert_eq!(byte(s, 1), b'\''); @@ -1283,7 +1347,7 @@ mod value { } // Returns (value, suffix). - pub fn parse_lit_char(mut s: &str) -> (char, Box) { + pub(crate) fn parse_lit_char(mut s: &str) -> (char, Box) { assert_eq!(byte(s, 0), b'\''); s = &s[1..]; @@ -1388,7 +1452,7 @@ mod value { } // Returns base 10 digits and suffix. - pub fn parse_lit_int(mut s: &str) -> Option<(Box, Box)> { + pub(crate) fn parse_lit_int(mut s: &str) -> Option<(Box, Box)> { let negative = byte(s, 0) == b'-'; if negative { s = &s[1..]; @@ -1412,6 +1476,7 @@ mod value { }; let mut value = BigInt::new(); + let mut has_digit = false; 'outer: loop { let b = byte(s, 0); let digit = match b { @@ -1455,11 +1520,16 @@ mod value { return None; } + has_digit = true; value *= base; value += digit; s = &s[1..]; } + if !has_digit { + return None; + } + let suffix = s; if suffix.is_empty() || crate::ident::xid_ok(suffix) { let mut repr = value.to_string(); @@ -1473,7 +1543,7 @@ mod value { } // Returns base 10 digits and suffix. - pub fn parse_lit_float(input: &str) -> Option<(Box, Box)> { + pub(crate) fn parse_lit_float(input: &str) -> Option<(Box, Box)> { // Rust's floating point literals are very similar to the ones parsed by // the standard library, except that rust's literals can contain // ignorable underscores. Let's remove those underscores. @@ -1563,38 +1633,4 @@ mod value { None } } - - #[allow(clippy::unnecessary_wraps)] - pub fn to_literal(repr: &str, digits: &str, suffix: &str) -> Option { - #[cfg(syn_no_negative_literal_parse)] - { - // Rustc older than https://github.com/rust-lang/rust/pull/87262. - if repr.starts_with('-') { - let f64_parse_finite = || digits.parse().ok().filter(|x: &f64| x.is_finite()); - let f32_parse_finite = || digits.parse().ok().filter(|x: &f32| x.is_finite()); - return if suffix == "f64" { - f64_parse_finite().map(Literal::f64_suffixed) - } else if suffix == "f32" { - f32_parse_finite().map(Literal::f32_suffixed) - } else if suffix == "i64" { - digits.parse().ok().map(Literal::i64_suffixed) - } else if suffix == "i32" { - digits.parse().ok().map(Literal::i32_suffixed) - } else if suffix == "i16" { - digits.parse().ok().map(Literal::i16_suffixed) - } else if suffix == "i8" { - digits.parse().ok().map(Literal::i8_suffixed) - } else if !suffix.is_empty() { - None - } else if digits.contains('.') { - f64_parse_finite().map(Literal::f64_unsuffixed) - } else { - digits.parse().ok().map(Literal::i64_unsuffixed) - }; - } - } - let _ = digits; - let _ = suffix; - Some(repr.parse::().unwrap()) - } } -- cgit v1.2.3