@@ -43,7 +43,6 @@ const HOURS_PER_DAY: u64 = 24;
43
43
#[ unstable( feature = "duration_units" , issue = "120301" ) ]
44
44
const DAYS_PER_WEEK : u64 = 7 ;
45
45
46
-
47
46
#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
48
47
#[ repr( transparent) ]
49
48
#[ rustc_layout_scalar_valid_range_start( 0 ) ]
@@ -222,7 +221,7 @@ impl Duration {
222
221
#[ inline]
223
222
#[ must_use]
224
223
#[ 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 ) ]
226
225
#[ requires( nanos < NANOS_PER_SEC || secs. checked_add( ( nanos / NANOS_PER_SEC ) as u64 ) . is_some( ) ) ]
227
226
#[ ensures( |duration| duration. is_safe( ) ) ]
228
227
pub const fn new ( secs : u64 , nanos : u32 ) -> Duration {
@@ -277,7 +276,7 @@ impl Duration {
277
276
#[ must_use]
278
277
#[ inline]
279
278
#[ 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 ) ]
281
280
#[ ensures( |duration| duration. is_safe( ) ) ]
282
281
pub const fn from_millis ( millis : u64 ) -> Duration {
283
282
let secs = millis / MILLIS_PER_SEC ;
@@ -305,7 +304,7 @@ impl Duration {
305
304
#[ must_use]
306
305
#[ inline]
307
306
#[ 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 ) ]
309
308
#[ ensures( |duration| duration. is_safe( ) ) ]
310
309
pub const fn from_micros ( micros : u64 ) -> Duration {
311
310
let secs = micros / MICROS_PER_SEC ;
@@ -338,7 +337,7 @@ impl Duration {
338
337
#[ must_use]
339
338
#[ inline]
340
339
#[ 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 ) ]
342
341
#[ ensures( |duration| duration. is_safe( ) ) ]
343
342
pub const fn from_nanos ( nanos : u64 ) -> Duration {
344
343
const NANOS_PER_SEC : u64 = self :: NANOS_PER_SEC as u64 ;
@@ -558,6 +557,7 @@ impl Duration {
558
557
#[ rustc_const_stable( feature = "duration_consts" , since = "1.32.0" ) ]
559
558
#[ must_use]
560
559
#[ inline]
560
+ #[ requires( NANOS_PER_MICRO != 0 ) ]
561
561
#[ ensures( |ms| * ms == self . nanos. 0 / NANOS_PER_MICRO ) ]
562
562
pub const fn subsec_micros ( & self ) -> u32 {
563
563
self . nanos . 0 / NANOS_PER_MICRO
@@ -678,7 +678,7 @@ impl Duration {
678
678
without modifying the original"]
679
679
#[ inline]
680
680
#[ 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( ) ) ]
682
682
pub const fn checked_add ( self , rhs : Duration ) -> Option < Duration > {
683
683
if let Some ( mut secs) = self . secs . checked_add ( rhs. secs ) {
684
684
let mut nanos = self . nanos . 0 + rhs. nanos . 0 ;
@@ -850,7 +850,8 @@ impl Duration {
850
850
#[ must_use = "this returns the result of the operation, \
851
851
without modifying the original"]
852
852
#[ 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 ) ) ]
854
855
#[ rustc_const_stable( feature = "duration_consts_2" , since = "1.58.0" ) ]
855
856
pub const fn checked_div ( self , rhs : u32 ) -> Option < Duration > {
856
857
if rhs != 0 {
@@ -1754,14 +1755,6 @@ pub mod duration_verify {
1754
1755
}
1755
1756
}
1756
1757
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
-
1765
1758
#[ kani:: proof_for_contract( Duration :: new) ]
1766
1759
fn duration_new ( ) {
1767
1760
let secs = kani:: any :: < u64 > ( ) ;
0 commit comments