Skip to content

Commit

Permalink
Add contract to of<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
tautschnig committed Aug 20, 2024
1 parent c08b98d commit fe4ba88
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions library/core/src/ptr/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ impl Alignment {
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
#[rustc_const_unstable(feature = "ptr_alignment_type", issue = "102070")]
#[inline]
#[requires(mem::align_of::<T>().is_power_of_two())]
#[ensures(|result| result.as_usize().is_power_of_two())]
pub const fn of<T>() -> Self {
// SAFETY: rustc ensures that type alignment is always a power of two.
unsafe { Alignment::new_unchecked(mem::align_of::<T>()) }
Expand Down Expand Up @@ -396,10 +398,9 @@ mod verify {
}

// pub const fn of<T>() -> Self
#[kani::proof]
#[kani::proof_for_contract(Alignment::of)]
pub fn check_of_i32() {
let alignment = Alignment::of::<i32>();
assert!(alignment.as_usize().is_power_of_two());
let _ = Alignment::of::<i32>();
}

// pub const fn new(align: usize) -> Option<Self>
Expand Down

0 comments on commit fe4ba88

Please sign in to comment.