@@ -166,7 +166,6 @@ impl Layout {
166
166
without modifying the layout"]
167
167
#[ inline]
168
168
#[ rustc_allow_const_fn_unstable( ptr_alignment_type) ]
169
- #[ ensures( |result| result. is_power_of_two( ) ) ]
170
169
pub const fn align ( & self ) -> usize {
171
170
self . align . as_usize ( )
172
171
}
@@ -176,7 +175,6 @@ impl Layout {
176
175
#[ rustc_const_stable( feature = "alloc_layout_const_new" , since = "1.42.0" ) ]
177
176
#[ must_use]
178
177
#[ inline]
179
- #[ requires( mem:: align_of:: <T >( ) . is_power_of_two( ) ) ]
180
178
#[ ensures( |result| result. size( ) == mem:: size_of:: <T >( ) ) ]
181
179
#[ ensures( |result| result. align( ) == mem:: align_of:: <T >( ) ) ]
182
180
pub const fn new < T > ( ) -> Self {
@@ -235,7 +233,8 @@ impl Layout {
235
233
// TODO: we should try to capture the above constraints on T in a `requires` clause, and the
236
234
// metadata helpers from https://github.com/model-checking/verify-rust-std/pull/37 may be able
237
235
// to accomplish this.
238
- #[ ensures( |result| result. align( ) . is_power_of_two( ) ) ]
236
+ #[ cfg_attr( not( kani) , ensures( |result| result. align( ) . is_power_of_two( ) ) ) ]
237
+ #[ cfg_attr( kani, ensures( |result| result. is_safe( ) ) ) ]
239
238
pub const unsafe fn for_value_raw < T : ?Sized > ( t : * const T ) -> Self {
240
239
// SAFETY: we pass along the prerequisites of these functions to the caller
241
240
let ( size, align) = unsafe { ( mem:: size_of_val_raw ( t) , mem:: align_of_val_raw ( t) ) } ;
@@ -581,6 +580,12 @@ mod verify {
581
580
}
582
581
}
583
582
583
+ impl kani:: Invariant for Layout {
584
+ fn is_safe ( & self ) -> bool {
585
+ self . align . as_usize ( ) . is_power_of_two ( )
586
+ }
587
+ }
588
+
584
589
// pub const fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutError>
585
590
#[ kani:: proof_for_contract( Layout :: from_size_align) ]
586
591
pub fn check_from_size_align ( ) {
@@ -612,10 +617,10 @@ mod verify {
612
617
}
613
618
614
619
// pub const fn align(&self) -> usize
615
- #[ kani:: proof_for_contract ( Layout :: align ) ]
620
+ #[ kani:: proof ]
616
621
pub fn check_align ( ) {
617
622
let layout = kani:: any :: < Layout > ( ) ;
618
- let _ = layout. align ( ) ;
623
+ assert ! ( layout. align( ) . is_power_of_two ( ) ) ;
619
624
}
620
625
621
626
// pub const fn new<T>() -> Self
0 commit comments