4
4
// collections, resulting in having to optimize down excess IR multiple times.
5
5
// Your performance intuition is useless. Run perf.
6
6
7
- use safety:: requires;
7
+ use safety:: { ensures , requires} ;
8
8
use crate :: cmp;
9
9
use crate :: error:: Error ;
10
10
use crate :: fmt;
@@ -71,6 +71,7 @@ impl Layout {
71
71
#[ rustc_const_stable( feature = "const_alloc_layout_size_align" , since = "1.50.0" ) ]
72
72
#[ inline]
73
73
#[ rustc_allow_const_fn_unstable( ptr_alignment_type) ]
74
+ #[ ensures( |result| result. is_err( ) || align. is_power_of_two( ) ) ]
74
75
pub const fn from_size_align ( size : usize , align : usize ) -> Result < Self , LayoutError > {
75
76
if !align. is_power_of_two ( ) {
76
77
return Err ( LayoutError ) ;
@@ -145,6 +146,7 @@ impl Layout {
145
146
without modifying the layout"]
146
147
#[ inline]
147
148
#[ rustc_allow_const_fn_unstable( ptr_alignment_type) ]
149
+ #[ ensures( |result| result. is_power_of_two( ) ) ]
148
150
pub const fn align ( & self ) -> usize {
149
151
self . align . as_usize ( )
150
152
}
@@ -154,6 +156,7 @@ impl Layout {
154
156
#[ rustc_const_stable( feature = "alloc_layout_const_new" , since = "1.42.0" ) ]
155
157
#[ must_use]
156
158
#[ inline]
159
+ #[ ensures( |result| result. align( ) . is_power_of_two( ) ) ]
157
160
pub const fn new < T > ( ) -> Self {
158
161
let ( size, align) = size_align :: < T > ( ) ;
159
162
// SAFETY: if the type is instantiated, rustc already ensures that its
@@ -169,6 +172,7 @@ impl Layout {
169
172
#[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
170
173
#[ must_use]
171
174
#[ inline]
175
+ #[ ensures( |result| result. align( ) . is_power_of_two( ) ) ]
172
176
pub const fn for_value < T : ?Sized > ( t : & T ) -> Self {
173
177
let ( size, align) = ( mem:: size_of_val ( t) , mem:: align_of_val ( t) ) ;
174
178
// SAFETY: see rationale in `new` for why this is using the unsafe variant
@@ -203,6 +207,7 @@ impl Layout {
203
207
#[ unstable( feature = "layout_for_ptr" , issue = "69835" ) ]
204
208
#[ rustc_const_unstable( feature = "const_alloc_layout" , issue = "67521" ) ]
205
209
#[ must_use]
210
+ #[ ensures( |result| result. align( ) . is_power_of_two( ) ) ]
206
211
pub const unsafe fn for_value_raw < T : ?Sized > ( t : * const T ) -> Self {
207
212
// SAFETY: we pass along the prerequisites of these functions to the caller
208
213
let ( size, align) = unsafe { ( mem:: size_of_val_raw ( t) , mem:: align_of_val_raw ( t) ) } ;
@@ -220,6 +225,7 @@ impl Layout {
220
225
#[ rustc_const_unstable( feature = "alloc_layout_extra" , issue = "55724" ) ]
221
226
#[ must_use]
222
227
#[ inline]
228
+ #[ ensures( |result| result. is_aligned( ) ) ]
223
229
pub const fn dangling ( & self ) -> NonNull < u8 > {
224
230
// SAFETY: align is guaranteed to be non-zero
225
231
unsafe { NonNull :: new_unchecked ( crate :: ptr:: without_provenance_mut :: < u8 > ( self . align ( ) ) ) }
@@ -241,6 +247,7 @@ impl Layout {
241
247
/// `align` violates the conditions listed in [`Layout::from_size_align`].
242
248
#[ stable( feature = "alloc_layout_manipulation" , since = "1.44.0" ) ]
243
249
#[ inline]
250
+ #[ ensures( |result| result. is_err( ) || result. unwrap( ) . is_aligned( ) ) ]
244
251
pub fn align_to ( & self , align : usize ) -> Result < Self , LayoutError > {
245
252
Layout :: from_size_align ( self . size ( ) , cmp:: max ( self . align ( ) , align) )
246
253
}
@@ -532,7 +539,7 @@ mod verify {
532
539
}
533
540
534
541
// pub const fn size(&self) -> usize
535
- #[ kani:: proof_for_contract ( Layout :: size ) ]
542
+ #[ kani:: proof ]
536
543
pub fn check_size ( ) {
537
544
let s = kani:: any :: < usize > ( ) ;
538
545
let a = kani:: any :: < usize > ( ) ;
0 commit comments