Skip to content

Commit 4787e97

Browse files
committed
Auto merge of #66256 - CAD97:patch-2, r=RalfJung
Layout::pad_to_align is infallible As per [this comment](#55724 (comment)) (cc @glandium). > Per https://github.com/rust-lang/rust/blob/eb981a1/src/libcore/alloc.rs#L63-L65, `layout.size()` is always <= `usize::MAX - (layout.align() - 1)`. > > Which means: > > * The maximum value `layout.size()` can have is already aligned for `layout.align()` (`layout.align()` being a power of two, `usize::MAX - (layout.align() - 1)` is a multiple of `layout.align()`) > * Incidentally, any value smaller than that maximum value will align at most to that maximum value. > > IOW, `pad_to_align` can not return `Err(LayoutErr)`, except for the layout not respecting its invariants, but we shouldn't care about that. This PR makes `pad_to_align` return `Layout` directly, representing the fact that it cannot fail.
2 parents fdc0011 + d1e53da commit 4787e97

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

src/liballoc/rc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ impl<T: ?Sized> Rc<T> {
926926
// reference (see #54908).
927927
let layout = Layout::new::<RcBox<()>>()
928928
.extend(value_layout).unwrap().0
929-
.pad_to_align().unwrap();
929+
.pad_to_align();
930930

931931
// Allocate for the layout.
932932
let mem = Global.alloc(layout)

src/liballoc/sync.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ impl<T: ?Sized> Arc<T> {
780780
// reference (see #54908).
781781
let layout = Layout::new::<ArcInner<()>>()
782782
.extend(value_layout).unwrap().0
783-
.pad_to_align().unwrap();
783+
.pad_to_align();
784784

785785
let mem = Global.alloc(layout)
786786
.unwrap_or_else(|_| handle_alloc_error(layout));

src/libcore/alloc.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -213,18 +213,19 @@ impl Layout {
213213
/// Creates a layout by rounding the size of this layout up to a multiple
214214
/// of the layout's alignment.
215215
///
216-
/// Returns `Err` if the padded size would overflow.
217-
///
218216
/// This is equivalent to adding the result of `padding_needed_for`
219217
/// to the layout's current size.
220218
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
221219
#[inline]
222-
pub fn pad_to_align(&self) -> Result<Layout, LayoutErr> {
220+
pub fn pad_to_align(&self) -> Layout {
223221
let pad = self.padding_needed_for(self.align());
224-
let new_size = self.size().checked_add(pad)
225-
.ok_or(LayoutErr { private: () })?;
222+
// This cannot overflow. Quoting from the invariant of Layout:
223+
// > `size`, when rounded up to the nearest multiple of `align`,
224+
// > must not overflow (i.e., the rounded value must be less than
225+
// > `usize::MAX`)
226+
let new_size = self.size() + pad;
226227

227-
Layout::from_size_align(new_size, self.align())
228+
Layout::from_size_align(new_size, self.align()).unwrap()
228229
}
229230

230231
/// Creates a layout describing the record for `n` instances of

0 commit comments

Comments
 (0)