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

Append .0 to unsuffixed float if it would otherwise become int token #90297

Merged
merged 2 commits into from
Nov 6, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions library/proc_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,11 @@ impl Literal {
if !n.is_finite() {
panic!("Invalid float literal {}", n);
}
Literal(bridge::client::Literal::float(&n.to_string()))
let mut repr = n.to_string();
if !repr.contains('.') {
repr.push_str(".0");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From reading the standard library code it looks like to_string should never produce an exponential form, so this shouldn't result in something like 1e100.0.

The Debug impl for floats does exactly what we need (10.0) for small floats, but it does produce exponential forms for large floats.

Too bad fn float_to_decimal_common_shortest in library\core\src\fmt\float.rs is private, it can do exactly what we need here (although in theory it can be made public, but unstable and hidden, for library/proc_macro).

It's actually unfortunate that there's no public way to "print float as a float" in the standard library.
I wonder why the Display impl doesn't use the minimal precision 1 by default.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test involving 1e100 so that at least something breaks if to_string starts turning it into 1e100.0, though I don't expect that change to ever get made.

}
Literal(bridge::client::Literal::float(&repr))
}

/// Creates a new suffixed floating-point literal.
Expand Down Expand Up @@ -1115,7 +1119,11 @@ impl Literal {
if !n.is_finite() {
panic!("Invalid float literal {}", n);
}
Literal(bridge::client::Literal::float(&n.to_string()))
let mut repr = n.to_string();
if !repr.contains('.') {
repr.push_str(".0");
}
Literal(bridge::client::Literal::float(&repr))
}

/// Creates a new suffixed floating-point literal.
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/proc-macro/auxiliary/api/parse.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore-tidy-linelength

use proc_macro::Literal;

pub fn test() {
Expand All @@ -8,6 +10,14 @@ pub fn test() {
fn test_display_literal() {
assert_eq!(Literal::isize_unsuffixed(-10).to_string(), "-10");
assert_eq!(Literal::isize_suffixed(-10).to_string(), "-10isize");
assert_eq!(Literal::f32_unsuffixed(-10.0).to_string(), "-10.0");
assert_eq!(Literal::f32_suffixed(-10.0).to_string(), "-10f32");
assert_eq!(Literal::f64_unsuffixed(-10.0).to_string(), "-10.0");
assert_eq!(Literal::f64_suffixed(-10.0).to_string(), "-10f64");
assert_eq!(
Literal::f64_unsuffixed(1e100).to_string(),
"10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0",
);
}

fn test_parse_literal() {
Expand Down