Skip to content

Commit

Permalink
Merge pull request #1088 from dtolnay/literalparse
Browse files Browse the repository at this point in the history
Bypass negative literal workaround on 1.56+
  • Loading branch information
dtolnay committed Oct 26, 2021
2 parents 30984ec + 0ca31d1 commit f39a581
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -36,7 +36,7 @@ proc-macro = ["proc-macro2/proc-macro", "quote/proc-macro"]
test = ["syn-test-suite/all-features"]

[dependencies]
proc-macro2 = { version = "1.0.27", default-features = false }
proc-macro2 = { version = "1.0.32", default-features = false }
quote = { version = "1.0", optional = true, default-features = false }
unicode-xid = "0.2"

Expand Down
4 changes: 4 additions & 0 deletions build.rs
Expand Up @@ -19,6 +19,10 @@ fn main() {
println!("cargo:rustc-cfg=syn_no_const_vec_new");
}

if compiler.minor < 56 {
println!("cargo:rustc-cfg=syn_no_negative_literal_parse");
}

if !compiler.nightly {
println!("cargo:rustc-cfg=syn_disable_nightly_tests");
}
Expand Down
52 changes: 29 additions & 23 deletions src/lit.rs
Expand Up @@ -1539,31 +1539,37 @@ mod value {
}
}

#[allow(clippy::unnecessary_wraps)]
pub fn to_literal(repr: &str, digits: &str, suffix: &str) -> Option<Literal> {
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());
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)
#[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)
};
}
} else {
Some(repr.parse::<Literal>().unwrap())
}
let _ = digits;
let _ = suffix;
Some(repr.parse::<Literal>().unwrap())
}
}
6 changes: 0 additions & 6 deletions tests/test_lit.rs
Expand Up @@ -215,12 +215,6 @@ fn negative() {
assert_eq!("-1.5f64", LitFloat::new("-1.5f64", span).to_string());
}

#[test]
fn negative_overflow() {
assert!(syn::parse_str::<LitFloat>("-1.0e99f64").is_ok());
assert!(syn::parse_str::<LitFloat>("-1.0e999f64").is_err());
}

#[test]
fn suffix() {
fn get_suffix(token: &str) -> String {
Expand Down

0 comments on commit f39a581

Please sign in to comment.