Skip to content

Commit 58f7f53

Browse files
committed
change comments to preconditions, add cfg(not(kani)) to checked_div
1 parent 8b7113d commit 58f7f53

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

library/core/src/time.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const HOURS_PER_DAY: u64 = 24;
4343
#[unstable(feature = "duration_units", issue = "120301")]
4444
const DAYS_PER_WEEK: u64 = 7;
4545

46-
4746
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
4847
#[repr(transparent)]
4948
#[rustc_layout_scalar_valid_range_start(0)]
@@ -222,7 +221,7 @@ impl Duration {
222221
#[inline]
223222
#[must_use]
224223
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
225-
// by definition of NANOS_PER_SEC, the checks for div by 0 cases are unreachable, but also unneeded
224+
#[requires(NANOS_PER_SEC != 0)]
226225
#[requires(nanos < NANOS_PER_SEC || secs.checked_add((nanos / NANOS_PER_SEC) as u64).is_some())]
227226
#[ensures(|duration| duration.is_safe())]
228227
pub const fn new(secs: u64, nanos: u32) -> Duration {
@@ -277,7 +276,7 @@ impl Duration {
277276
#[must_use]
278277
#[inline]
279278
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
280-
// by definition of MILLIS_PER_SEC, the checks for div by 0 cases are unreachable, but also unneeded
279+
#[requires(MILLIS_PER_SEC != 0)]
281280
#[ensures(|duration| duration.is_safe())]
282281
pub const fn from_millis(millis: u64) -> Duration {
283282
let secs = millis / MILLIS_PER_SEC;
@@ -305,7 +304,7 @@ impl Duration {
305304
#[must_use]
306305
#[inline]
307306
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
308-
// by definition of MILLIS_PER_SEC, the checks for div by 0 cases are unreachable, but also unneeded
307+
#[requires(MICROS_PER_SEC != 0)]
309308
#[ensures(|duration| duration.is_safe())]
310309
pub const fn from_micros(micros: u64) -> Duration {
311310
let secs = micros / MICROS_PER_SEC;
@@ -338,7 +337,7 @@ impl Duration {
338337
#[must_use]
339338
#[inline]
340339
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
341-
// by definition of MILLIS_PER_SEC, the checks for div by 0 cases are unreachable, but also unneeded
340+
#[requires(NANOS_PER_SEC != 0)]
342341
#[ensures(|duration| duration.is_safe())]
343342
pub const fn from_nanos(nanos: u64) -> Duration {
344343
const NANOS_PER_SEC: u64 = self::NANOS_PER_SEC as u64;
@@ -558,6 +557,7 @@ impl Duration {
558557
#[rustc_const_stable(feature = "duration_consts", since = "1.32.0")]
559558
#[must_use]
560559
#[inline]
560+
#[requires(NANOS_PER_MICRO != 0)]
561561
#[ensures(|ms| *ms == self.nanos.0 / NANOS_PER_MICRO)]
562562
pub const fn subsec_micros(&self) -> u32 {
563563
self.nanos.0 / NANOS_PER_MICRO
@@ -678,7 +678,7 @@ impl Duration {
678678
without modifying the original"]
679679
#[inline]
680680
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
681-
#[ensures(|duration| !duration.is_some() || duration.unwrap().is_safe())]
681+
#[ensures(|duration| duration.is_none() || duration.unwrap().is_safe())]
682682
pub const fn checked_add(self, rhs: Duration) -> Option<Duration> {
683683
if let Some(mut secs) = self.secs.checked_add(rhs.secs) {
684684
let mut nanos = self.nanos.0 + rhs.nanos.0;
@@ -850,7 +850,8 @@ impl Duration {
850850
#[must_use = "this returns the result of the operation, \
851851
without modifying the original"]
852852
#[inline]
853-
#[ensures(|duration| rhs == 0 || duration.unwrap().is_safe())]
853+
#[cfg_attr(not(kani), ensures(|duration| rhs == 0 || duration.unwrap().is_safe()))]
854+
#[cfg_attr(kani, ensures(|duration| false))]
854855
#[rustc_const_stable(feature = "duration_consts_2", since = "1.58.0")]
855856
pub const fn checked_div(self, rhs: u32) -> Option<Duration> {
856857
if rhs != 0 {
@@ -1754,14 +1755,6 @@ pub mod duration_verify {
17541755
}
17551756
}
17561757

1757-
#[kani::proof]
1758-
#[kani::should_panic]
1759-
fn duration_new_panic() {
1760-
let secs = kani::any::<u64>();
1761-
let nanos = kani::any::<u32>();
1762-
let _ = Duration::new(secs, nanos);
1763-
}
1764-
17651758
#[kani::proof_for_contract(Duration::new)]
17661759
fn duration_new() {
17671760
let secs = kani::any::<u64>();

0 commit comments

Comments
 (0)