Skip to content

Commit d5e2290

Browse files
committed
Auto merge of #30681 - Toby-S:master, r=bluss
Make `".".parse::<f32>()` and `".".parse::<f64>()` return Err This fixes #30344. This is a [breaking-change], which the libs team have classified as a bug fix.
2 parents 41611ba + 33f3c52 commit d5e2290

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/libcore/num/dec2flt/parse.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,28 @@ pub enum ParseResult<'a> {
5656
/// Check if the input string is a valid floating point number and if so, locate the integral
5757
/// part, the fractional part, and the exponent in it. Does not handle signs.
5858
pub fn parse_decimal(s: &str) -> ParseResult {
59+
if s.is_empty() {
60+
return Invalid;
61+
}
62+
5963
let s = s.as_bytes();
6064
let (integral, s) = eat_digits(s);
65+
6166
match s.first() {
62-
None => {
63-
if integral.is_empty() {
64-
return Invalid; // No digits at all
65-
}
66-
Valid(Decimal::new(integral, b"", 0))
67-
}
67+
None => Valid(Decimal::new(integral, b"", 0)),
6868
Some(&b'e') | Some(&b'E') => {
6969
if integral.is_empty() {
7070
return Invalid; // No digits before 'e'
7171
}
72+
7273
parse_exp(integral, b"", &s[1..])
7374
}
7475
Some(&b'.') => {
7576
let (fractional, s) = eat_digits(&s[1..]);
7677
if integral.is_empty() && fractional.is_empty() && s.is_empty() {
77-
// For historic reasons "." is a valid input.
78-
return Valid(Decimal::new(b"", b"", 0));
78+
return Invalid;
7979
}
80+
8081
match s.first() {
8182
None => Valid(Decimal::new(integral, fractional, 0)),
8283
Some(&b'e') | Some(&b'E') => parse_exp(integral, fractional, &s[1..]),

src/libcoretest/num/dec2flt/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ fn fast_path_correct() {
9898

9999
#[test]
100100
fn lonely_dot() {
101-
assert_eq!(".".parse(), Ok(0.0));
101+
assert!(".".parse::<f32>().is_err());
102+
assert!(".".parse::<f64>().is_err());
102103
}
103104

104105
#[test]

0 commit comments

Comments
 (0)