Skip to content

Commit 5bbf75d

Browse files
authored
Rollup merge of #77691 - exrook:rename-layouterr, r=KodrAus
Rename/Deprecate LayoutErr in favor of LayoutError Implements rust-lang/wg-allocators#73. This patch renames LayoutErr to LayoutError, and uses a type alias to support users using the old name. The new name will be instantly stable in release 1.49 (current nightly), the type alias will become deprecated in release 1.51 (so that when the current nightly is 1.51, 1.49 will be stable). This is the only error type in `std` that ends in `Err` rather than `Error`, if this PR lands all stdlib error types will end in `Error` 🥰
2 parents de0aa61 + 8ff0c14 commit 5bbf75d

File tree

5 files changed

+52
-33
lines changed

5 files changed

+52
-33
lines changed

library/alloc/src/collections/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub use linked_list::LinkedList;
4141
#[doc(no_inline)]
4242
pub use vec_deque::VecDeque;
4343

44-
use crate::alloc::{Layout, LayoutErr};
44+
use crate::alloc::{Layout, LayoutError};
4545
use core::fmt::Display;
4646

4747
/// The error type for `try_reserve` methods.
@@ -71,9 +71,9 @@ pub enum TryReserveError {
7171
}
7272

7373
#[unstable(feature = "try_reserve", reason = "new API", issue = "48043")]
74-
impl From<LayoutErr> for TryReserveError {
74+
impl From<LayoutError> for TryReserveError {
7575
#[inline]
76-
fn from(_: LayoutErr) -> Self {
76+
fn from(_: LayoutError) -> Self {
7777
TryReserveError::CapacityOverflow
7878
}
7979
}

library/alloc/src/raw_vec.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![unstable(feature = "raw_vec_internals", reason = "implementation detail", issue = "none")]
22
#![doc(hidden)]
33

4-
use core::alloc::LayoutErr;
4+
use core::alloc::LayoutError;
55
use core::cmp;
66
use core::intrinsics;
77
use core::mem::{self, ManuallyDrop, MaybeUninit};
@@ -472,7 +472,7 @@ impl<T, A: AllocRef> RawVec<T, A> {
472472
// significant, because the number of different `A` types seen in practice is
473473
// much smaller than the number of `T` types.)
474474
fn finish_grow<A>(
475-
new_layout: Result<Layout, LayoutErr>,
475+
new_layout: Result<Layout, LayoutError>,
476476
current_memory: Option<(NonNull<u8>, Layout)>,
477477
alloc: &mut A,
478478
) -> Result<NonNull<[u8]>, TryReserveError>

library/core/src/alloc/layout.rs

+33-25
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub struct Layout {
3939

4040
impl Layout {
4141
/// Constructs a `Layout` from a given `size` and `align`,
42-
/// or returns `LayoutErr` if any of the following conditions
42+
/// or returns `LayoutError` if any of the following conditions
4343
/// are not met:
4444
///
4545
/// * `align` must not be zero,
@@ -52,9 +52,9 @@ impl Layout {
5252
#[stable(feature = "alloc_layout", since = "1.28.0")]
5353
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
5454
#[inline]
55-
pub const fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutErr> {
55+
pub const fn from_size_align(size: usize, align: usize) -> Result<Self, LayoutError> {
5656
if !align.is_power_of_two() {
57-
return Err(LayoutErr { private: () });
57+
return Err(LayoutError { private: () });
5858
}
5959

6060
// (power-of-two implies align != 0.)
@@ -72,7 +72,7 @@ impl Layout {
7272
// Above implies that checking for summation overflow is both
7373
// necessary and sufficient.
7474
if size > usize::MAX - (align - 1) {
75-
return Err(LayoutErr { private: () });
75+
return Err(LayoutError { private: () });
7676
}
7777

7878
// SAFETY: the conditions for `from_size_align_unchecked` have been
@@ -200,7 +200,7 @@ impl Layout {
200200
/// `align` violates the conditions listed in [`Layout::from_size_align`].
201201
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
202202
#[inline]
203-
pub fn align_to(&self, align: usize) -> Result<Self, LayoutErr> {
203+
pub fn align_to(&self, align: usize) -> Result<Self, LayoutError> {
204204
Layout::from_size_align(self.size(), cmp::max(self.align(), align))
205205
}
206206

@@ -274,16 +274,16 @@ impl Layout {
274274
/// layout of the array and `offs` is the distance between the start
275275
/// of each element in the array.
276276
///
277-
/// On arithmetic overflow, returns `LayoutErr`.
277+
/// On arithmetic overflow, returns `LayoutError`.
278278
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
279279
#[inline]
280-
pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutErr> {
280+
pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutError> {
281281
// This cannot overflow. Quoting from the invariant of Layout:
282282
// > `size`, when rounded up to the nearest multiple of `align`,
283283
// > must not overflow (i.e., the rounded value must be less than
284284
// > `usize::MAX`)
285285
let padded_size = self.size() + self.padding_needed_for(self.align());
286-
let alloc_size = padded_size.checked_mul(n).ok_or(LayoutErr { private: () })?;
286+
let alloc_size = padded_size.checked_mul(n).ok_or(LayoutError { private: () })?;
287287

288288
// SAFETY: self.align is already known to be valid and alloc_size has been
289289
// padded already.
@@ -307,16 +307,16 @@ impl Layout {
307307
/// start of the `next` embedded within the concatenated record
308308
/// (assuming that the record itself starts at offset 0).
309309
///
310-
/// On arithmetic overflow, returns `LayoutErr`.
310+
/// On arithmetic overflow, returns `LayoutError`.
311311
///
312312
/// # Examples
313313
///
314314
/// To calculate the layout of a `#[repr(C)]` structure and the offsets of
315315
/// the fields from its fields' layouts:
316316
///
317317
/// ```rust
318-
/// # use std::alloc::{Layout, LayoutErr};
319-
/// pub fn repr_c(fields: &[Layout]) -> Result<(Layout, Vec<usize>), LayoutErr> {
318+
/// # use std::alloc::{Layout, LayoutError};
319+
/// pub fn repr_c(fields: &[Layout]) -> Result<(Layout, Vec<usize>), LayoutError> {
320320
/// let mut offsets = Vec::new();
321321
/// let mut layout = Layout::from_size_align(0, 1)?;
322322
/// for &field in fields {
@@ -337,12 +337,12 @@ impl Layout {
337337
/// ```
338338
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
339339
#[inline]
340-
pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutErr> {
340+
pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutError> {
341341
let new_align = cmp::max(self.align(), next.align());
342342
let pad = self.padding_needed_for(next.align());
343343

344-
let offset = self.size().checked_add(pad).ok_or(LayoutErr { private: () })?;
345-
let new_size = offset.checked_add(next.size()).ok_or(LayoutErr { private: () })?;
344+
let offset = self.size().checked_add(pad).ok_or(LayoutError { private: () })?;
345+
let new_size = offset.checked_add(next.size()).ok_or(LayoutError { private: () })?;
346346

347347
let layout = Layout::from_size_align(new_size, new_align)?;
348348
Ok((layout, offset))
@@ -359,11 +359,11 @@ impl Layout {
359359
/// guaranteed that all elements in the array will be properly
360360
/// aligned.
361361
///
362-
/// On arithmetic overflow, returns `LayoutErr`.
362+
/// On arithmetic overflow, returns `LayoutError`.
363363
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
364364
#[inline]
365-
pub fn repeat_packed(&self, n: usize) -> Result<Self, LayoutErr> {
366-
let size = self.size().checked_mul(n).ok_or(LayoutErr { private: () })?;
365+
pub fn repeat_packed(&self, n: usize) -> Result<Self, LayoutError> {
366+
let size = self.size().checked_mul(n).ok_or(LayoutError { private: () })?;
367367
Layout::from_size_align(size, self.align())
368368
}
369369

@@ -372,38 +372,46 @@ impl Layout {
372372
/// padding is inserted, the alignment of `next` is irrelevant,
373373
/// and is not incorporated *at all* into the resulting layout.
374374
///
375-
/// On arithmetic overflow, returns `LayoutErr`.
375+
/// On arithmetic overflow, returns `LayoutError`.
376376
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
377377
#[inline]
378-
pub fn extend_packed(&self, next: Self) -> Result<Self, LayoutErr> {
379-
let new_size = self.size().checked_add(next.size()).ok_or(LayoutErr { private: () })?;
378+
pub fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> {
379+
let new_size = self.size().checked_add(next.size()).ok_or(LayoutError { private: () })?;
380380
Layout::from_size_align(new_size, self.align())
381381
}
382382

383383
/// Creates a layout describing the record for a `[T; n]`.
384384
///
385-
/// On arithmetic overflow, returns `LayoutErr`.
385+
/// On arithmetic overflow, returns `LayoutError`.
386386
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
387387
#[inline]
388-
pub fn array<T>(n: usize) -> Result<Self, LayoutErr> {
388+
pub fn array<T>(n: usize) -> Result<Self, LayoutError> {
389389
let (layout, offset) = Layout::new::<T>().repeat(n)?;
390390
debug_assert_eq!(offset, mem::size_of::<T>());
391391
Ok(layout.pad_to_align())
392392
}
393393
}
394394

395+
#[stable(feature = "alloc_layout", since = "1.28.0")]
396+
#[rustc_deprecated(
397+
since = "1.51.0",
398+
reason = "Name does not follow std convention, use LayoutError",
399+
suggestion = "LayoutError"
400+
)]
401+
pub type LayoutErr = LayoutError;
402+
395403
/// The parameters given to `Layout::from_size_align`
396404
/// or some other `Layout` constructor
397405
/// do not satisfy its documented constraints.
398-
#[stable(feature = "alloc_layout", since = "1.28.0")]
406+
#[stable(feature = "alloc_layout_error", since = "1.49.0")]
399407
#[derive(Clone, PartialEq, Eq, Debug)]
400-
pub struct LayoutErr {
408+
pub struct LayoutError {
401409
private: (),
402410
}
403411

404412
// (we need this for downstream impl of trait Error)
405413
#[stable(feature = "alloc_layout", since = "1.28.0")]
406-
impl fmt::Display for LayoutErr {
414+
impl fmt::Display for LayoutError {
407415
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
408416
f.write_str("invalid parameters to Layout::from_size_align")
409417
}

library/core/src/alloc/mod.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@ mod layout;
88
#[stable(feature = "global_alloc", since = "1.28.0")]
99
pub use self::global::GlobalAlloc;
1010
#[stable(feature = "alloc_layout", since = "1.28.0")]
11-
pub use self::layout::{Layout, LayoutErr};
11+
pub use self::layout::Layout;
12+
#[stable(feature = "alloc_layout", since = "1.28.0")]
13+
#[rustc_deprecated(
14+
since = "1.51.0",
15+
reason = "Name does not follow std convention, use LayoutError",
16+
suggestion = "LayoutError"
17+
)]
18+
#[allow(deprecated, deprecated_in_future)]
19+
pub use self::layout::LayoutErr;
20+
21+
#[stable(feature = "alloc_layout_error", since = "1.49.0")]
22+
pub use self::layout::LayoutError;
1223

1324
use crate::fmt;
1425
use crate::ptr::{self, NonNull};

library/std/src/error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ mod tests;
1919
use core::array;
2020
use core::convert::Infallible;
2121

22-
use crate::alloc::{AllocError, LayoutErr};
22+
use crate::alloc::{AllocError, LayoutError};
2323
use crate::any::TypeId;
2424
use crate::backtrace::Backtrace;
2525
use crate::borrow::Cow;
@@ -390,7 +390,7 @@ impl Error for ! {}
390390
impl Error for AllocError {}
391391

392392
#[stable(feature = "alloc_layout", since = "1.28.0")]
393-
impl Error for LayoutErr {}
393+
impl Error for LayoutError {}
394394

395395
#[stable(feature = "rust1", since = "1.0.0")]
396396
impl Error for str::ParseBoolError {

0 commit comments

Comments
 (0)