Skip to content

Commit 6ca499b

Browse files
committed
Only format Unexpected::Float with decimal point if it is finite
bef110b changed the display for unexpected floats to always append a ".0" if there was no decimal point found in the formatting of the float. However, this should only be relevant for finite (i.e., not NaN or inf) values. The change introduced a test failure in the ordered-float crate due to this: ---- impl_serde::test_fail_on_nan stdout ---- thread 'impl_serde::test_fail_on_nan' panicked at 'assertion failed: `(left == right)` left: `Error { msg: "invalid value: floating point `NaN.0`, expected float (but not NaN)" }`, right: `"invalid value: floating point `NaN`, expected float (but not NaN)"`', src/lib.rs:1554:9 stack backtrace: 0: rust_begin_unwind at /usr/src/rustc-1.70.0/library/std/src/panicking.rs:578:5 1: core::panicking::panic_fmt at /usr/src/rustc-1.70.0/library/core/src/panicking.rs:67:14 2: core::panicking::assert_failed_inner 3: core::panicking::assert_failed at /usr/src/rustc-1.70.0/library/core/src/panicking.rs:228:5 4: serde_test::assert::assert_de_tokens_error at /usr/share/cargo/registry/serde_test-1.0.171/src/assert.rs:228:19 5: ordered_float::impl_serde::test_fail_on_nan at ./src/lib.rs:1554:9 6: ordered_float::impl_serde::test_fail_on_nan::{{closure}} at ./src/lib.rs:1553:27 7: core::ops::function::FnOnce::call_once at /usr/src/rustc-1.70.0/library/core/src/ops/function.rs:250:5 8: core::ops::function::FnOnce::call_once at /usr/src/rustc-1.70.0/library/core/src/ops/function.rs:250:5 note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
1 parent 1477028 commit 6ca499b

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

serde/src/de/mod.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -2312,13 +2312,17 @@ impl Display for WithDecimalPoint {
23122312
}
23132313
}
23142314

2315-
let mut writer = LookForDecimalPoint {
2316-
formatter,
2317-
has_decimal_point: false,
2318-
};
2319-
tri!(write!(writer, "{}", self.0));
2320-
if !writer.has_decimal_point {
2321-
tri!(formatter.write_str(".0"));
2315+
if self.0.is_finite() {
2316+
let mut writer = LookForDecimalPoint {
2317+
formatter,
2318+
has_decimal_point: false,
2319+
};
2320+
tri!(write!(writer, "{}", self.0));
2321+
if !writer.has_decimal_point {
2322+
tri!(formatter.write_str(".0"));
2323+
}
2324+
} else {
2325+
tri!(write!(formatter, "{}", self.0));
23222326
}
23232327
Ok(())
23242328
}

test_suite/tests/test_de_error.rs

+8
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,14 @@ fn test_integer_from_float() {
14381438
);
14391439
}
14401440

1441+
#[test]
1442+
fn test_nan_no_decimal_point() {
1443+
assert_de_tokens_error::<isize>(
1444+
&[Token::F32(f32::NAN)],
1445+
"invalid type: floating point `NaN`, expected isize",
1446+
);
1447+
}
1448+
14411449
#[test]
14421450
fn test_unit_struct_from_seq() {
14431451
assert_de_tokens_error::<UnitStruct>(

0 commit comments

Comments
 (0)