Skip to content

Commit 5d56e52

Browse files
authored
Rollup merge of #146473 - RalfJung:system-time-deconst, r=workingjubilee
Revert "Constify SystemTime methods" This reverts #144519. The const-hacks introduces bugs, and they make the code harder to maintain. Let's wait until we can constify these functions without changing their implementation. Fixes #146228. Closes #144517 (since the feature is gone). r? `@tgross35` Cc `@clarfonthey`
2 parents 04f17d8 + 2baa39e commit 5d56e52

File tree

12 files changed

+75
-183
lines changed

12 files changed

+75
-183
lines changed

library/std/src/lib.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@
284284
#![feature(core_float_math)]
285285
#![feature(decl_macro)]
286286
#![feature(deprecated_suggestion)]
287-
#![feature(derive_const)]
288287
#![feature(doc_cfg)]
289288
#![feature(doc_cfg_hide)]
290289
#![feature(doc_masked)]
@@ -332,11 +331,7 @@
332331
#![feature(cfg_select)]
333332
#![feature(char_internals)]
334333
#![feature(clone_to_uninit)]
335-
#![feature(const_cmp)]
336334
#![feature(const_convert)]
337-
#![feature(const_ops)]
338-
#![feature(const_option_ops)]
339-
#![feature(const_try)]
340335
#![feature(core_intrinsics)]
341336
#![feature(core_io_borrowed_buf)]
342337
#![feature(drop_guard)]

library/std/src/sys/pal/hermit/time.rs

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,8 @@ impl Timespec {
2525
Timespec { t: timespec { tv_sec, tv_nsec } }
2626
}
2727

28-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
29-
const fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
30-
// FIXME: const PartialOrd
31-
let mut cmp = self.t.tv_sec - other.t.tv_sec;
32-
if cmp == 0 {
33-
cmp = self.t.tv_nsec as i64 - other.t.tv_nsec as i64;
34-
}
35-
36-
if cmp >= 0 {
28+
fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
29+
if self >= other {
3730
Ok(if self.t.tv_nsec >= other.t.tv_nsec {
3831
Duration::new(
3932
(self.t.tv_sec - other.t.tv_sec) as u64,
@@ -53,22 +46,20 @@ impl Timespec {
5346
}
5447
}
5548

56-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
57-
const fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
49+
fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
5850
let mut secs = self.t.tv_sec.checked_add_unsigned(other.as_secs())?;
5951

6052
// Nano calculations can't overflow because nanos are <1B which fit
6153
// in a u32.
62-
let mut nsec = other.subsec_nanos() + self.t.tv_nsec as u32;
63-
if nsec >= NSEC_PER_SEC as u32 {
64-
nsec -= NSEC_PER_SEC as u32;
54+
let mut nsec = other.subsec_nanos() + u32::try_from(self.t.tv_nsec).unwrap();
55+
if nsec >= NSEC_PER_SEC.try_into().unwrap() {
56+
nsec -= u32::try_from(NSEC_PER_SEC).unwrap();
6557
secs = secs.checked_add(1)?;
6658
}
6759
Some(Timespec { t: timespec { tv_sec: secs, tv_nsec: nsec as _ } })
6860
}
6961

70-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
71-
const fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
62+
fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
7263
let mut secs = self.t.tv_sec.checked_sub_unsigned(other.as_secs())?;
7364

7465
// Similar to above, nanos can't overflow.
@@ -222,18 +213,15 @@ impl SystemTime {
222213
SystemTime(time)
223214
}
224215

225-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
226-
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
216+
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
227217
self.0.sub_timespec(&other.0)
228218
}
229219

230-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
231-
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
220+
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
232221
Some(SystemTime(self.0.checked_add_duration(other)?))
233222
}
234223

235-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
236-
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
224+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
237225
Some(SystemTime(self.0.checked_sub_duration(other)?))
238226
}
239227
}

library/std/src/sys/pal/sgx/time.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,15 @@ impl SystemTime {
3232
SystemTime(usercalls::insecure_time())
3333
}
3434

35-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
36-
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
37-
// FIXME: ok_or_else with const closures
38-
match self.0.checked_sub(other.0) {
39-
Some(duration) => Ok(duration),
40-
None => Err(other.0 - self.0),
41-
}
35+
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
36+
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
4237
}
4338

44-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
45-
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
39+
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
4640
Some(SystemTime(self.0.checked_add(*other)?))
4741
}
4842

