diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index a11f9e8c14579..c92ac7f1343d8 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -897,7 +897,7 @@ impl Rc { // reference (see #54908). let layout = Layout::new::>() .extend(value_layout).unwrap().0 - .pad_to_align().unwrap(); + .pad_to_align(); // Allocate for the layout. let mem = Global.alloc(layout) diff --git a/src/liballoc/sync.rs b/src/liballoc/sync.rs index 4b10f089c2950..edd32fe6d41fd 100644 --- a/src/liballoc/sync.rs +++ b/src/liballoc/sync.rs @@ -751,7 +751,7 @@ impl Arc { // reference (see #54908). let layout = Layout::new::>() .extend(value_layout).unwrap().0 - .pad_to_align().unwrap(); + .pad_to_align(); let mem = Global.alloc(layout) .unwrap_or_else(|_| handle_alloc_error(layout)); diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs index 1b06baeb711c2..4798769823f43 100644 --- a/src/libcore/alloc.rs +++ b/src/libcore/alloc.rs @@ -213,18 +213,19 @@ impl Layout { /// Creates a layout by rounding the size of this layout up to a multiple /// of the layout's alignment. /// - /// Returns `Err` if the padded size would overflow. - /// /// This is equivalent to adding the result of `padding_needed_for` /// to the layout's current size. #[unstable(feature = "alloc_layout_extra", issue = "55724")] #[inline] - pub fn pad_to_align(&self) -> Result { + pub fn pad_to_align(&self) -> Layout { let pad = self.padding_needed_for(self.align()); - let new_size = self.size().checked_add(pad) - .ok_or(LayoutErr { private: () })?; + // This cannot overflow. Quoting from the invariant of Layout: + // > `size`, when rounded up to the nearest multiple of `align`, + // > must not overflow (i.e., the rounded value must be less than + // > `usize::MAX`) + let new_size = self.size() + pad; - Layout::from_size_align(new_size, self.align()) + Layout::from_size_align(new_size, self.align()).unwrap() } /// Creates a layout describing the record for `n` instances of