Skip to content

Commit b71e64b

Browse files
authored
Merge pull request #46 from alexcrichton/float
Ensure that float literal is always formatted as floating point
2 parents 12f379e + 114990e commit b71e64b

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/stable.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,15 @@ impl Literal {
492492
Literal(s.to_string())
493493
}
494494

495-
pub fn float(s: f64) -> Literal {
496-
Literal(s.to_string())
495+
pub fn float(n: f64) -> Literal {
496+
if !n.is_finite() {
497+
panic!("Invalid float literal {}", n);
498+
}
499+
let mut s = n.to_string();
500+
if !s.contains('.') {
501+
s += ".0";
502+
}
503+
Literal(s)
497504
}
498505

499506
pub fn integer(s: i64) -> Literal {

tests/test.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ fn symbols() {
1919
fn literals() {
2020
assert_eq!(Literal::string("foo").to_string(), "\"foo\"");
2121
assert_eq!(Literal::string("\"").to_string(), "\"\\\"\"");
22+
assert_eq!(Literal::float(10.0).to_string(), "10.0");
2223
}
2324

2425
#[test]

0 commit comments

Comments
 (0)