49-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
50-
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
43+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
5144
Some(SystemTime(self.0.checked_sub(*other)?))
5245
}
5346
}

library/std/src/sys/pal/solid/time.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,19 @@ impl SystemTime {
3939
Self(t)
4040
}
4141

42-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
43-
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
42+
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
4443
if self.0 >= other.0 {
4544
Ok(Duration::from_secs((self.0 as u64).wrapping_sub(other.0 as u64)))
4645
} else {
4746
Err(Duration::from_secs((other.0 as u64).wrapping_sub(self.0 as u64)))
4847
}
4948
}
5049

51-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
52-
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
50+
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
5351
Some(SystemTime(self.0.checked_add_unsigned(other.as_secs())?))
5452
}
5553

56-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
57-
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
54+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
5855
Some(SystemTime(self.0.checked_sub_unsigned(other.as_secs())?))
5956
}
6057
}

library/std/src/sys/pal/uefi/time.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,19 @@ impl SystemTime {
8080
.unwrap_or_else(|| panic!("time not implemented on this platform"))
8181
}
8282

83-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
84-
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
85-
// FIXME: ok_or_else with const closures
86-
match self.0.checked_sub(other.0) {
87-
Some(duration) => Ok(duration),
88-
None => Err(other.0 - self.0),
89-
}
83+
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
84+
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
9085
}
9186

92-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
93-
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
94-
let temp = self.0.checked_add(*other)?;
87+
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
88+
let temp = Self(self.0.checked_add(*other)?);
9589

9690
// Check if can be represented in UEFI
97-
// FIXME: const PartialOrd
98-
let mut cmp = temp.as_secs() - MAX_UEFI_TIME.0.as_secs();
99-
if cmp == 0 {
100-
cmp = temp.subsec_nanos() as u64 - MAX_UEFI_TIME.0.subsec_nanos() as u64;
101-
}
102-
103-
if cmp <= 0 { Some(SystemTime(temp)) } else { None }
91+
if temp <= MAX_UEFI_TIME { Some(temp) } else { None }
10492
}
10593

106-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
107-
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
108-
Some(SystemTime(self.0.checked_sub(*other)?))
94+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
95+
self.0.checked_sub(*other).map(Self)
10996
}
11097
}
11198

library/std/src/sys/pal/unix/time.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,15 @@ impl SystemTime {
3838
SystemTime { t: Timespec::now(libc::CLOCK_REALTIME) }
3939
}
4040

41-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
42-
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
41+
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
4342
self.t.sub_timespec(&other.t)
4443
}
4544

46-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
47-
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
45+
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
4846
Some(SystemTime { t: self.t.checked_add_duration(other)? })
4947
}
5048

51-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
52-
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
49+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
5350
Some(SystemTime { t: self.t.checked_sub_duration(other)? })
5451
}
5552
}
@@ -136,15 +133,8 @@ impl Timespec {
136133
Timespec::new(t.tv_sec as i64, t.tv_nsec as i64).unwrap()
137134
}
138135

139-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
140-
pub const fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
141-
// FIXME: const PartialOrd
142-
let mut cmp = self.tv_sec - other.tv_sec;
143-
if cmp == 0 {
144-
cmp = self.tv_nsec.as_inner() as i64 - other.tv_nsec.as_inner() as i64;
145-
}
146-
147-
if cmp >= 0 {
136+
pub fn sub_timespec(&self, other: &Timespec) -> Result<Duration, Duration> {
137+
if self >= other {
148138
// NOTE(eddyb) two aspects of this `if`-`else` are required for LLVM
149139
// to optimize it into a branchless form (see also #75545):
150140
//
@@ -179,8 +169,7 @@ impl Timespec {
179169
}
180170
}
181171

182-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
183-
pub const fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
172+
pub fn checked_add_duration(&self, other: &Duration) -> Option<Timespec> {
184173
let mut secs = self.tv_sec.checked_add_unsigned(other.as_secs())?;
185174

186175
// Nano calculations can't overflow because nanos are <1B which fit
@@ -190,11 +179,10 @@ impl Timespec {
190179
nsec -= NSEC_PER_SEC as u32;
191180
secs = secs.checked_add(1)?;
192181
}
193-
Some(unsafe { Timespec::new_unchecked(secs, nsec as i64) })
182+
Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
194183
}
195184

196-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
197-
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
185+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<Timespec> {
198186
let mut secs = self.tv_sec.checked_sub_unsigned(other.as_secs())?;
199187

200188
// Similar to above, nanos can't overflow.
@@ -203,7 +191,7 @@ impl Timespec {
203191
nsec += NSEC_PER_SEC as i32;
204192
secs = secs.checked_sub(1)?;
205193
}
206-
Some(unsafe { Timespec::new_unchecked(secs, nsec as i64) })
194+
Some(unsafe { Timespec::new_unchecked(secs, nsec.into()) })
207195
}
208196

209197
#[allow(dead_code)]

library/std/src/sys/pal/unsupported/time.rs

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,15 @@ impl SystemTime {
3131
panic!("time not implemented on this platform")
3232
}
3333

34-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
35-
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
36-
// FIXME: ok_or_else with const closures
37-
match self.0.checked_sub(other.0) {
38-
Some(duration) => Ok(duration),
39-
None => Err(other.0 - self.0),
40-
}
34+
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
35+
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
4136
}
4237

43-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
44-
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
38+
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
4539
Some(SystemTime(self.0.checked_add(*other)?))
4640
}
4741

