From c9948f5c5f5cc49a74f04e33a1b0cf6ed6a22b93 Mon Sep 17 00:00:00 2001 From: beetrees Date: Fri, 14 Oct 2022 15:51:20 +0100 Subject: [PATCH] Fix `Duration::{try_,}from_secs_f{32,64}(-0.0)` --- library/core/src/time.rs | 2 +- library/core/tests/lib.rs | 1 + library/core/tests/time.rs | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/library/core/src/time.rs b/library/core/src/time.rs index 1d0c51c3c8368..7cbb477d7a346 100644 --- a/library/core/src/time.rs +++ b/library/core/src/time.rs @@ -1279,7 +1279,7 @@ macro_rules! try_from_secs { const MANT_MASK: $bits_ty = (1 << $mant_bits) - 1; const EXP_MASK: $bits_ty = (1 << $exp_bits) - 1; - if $secs.is_sign_negative() { + if $secs < 0.0 { return Err(FromFloatSecsError { kind: FromFloatSecsErrorKind::Negative }); } diff --git a/library/core/tests/lib.rs b/library/core/tests/lib.rs index ca0c7a54b3e24..525a495903d53 100644 --- a/library/core/tests/lib.rs +++ b/library/core/tests/lib.rs @@ -101,6 +101,7 @@ #![feature(provide_any)] #![feature(utf8_chunks)] #![feature(is_ascii_octdigit)] +#![feature(duration_checked_float)] #![deny(unsafe_op_in_unsafe_fn)] extern crate test; diff --git a/library/core/tests/time.rs b/library/core/tests/time.rs index 697bf33a8b0da..a05128de471ab 100644 --- a/library/core/tests/time.rs +++ b/library/core/tests/time.rs @@ -467,3 +467,11 @@ fn duration_const() { const SATURATING_MUL: Duration = MAX.saturating_mul(2); assert_eq!(SATURATING_MUL, MAX); } + +#[test] +fn from_neg_zero() { + assert_eq!(Duration::try_from_secs_f32(-0.0), Ok(Duration::ZERO)); + assert_eq!(Duration::try_from_secs_f64(-0.0), Ok(Duration::ZERO)); + assert_eq!(Duration::from_secs_f32(-0.0), Duration::ZERO); + assert_eq!(Duration::from_secs_f64(-0.0), Duration::ZERO); +}