Skip to content

Commit c271428

Browse files
committed
Stabilize alloc_layout_extra
1 parent ab67c37 commit c271428

File tree

14 files changed

+50
-49
lines changed

14 files changed

+50
-49
lines changed

compiler/rustc_codegen_cranelift/patches/0027-sysroot_tests-128bit-atomic-operations.patch

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ diff --git a/coretests/tests/lib.rs b/coretests/tests/lib.rs
1414
index 1e336bf..35e6f54 100644
1515
--- a/coretests/tests/lib.rs
1616
+++ b/coretests/tests/lib.rs
17-
@@ -2,5 +2,4 @@
17+
@@ -2,4 +2,3 @@
1818
// tidy-alphabetical-start
1919
-#![cfg_attr(target_has_atomic = "128", feature(integer_atomics))]
2020
#![cfg_attr(test, feature(cfg_select))]
21-
#![feature(alloc_layout_extra)]
2221
#![feature(array_ptr_get)]
2322
diff --git a/coretests/tests/atomic.rs b/coretests/tests/atomic.rs
2423
index b735957..ea728b6 100644

library/alloc/src/alloc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ impl Global {
184184
#[cfg_attr(miri, track_caller)] // even without panics, this helps for Miri backtraces
185185
fn alloc_impl(&self, layout: Layout, zeroed: bool) -> Result<NonNull<[u8]>, AllocError> {
186186
match layout.size() {
187-
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
187+
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling_ptr(), 0)),
188188
// SAFETY: `layout` is non-zero in size,
189189
size => unsafe {
190190
let raw_ptr = if zeroed { alloc_zeroed(layout) } else { alloc(layout) };
@@ -314,7 +314,7 @@ unsafe impl Allocator for Global {
314314
// SAFETY: conditions must be upheld by the caller
315315
0 => unsafe {
316316
self.deallocate(ptr, old_layout);
317-
Ok(NonNull::slice_from_raw_parts(new_layout.dangling(), 0))
317+
Ok(NonNull::slice_from_raw_parts(new_layout.dangling_ptr(), 0))
318318
},
319319

320320
// SAFETY: `new_size` is non-zero. Other conditions must be upheld by the caller

library/alloc/src/boxed/thin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl<H> WithHeader<H> {
245245
// Some paranoia checking, mostly so that the ThinBox tests are
246246
// more able to catch issues.
247247
debug_assert!(value_offset == 0 && T::IS_ZST && H::IS_ZST);
248-
layout.dangling()
248+
layout.dangling_ptr()
249249
} else {
250250
let ptr = alloc::alloc(layout);
251251
if ptr.is_null() {
@@ -282,7 +282,7 @@ impl<H> WithHeader<H> {
282282
// Some paranoia checking, mostly so that the ThinBox tests are
283283
// more able to catch issues.
284284
debug_assert!(value_offset == 0 && size_of::<T>() == 0 && size_of::<H>() == 0);
285-
layout.dangling()
285+
layout.dangling_ptr()
286286
} else {
287287
let ptr = alloc::alloc(layout);
288288
if ptr.is_null() {

library/alloc/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
// Library features:
8787
// tidy-alphabetical-start
8888
#![cfg_attr(not(no_global_oom_handling), feature(string_replace_in_place))]
89-
#![feature(alloc_layout_extra)]
9089
#![feature(allocator_api)]
9190
#![feature(array_into_iter_constructors)]
9291
#![feature(array_windows)]

library/alloc/src/rc.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ use core::panic::{RefUnwindSafe, UnwindSafe};
261261
#[cfg(not(no_global_oom_handling))]
262262
use core::pin::Pin;
263263
use core::pin::PinCoerceUnsized;
264-
use core::ptr::{self, NonNull, drop_in_place};
264+
use core::ptr::{self, Alignment, NonNull, drop_in_place};
265265
#[cfg(not(no_global_oom_handling))]
266266
use core::slice::from_raw_parts_mut;
267267
use core::{borrow, fmt, hint};
@@ -3761,11 +3761,11 @@ unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> usize {
37613761
// and extern types, the input safety requirement is currently enough to
37623762
// satisfy the requirements of align_of_val_raw; this is an implementation
37633763
// detail of the language that must not be relied upon outside of std.
3764-
unsafe { data_offset_align(align_of_val_raw(ptr)) }
3764+
unsafe { data_offset_align(Alignment::new_unchecked(align_of_val_raw(ptr))) }
37653765
}
37663766

37673767
#[inline]
3768-
fn data_offset_align(align: usize) -> usize {
3768+
fn data_offset_align(align: Alignment) -> usize {
37693769
let layout = Layout::new::<RcInner<()>>();
37703770
layout.size() + layout.padding_needed_for(align)
37713771
}
@@ -4379,7 +4379,7 @@ impl<T: ?Sized, A: Allocator> UniqueRcUninit<T, A> {
43794379

43804380
/// Returns the pointer to be written into to initialize the [`Rc`].
43814381
fn data_ptr(&mut self) -> *mut T {
4382-
let offset = data_offset_align(self.layout_for_value.align());
4382+
let offset = data_offset_align(self.layout_for_value.alignment());
43834383
unsafe { self.ptr.as_ptr().byte_add(offset) as *mut T }
43844384
}
43854385

library/alloc/src/sync.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use core::ops::{CoerceUnsized, Deref, DerefMut, DerefPure, DispatchFromDyn, Lega
2626
use core::ops::{Residual, Try};
2727
use core::panic::{RefUnwindSafe, UnwindSafe};
2828
use core::pin::{Pin, PinCoerceUnsized};
29-
use core::ptr::{self, NonNull};
29+
use core::ptr::{self, Alignment, NonNull};
3030
#[cfg(not(no_global_oom_handling))]
3131
use core::slice::from_raw_parts_mut;
3232
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release};
@@ -4119,11 +4119,11 @@ unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> usize {
41194119
// and extern types, the input safety requirement is currently enough to
41204120
// satisfy the requirements of align_of_val_raw; this is an implementation
41214121
// detail of the language that must not be relied upon outside of std.
4122-
unsafe { data_offset_align(align_of_val_raw(ptr)) }
4122+
unsafe { data_offset_align(Alignment::new_unchecked(align_of_val_raw(ptr))) }
41234123
}
41244124

41254125
#[inline]
4126-
fn data_offset_align(align: usize) -> usize {
4126+
fn data_offset_align(align: Alignment) -> usize {
41274127
let layout = Layout::new::<ArcInner<()>>();
41284128
layout.size() + layout.padding_needed_for(align)
41294129
}
@@ -4156,7 +4156,7 @@ impl<T: ?Sized, A: Allocator> UniqueArcUninit<T, A> {
41564156

41574157
/// Returns the pointer to be written into to initialize the [`Arc`].
41584158
fn data_ptr(&mut self) -> *mut T {
4159-
let offset = data_offset_align(self.layout_for_value.align());
4159+
let offset = data_offset_align(self.layout_for_value.alignment());
41604160
unsafe { self.ptr.as_ptr().byte_add(offset) as *mut T }
41614161
}
41624162

library/alloctests/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
//
1515
// Library features:
1616
// tidy-alphabetical-start
17-
#![feature(alloc_layout_extra)]
1817
#![feature(allocator_api)]
1918
#![feature(array_into_iter_constructors)]
2019
#![feature(assert_matches)]

library/alloctests/tests/boxed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub struct ConstAllocator;
104104
unsafe impl Allocator for ConstAllocator {
105105
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
106106
match layout.size() {
107-
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0)),
107+
0 => Ok(NonNull::slice_from_raw_parts(layout.dangling_ptr(), 0)),
108108
_ => unsafe {
109109
let ptr = core::intrinsics::const_allocate(layout.size(), layout.align());
110110
Ok(NonNull::new_unchecked(ptr as *mut [u8; 0] as *mut [u8]))

library/alloctests/tests/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#![feature(allocator_api)]
2-
#![feature(alloc_layout_extra)]
32
#![feature(iter_array_chunks)]
43
#![feature(assert_matches)]
54
#![feature(wtf8_internals)]

library/core/src/alloc/layout.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,11 @@ impl Layout {
230230
/// be that of a valid pointer, which means this must not be used
231231
/// as a "not yet initialized" sentinel value.
232232
/// Types that lazily allocate must track initialization by some other means.
233-
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
233+
#[stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")]
234+
#[rustc_const_stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")]
234235
#[must_use]
235236
#[inline]
236-
pub const fn dangling(&self) -> NonNull<u8> {
237+
pub const fn dangling_ptr(&self) -> NonNull<u8> {
237238
NonNull::without_provenance(self.align.as_nonzero())
238239
}
239240

@@ -263,29 +264,23 @@ impl Layout {
263264
}
264265

265266
/// Returns the amount of padding we must insert after `self`
266-
/// to ensure that the following address will satisfy `align`
267-
/// (measured in bytes).
267+
/// to ensure that the following address will satisfy `alignment`.
268268
///
269-
/// e.g., if `self.size()` is 9, then `self.padding_needed_for(4)`
269+
/// e.g., if `self.size()` is 9, then `self.padding_needed_for(alignment4)`
270+
/// (where `alignment4.as_usize() == 4`)
270271
/// returns 3, because that is the minimum number of bytes of
271272
/// padding required to get a 4-aligned address (assuming that the
272273
/// corresponding memory block starts at a 4-aligned address).
273274
///
274-
/// The return value of this function has no meaning if `align` is
275-
/// not a power-of-two.
276-
///
277-
/// Note that the utility of the returned value requires `align`
275+
/// Note that the utility of the returned value requires `alignment`
278276
/// to be less than or equal to the alignment of the starting
279277
/// address for the whole allocated block of memory. One way to
280-
/// satisfy this constraint is to ensure `align <= self.align()`.
281-
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
282-
#[must_use = "this returns the padding needed, \
283-
without modifying the `Layout`"]
278+
/// satisfy this constraint is to ensure `alignment.as_usize() <= self.align()`.
279+
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
280+
#[must_use = "this returns the padding needed, without modifying the `Layout`"]
284281
#[inline]
285-
pub const fn padding_needed_for(&self, align: usize) -> usize {
286-
// FIXME: Can we just change the type on this to `Alignment`?
287-
let Some(align) = Alignment::new(align) else { return usize::MAX };
288-
let len_rounded_up = self.size_rounded_up_to_custom_align(align);
282+
pub const fn padding_needed_for(&self, alignment: Alignment) -> usize {
283+
let len_rounded_up = self.size_rounded_up_to_custom_align(alignment);
289284
// SAFETY: Cannot overflow because the rounded-up value is never less
290285
unsafe { unchecked_sub(len_rounded_up, self.size) }
291286
}
@@ -348,14 +343,15 @@ impl Layout {
348343
/// layout of the array and `offs` is the distance between the start
349344
/// of each element in the array.
350345
///
346+
/// Does not include padding after the trailing element.
347+
///
351348
/// (That distance between elements is sometimes known as "stride".)
352349
///
353350
/// On arithmetic overflow, returns `LayoutError`.
354351
///
355352
/// # Examples
356353
///
357354
/// ```
358-
/// #![feature(alloc_layout_extra)]
359355
/// use std::alloc::Layout;
360356
///
361357
/// // All rust types have a size that's a multiple of their alignment.
@@ -366,17 +362,26 @@ impl Layout {
366362
/// // But you can manually make layouts which don't meet that rule.
367363
/// let padding_needed = Layout::from_size_align(6, 4).unwrap();
368364
/// let repeated = padding_needed.repeat(3).unwrap();
369-
/// assert_eq!(repeated, (Layout::from_size_align(24, 4).unwrap(), 8));
365+
/// assert_eq!(repeated, (Layout::from_size_align(22, 4).unwrap(), 8));
370366
/// ```
371-
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
367+
#[stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")]
368+
#[rustc_const_stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")]
372369
#[inline]
373370
pub const fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutError> {
371+
// FIXME(const-hack): the following could be way shorter with `?`
374372
let padded = self.pad_to_align();
375-
if let Ok(repeated) = padded.repeat_packed(n) {
376-
Ok((repeated, padded.size()))
373+
let Ok(result) = (if let Some(k) = n.checked_sub(1) {
374+
let Ok(repeated) = padded.repeat_packed(k) else {
375+
return Err(LayoutError);
376+
};
377+
repeated.extend_packed(*self)
377378
} else {
378-
Err(LayoutError)
379-
}
379+
debug_assert!(n == 0);
380+
self.repeat_packed(0)
381+
}) else {
382+
return Err(LayoutError);
383+
};
384+
Ok((result, padded.size()))
380385
}
381386

382387
/// Creates a layout describing the record for `self` followed by
@@ -456,7 +461,8 @@ impl Layout {
456461
/// aligned.
457462
///
458463
/// On arithmetic overflow, returns `LayoutError`.
459-
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
464+
#[stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")]
465+
#[rustc_const_stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")]
460466
#[inline]
461467
pub const fn repeat_packed(&self, n: usize) -> Result<Self, LayoutError> {
462468
if let Some(size) = self.size.checked_mul(n) {
@@ -473,7 +479,8 @@ impl Layout {
473479
/// and is not incorporated *at all* into the resulting layout.
474480
///
475481
/// On arithmetic overflow, returns `LayoutError`.
476-
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
482+
#[stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")]
483+
#[rustc_const_stable(feature = "alloc_layout_extra", since = "CURRENT_RUSTC_VERSION")]
477484
#[inline]
478485
pub const fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> {
479486
// SAFETY: each `size` is at most `isize::MAX == usize::MAX/2`, so the

0 commit comments

Comments
 (0)