48-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
49-
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
42+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
5043
Some(SystemTime(self.0.checked_sub(*other)?))
5144
}
5245
}

library/std/src/sys/pal/wasip1/time.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,23 @@ impl SystemTime {
4343
SystemTime(current_time(wasi::CLOCKID_REALTIME))
4444
}
4545

46-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
47-
pub const fn from_wasi_timestamp(ts: wasi::Timestamp) -> SystemTime {
46+
pub fn from_wasi_timestamp(ts: wasi::Timestamp) -> SystemTime {
4847
SystemTime(Duration::from_nanos(ts))
4948
}
5049

51-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
52-
pub const fn to_wasi_timestamp(&self) -> Option<wasi::Timestamp> {
53-
// FIXME: const TryInto
54-
let ns = self.0.as_nanos();
55-
if ns <= u64::MAX as u128 { Some(ns as u64) } else { None }
50+
pub fn to_wasi_timestamp(&self) -> Option<wasi::Timestamp> {
51+
self.0.as_nanos().try_into().ok()
5652
}
5753

58-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
59-
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
60-
// FIXME: ok_or_else with const closures
61-
match self.0.checked_sub(other.0) {
62-
Some(duration) => Ok(duration),
63-
None => Err(other.0 - self.0),
64-
}
54+
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
55+
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
6556
}
6657

67-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
68-
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
58+
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
6959
Some(SystemTime(self.0.checked_add(*other)?))
7060
}
7161

72-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
73-
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
62+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
7463
Some(SystemTime(self.0.checked_sub(*other)?))
7564
}
7665
}

library/std/src/sys/pal/wasip2/time.rs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,23 @@ impl SystemTime {
3636
SystemTime(Duration::new(now.seconds, now.nanoseconds))
3737
}
3838

39-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
40-
pub const fn from_wasi_timestamp(ts: wasi::Timestamp) -> SystemTime {
39+
pub fn from_wasi_timestamp(ts: wasi::Timestamp) -> SystemTime {
4140
SystemTime(Duration::from_nanos(ts))
4241
}
4342

44-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
45-
pub const fn to_wasi_timestamp(&self) -> Option<wasi::Timestamp> {
46-
// FIXME: const TryInto
47-
let ns = self.0.as_nanos();
48-
if ns <= u64::MAX as u128 { Some(ns as u64) } else { None }
43+
pub fn to_wasi_timestamp(&self) -> Option<wasi::Timestamp> {
44+
self.0.as_nanos().try_into().ok()
4945
}
5046

51-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
52-
pub const fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
53-
// FIXME: ok_or_else with const closures
54-
match self.0.checked_sub(other.0) {
55-
Some(duration) => Ok(duration),
56-
None => Err(other.0 - self.0),
57-
}
47+
pub fn sub_time(&self, other: &SystemTime) -> Result<Duration, Duration> {
48+
self.0.checked_sub(other.0).ok_or_else(|| other.0 - self.0)
5849
}
5950

60-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
61-
pub const fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
51+
pub fn checked_add_duration(&self, other: &Duration) -> Option<SystemTime> {
6252
Some(SystemTime(self.0.checked_add(*other)?))
6353
}
6454

65-
#[rustc_const_unstable(feature = "const_system_time", issue = "144517")]
66-
pub const fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
55+
pub fn checked_sub_duration(&self, other: &Duration) -> Option<SystemTime> {
6756
Some(SystemTime(self.0.checked_sub(*other)?))
6857
}
6958
}

0 commit comments

Comments
 (0)