Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let compiler assume, that Layout::align() is always a power of two #75281

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion library/core/src/alloc/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,15 @@ impl Layout {
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
#[inline]
pub const fn align(&self) -> usize {
self.align_.get()
let align = self.align_.get();
if !align.is_power_of_two() {
debug_assert!(false, "alignment is not a power of two");
// SAFETY: align is guaranteed to be a power of two
unsafe {
crate::hint::unreachable_unchecked();
}
}
align
}

/// Constructs a `Layout` suitable for holding a value of type `T`.
Expand Down