Skip to content

Commit

Permalink
Append .0 to unsuffixed float if it would otherwise become int token
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 26, 2021
1 parent ffba430 commit c5025f0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
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");
}
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
4 changes: 4 additions & 0 deletions src/test/ui/proc-macro/auxiliary/api/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ 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");
}

fn test_parse_literal() {
Expand Down

0 comments on commit c5025f0

Please sign in to comment.