Skip to content

Commit 4ca5ece

Browse files
authored
Rollup merge of rust-lang#102207 - CraftSpider:const-layout, r=scottmcm
Constify remaining `Layout` methods Makes the methods on `Layout` that aren't yet unstably const, under the same feature and issue, rust-lang#67521. Most of them required no changes, only non-trivial change is probably constifying `ValidAlignment` which may affect rust-lang#102072
2 parents becc24a + bbcdebd commit 4ca5ece

File tree

3 files changed

+46
-18
lines changed

3 files changed

+46
-18
lines changed

library/core/src/alloc/layout.rs

+25-11
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,10 @@ impl Layout {
157157
/// allocate backing structure for `T` (which could be a trait
158158
/// or other unsized type like a slice).
159159
#[stable(feature = "alloc_layout", since = "1.28.0")]
160+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
160161
#[must_use]
161162
#[inline]
162-
pub fn for_value<T: ?Sized>(t: &T) -> Self {
163+
pub const fn for_value<T: ?Sized>(t: &T) -> Self {
163164
let (size, align) = (mem::size_of_val(t), mem::align_of_val(t));
164165
// SAFETY: see rationale in `new` for why this is using the unsafe variant
165166
unsafe { Layout::from_size_align_unchecked(size, align) }
@@ -191,8 +192,9 @@ impl Layout {
191192
/// [trait object]: ../../book/ch17-02-trait-objects.html
192193
/// [extern type]: ../../unstable-book/language-features/extern-types.html
193194
#[unstable(feature = "layout_for_ptr", issue = "69835")]
195+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
194196
#[must_use]
195-
pub unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self {
197+
pub const unsafe fn for_value_raw<T: ?Sized>(t: *const T) -> Self {
196198
// SAFETY: we pass along the prerequisites of these functions to the caller
197199
let (size, align) = unsafe { (mem::size_of_val_raw(t), mem::align_of_val_raw(t)) };
198200
// SAFETY: see rationale in `new` for why this is using the unsafe variant
@@ -229,8 +231,9 @@ impl Layout {
229231
/// Returns an error if the combination of `self.size()` and the given
230232
/// `align` violates the conditions listed in [`Layout::from_size_align`].
231233
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
234+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
232235
#[inline]
233-
pub fn align_to(&self, align: usize) -> Result<Self, LayoutError> {
236+
pub const fn align_to(&self, align: usize) -> Result<Self, LayoutError> {
234237
Layout::from_size_align(self.size(), cmp::max(self.align(), align))
235238
}
236239

@@ -287,10 +290,11 @@ impl Layout {
287290
/// This is equivalent to adding the result of `padding_needed_for`
288291
/// to the layout's current size.
289292
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
293+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
290294
#[must_use = "this returns a new `Layout`, \
291295
without modifying the original"]
292296
#[inline]
293-
pub fn pad_to_align(&self) -> Layout {
297+
pub const fn pad_to_align(&self) -> Layout {
294298
let pad = self.padding_needed_for(self.align());
295299
// This cannot overflow. Quoting from the invariant of Layout:
296300
// > `size`, when rounded up to the nearest multiple of `align`,
@@ -311,8 +315,9 @@ impl Layout {
311315
///
312316
/// On arithmetic overflow, returns `LayoutError`.
313317
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
318+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
314319
#[inline]
315-
pub fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutError> {
320+
pub const fn repeat(&self, n: usize) -> Result<(Self, usize), LayoutError> {
316321
// This cannot overflow. Quoting from the invariant of Layout:
317322
// > `size`, when rounded up to the nearest multiple of `align`,
318323
// > must not overflow isize (i.e., the rounded value must be
@@ -321,7 +326,8 @@ impl Layout {
321326
let alloc_size = padded_size.checked_mul(n).ok_or(LayoutError)?;
322327

323328
// The safe constructor is called here to enforce the isize size limit.
324-
Layout::from_size_alignment(alloc_size, self.align).map(|layout| (layout, padded_size))
329+
let layout = Layout::from_size_alignment(alloc_size, self.align)?;
330+
Ok((layout, padded_size))
325331
}
326332

327333
/// Creates a layout describing the record for `self` followed by
@@ -370,8 +376,9 @@ impl Layout {
370376
/// # assert_eq!(repr_c(&[u64, u32, u16, u32]), Ok((s, vec![0, 8, 12, 16])));
371377
/// ```
372378
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
379+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
373380
#[inline]
374-
pub fn extend(&self, next: Self) -> Result<(Self, usize), LayoutError> {
381+
pub const fn extend(&self, next: Self) -> Result<(Self, usize), LayoutError> {
375382
let new_align = cmp::max(self.align, next.align);
376383
let pad = self.padding_needed_for(next.align());
377384

@@ -396,8 +403,9 @@ impl Layout {
396403
///
397404
/// On arithmetic overflow, returns `LayoutError`.
398405
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
406+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
399407
#[inline]
400-
pub fn repeat_packed(&self, n: usize) -> Result<Self, LayoutError> {
408+
pub const fn repeat_packed(&self, n: usize) -> Result<Self, LayoutError> {
401409
let size = self.size().checked_mul(n).ok_or(LayoutError)?;
402410
// The safe constructor is called here to enforce the isize size limit.
403411
Layout::from_size_alignment(size, self.align)
@@ -410,8 +418,9 @@ impl Layout {
410418
///
411419
/// On arithmetic overflow, returns `LayoutError`.
412420
#[unstable(feature = "alloc_layout_extra", issue = "55724")]
421+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
413422
#[inline]
414-
pub fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> {
423+
pub const fn extend_packed(&self, next: Self) -> Result<Self, LayoutError> {
415424
let new_size = self.size().checked_add(next.size()).ok_or(LayoutError)?;
416425
// The safe constructor is called here to enforce the isize size limit.
417426
Layout::from_size_alignment(new_size, self.align)
@@ -422,13 +431,18 @@ impl Layout {
422431
/// On arithmetic overflow or when the total size would exceed
423432
/// `isize::MAX`, returns `LayoutError`.
424433
#[stable(feature = "alloc_layout_manipulation", since = "1.44.0")]
434+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
425435
#[inline]
426-
pub fn array<T>(n: usize) -> Result<Self, LayoutError> {
436+
pub const fn array<T>(n: usize) -> Result<Self, LayoutError> {
427437
// Reduce the amount of code we need to monomorphize per `T`.
428438
return inner(mem::size_of::<T>(), Alignment::of::<T>(), n);
429439

430440
#[inline]
431-
fn inner(element_size: usize, align: Alignment, n: usize) -> Result<Layout, LayoutError> {
441+
const fn inner(
442+
element_size: usize,
443+
align: Alignment,
444+
n: usize,
445+
) -> Result<Layout, LayoutError> {
432446
// We need to check two things about the size:
433447
// - That the total size won't overflow a `usize`, and
434448
// - That the total size still fits in an `isize`.

library/core/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
// Library features:
9999
#![feature(const_align_offset)]
100100
#![feature(const_align_of_val)]
101+
#![feature(const_align_of_val_raw)]
102+
#![feature(const_alloc_layout)]
101103
#![feature(const_arguments_as_str)]
102104
#![feature(const_array_into_iter_constructors)]
103105
#![feature(const_bigint_helper_methods)]
@@ -138,6 +140,7 @@
138140
#![feature(const_ptr_write)]
139141
#![feature(const_raw_ptr_comparison)]
140142
#![feature(const_size_of_val)]
143+
#![feature(const_size_of_val_raw)]
141144
#![feature(const_slice_from_raw_parts_mut)]
142145
#![feature(const_slice_ptr_len)]
143146
#![feature(const_slice_split_at_mut)]
@@ -152,6 +155,7 @@
152155
#![feature(const_unsafecell_get_mut)]
153156
#![feature(const_waker)]
154157
#![feature(core_panic)]
158+
#![cfg_attr(not(bootstrap), feature(derive_const))]
155159
#![feature(duration_consts_float)]
156160
#![feature(maybe_uninit_uninit_array)]
157161
#![feature(ptr_alignment_type)]

library/core/src/ptr/alignment.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ use crate::{cmp, fmt, hash, mem, num};
99
/// Note that particularly large alignments, while representable in this type,
1010
/// are likely not to be supported by actual allocators and linkers.
1111
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
12-
#[derive(Copy, Clone, Eq, PartialEq)]
12+
#[derive(Copy, Clone, Eq)]
13+
#[cfg_attr(bootstrap, derive(PartialEq))]
14+
#[cfg_attr(not(bootstrap), derive_const(PartialEq))]
1315
#[repr(transparent)]
1416
pub struct Alignment(AlignmentEnum);
1517

@@ -167,16 +169,18 @@ impl From<Alignment> for usize {
167169
}
168170
}
169171

172+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
170173
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
171-
impl cmp::Ord for Alignment {
174+
impl const cmp::Ord for Alignment {
172175
#[inline]
173176
fn cmp(&self, other: &Self) -> cmp::Ordering {
174-
self.as_nonzero().cmp(&other.as_nonzero())
177+
self.as_nonzero().get().cmp(&other.as_nonzero().get())
175178
}
176179
}
177180

181+
#[rustc_const_unstable(feature = "const_alloc_layout", issue = "67521")]
178182
#[unstable(feature = "ptr_alignment_type", issue = "102070")]
179-
impl cmp::PartialOrd for Alignment {
183+
impl const cmp::PartialOrd for Alignment {
180184
#[inline]
181185
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
182186
Some(self.cmp(other))
@@ -198,7 +202,9 @@ type AlignmentEnum = AlignmentEnum32;
198202
#[cfg(target_pointer_width = "64")]
199203
type AlignmentEnum = AlignmentEnum64;
200204

201-
#[derive(Copy, Clone, Eq, PartialEq)]
205+
#[derive(Copy, Clone, Eq)]
206+
#[cfg_attr(bootstrap, derive(PartialEq))]
207+
#[cfg_attr(not(bootstrap), derive_const(PartialEq))]
202208
#[repr(u16)]
203209
enum AlignmentEnum16 {
204210
_Align1Shl0 = 1 << 0,
@@ -219,7 +225,9 @@ enum AlignmentEnum16 {
219225
_Align1Shl15 = 1 << 15,
220226
}
221227

222-
#[derive(Copy, Clone, Eq, PartialEq)]
228+
#[derive(Copy, Clone, Eq)]
229+
#[cfg_attr(bootstrap, derive(PartialEq))]
230+
#[cfg_attr(not(bootstrap), derive_const(PartialEq))]
223231
#[repr(u32)]
224232
enum AlignmentEnum32 {
225233
_Align1Shl0 = 1 << 0,
@@ -256,7 +264,9 @@ enum AlignmentEnum32 {
256264
_Align1Shl31 = 1 << 31,
257265
}
258266

259-
#[derive(Copy, Clone, Eq, PartialEq)]
267+
#[derive(Copy, Clone, Eq)]
268+
#[cfg_attr(bootstrap, derive(PartialEq))]
269+
#[cfg_attr(not(bootstrap), derive_const(PartialEq))]
260270
#[repr(u64)]
261271
enum AlignmentEnum64 {
262272
_Align1Shl0 = 1 << 0,

0 commit comments

Comments
 (0)