Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bypass negative literal workaround on 1.56+ #1088

Merged
merged 5 commits into from Oct 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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