From 82cbd188b39afec817f26427be5ed481cab97af7 Mon Sep 17 00:00:00 2001 From: Tim Diekmann Date: Sat, 8 Aug 2020 08:28:51 +0200 Subject: [PATCH] Let compiler assume, that `Layout::align()` is always a power of two --- library/core/src/alloc/layout.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/library/core/src/alloc/layout.rs b/library/core/src/alloc/layout.rs index 7129f0f240291..9708bac0fe331 100644 --- a/library/core/src/alloc/layout.rs +++ b/library/core/src/alloc/layout.rs @@ -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`.