@@ -156,7 +156,6 @@ impl Layout {
156
156
#[ rustc_const_stable( feature = "alloc_layout_const_new" , since = "1.42.0" ) ]
157
157
#[ must_use]
158
158
#[ inline]
159
- #[ ensures( |result| result. align( ) . is_power_of_two( ) ) ]
160
159
pub const fn new < T > ( ) -> Self {
161
160
let ( size, align) = size_align :: < T > ( ) ;
162
161
// SAFETY: if the type is instantiated, rustc already ensures that its
@@ -247,7 +246,7 @@ impl Layout {
247
246
/// `align` violates the conditions listed in [`Layout::from_size_align`].
248
247
#[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
249
248
#[ inline]
250
- #[ ensures( |result| result. is_err( ) || result. unwrap( ) . is_aligned ( ) ) ]
249
+ #[ ensures( |result| result. is_err( ) || result. as_ref ( ) . unwrap( ) . align ( ) >= align ) ]
251
250
pub fn align_to ( & self , align : usize ) -> Result < Self , LayoutError > {
252
251
Layout :: from_size_align ( self . size ( ) , cmp:: max ( self . align ( ) , align) )
253
252
}
@@ -273,6 +272,7 @@ impl Layout {
273
272
#[ must_use = "this returns the padding needed, \
274
273
without modifying the `Layout`"]
275
274
#[ inline]
275
+ #[ ensures( |result| * result <= align) ]
276
276
pub const fn padding_needed_for ( & self , align : usize ) -> usize {
277
277
let len = self . size ( ) ;
278
278
@@ -309,6 +309,7 @@ impl Layout {
309
309
#[ must_use = "this returns a new `Layout`, \
310
310
without modifying the original"]
311
311
#[ inline]
312
+ #[ ensures( |result| result. size( ) % result. align( ) == 0 ) ]
312
313
pub const fn pad_to_align ( & self ) -> Layout {
313
314
let pad = self . padding_needed_for ( self . align ( ) ) ;
314
315
// This cannot overflow. Quoting from the invariant of Layout:
@@ -331,6 +332,7 @@ impl Layout {
331
332
/// On arithmetic overflow, returns `LayoutError`.
332
333
#[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
333
334
#[ inline]
335
+ #[ ensures( |result| result. is_err( ) || result. as_ref( ) . unwrap( ) . 1 % n == 0 ) ]
334
336
pub fn repeat ( & self , n : usize ) -> Result < ( Self , usize ) , LayoutError > {
335
337
// This cannot overflow. Quoting from the invariant of Layout:
336
338
// > `size`, when rounded up to the nearest multiple of `align`,
@@ -391,6 +393,7 @@ impl Layout {
391
393
/// ```
392
394
#[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
393
395
#[ inline]
396
+ #[ ensures( |result| result. is_err( ) || result. as_ref( ) . unwrap( ) . 1 <= result. as_ref( ) . unwrap( ) . 0 . size( ) ) ]
394
397
pub fn extend ( & self , next : Self ) -> Result < ( Self , usize ) , LayoutError > {
395
398
let new_align = cmp:: max ( self . align , next. align ) ;
396
399
let pad = self . padding_needed_for ( next. align ( ) ) ;
@@ -417,6 +420,7 @@ impl Layout {
417
420
/// On arithmetic overflow, returns `LayoutError`.
418
421
#[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
419
422
#[ inline]
423
+ #[ ensures( |result| result. is_err( ) || result. as_ref( ) . unwrap( ) . size( ) % n == 0 ) ]
420
424
pub fn repeat_packed ( & self , n : usize ) -> Result < Self , LayoutError > {
421
425
let size = self . size ( ) . checked_mul ( n) . ok_or ( LayoutError ) ?;
422
426
// The safe constructor is called here to enforce the isize size limit.
@@ -431,6 +435,7 @@ impl Layout {
431
435
/// On arithmetic overflow, returns `LayoutError`.
432
436
#[ unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
433
437
#[ inline]
438
+ #[ ensures( |result| result. is_err( ) || result. as_ref( ) . unwrap( ) . size( ) <= next. size( ) ) ]
434
439
pub fn extend_packed ( & self , next : Self ) -> Result < Self , LayoutError > {
435
440
let new_size = self . size ( ) . checked_add ( next. size ( ) ) . ok_or ( LayoutError ) ?;
436
441
// The safe constructor is called here to enforce the isize size limit.
@@ -563,23 +568,23 @@ mod verify {
563
568
}
564
569
565
570
// pub const fn new<T>() -> Self
566
- #[ kani:: proof_for_contract ( Layout :: new< i32 > ) ]
571
+ #[ kani:: proof ]
567
572
pub fn check_new_i32 ( ) {
568
573
let layout = Layout :: new :: < i32 > ( ) ;
569
574
assert_eq ! ( layout. size( ) , 4 ) ;
570
575
assert ! ( layout. align( ) . is_power_of_two( ) ) ;
571
576
}
572
577
573
578
// pub const fn for_value<T: ?Sized>(t: &T) -> Self
574
- #[ kani:: proof_for_contract( Layout :: for_value< i32 > ) ]
579
+ #[ kani:: proof_for_contract( Layout :: for_value) ]
575
580
pub fn check_for_value_i32 ( ) {
576
581
let array : [ i32 ; 2 ] = [ 1 , 2 ] ;
577
582
let layout = Layout :: for_value :: < [ i32 ] > ( & array[ 1 .. 1 ] ) ;
578
583
assert ! ( layout. align( ) . is_power_of_two( ) ) ;
579
584
}
580
585
581
586
// pub const unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self
582
- #[ kani:: proof_for_contract( Layout :: for_value_raw< i32 > ) ]
587
+ #[ kani:: proof_for_contract( Layout :: for_value_raw) ]
583
588
pub fn check_for_value_raw_i32 ( ) {
584
589
unsafe {
585
590
let layout = Layout :: for_value_raw :: < [ i32 ] > ( & [ ] as * const [ i32 ] ) ;
@@ -654,9 +659,9 @@ mod verify {
654
659
unsafe {
655
660
let layout = Layout :: from_size_align_unchecked ( s, a) ;
656
661
let n = kani:: any :: < usize > ( ) ;
657
- if let Ok ( ( layout2, padding ) ) = layout. repeat ( n) {
662
+ if let Ok ( ( layout2, padded_size ) ) = layout. repeat ( n) {
658
663
assert ! ( n == 0 || layout2. size( ) >= s) ;
659
- assert ! ( n == 0 || padding < a ) ;
664
+ assert_eq ! ( layout2 . size ( ) , n * padded_size ) ;
660
665
}
661
666
}
662
667
}
@@ -715,7 +720,7 @@ mod verify {
715
720
}
716
721
717
722
// pub const fn array<T>(n: usize) -> Result<Self, LayoutError>
718
- #[ kani:: proof_for_contract ( Layout :: array< i32 > ) ]
723
+ #[ kani:: proof ]
719
724
pub fn check_array_i32 ( ) {
720
725
let n = kani:: any :: < usize > ( ) ;
721
726
if let Ok ( layout) = Layout :: array :: < i32 > ( n) {
0 commit comments