Skip to content

Commit 9caf0b5

Browse files
Make {align,size}_of_val const
1 parent 8611e52 commit 9caf0b5

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

library/core/src/intrinsics.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,11 +1004,13 @@ extern "rust-intrinsic" {
10041004
///
10051005
/// The stabilized version of this intrinsic is
10061006
/// [`std::mem::size_of_val`](../../std/mem/fn.size_of_val.html).
1007+
#[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
10071008
pub fn size_of_val<T: ?Sized>(_: *const T) -> usize;
10081009
/// The required alignment of the referenced value.
10091010
///
10101011
/// The stabilized version of this intrinsic is
10111012
/// [`std::mem::align_of_val`](../../std/mem/fn.align_of_val.html).
1013+
#[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
10121014
pub fn min_align_of_val<T: ?Sized>(_: *const T) -> usize;
10131015

10141016
/// Gets a static string slice containing the name of a type.

library/core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
#![feature(const_result)]
8989
#![feature(const_slice_from_raw_parts)]
9090
#![feature(const_slice_ptr_len)]
91+
#![feature(const_size_of_val)]
92+
#![feature(const_align_of_val)]
9193
#![feature(const_type_name)]
9294
#![feature(const_likely)]
9395
#![feature(const_unreachable_unchecked)]

library/core/src/mem/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ pub const fn size_of<T>() -> usize {
332332
/// ```
333333
#[inline]
334334
#[stable(feature = "rust1", since = "1.0.0")]
335-
pub fn size_of_val<T: ?Sized>(val: &T) -> usize {
335+
#[rustc_const_unstable(feature = "const_size_of_val", issue = "46571")]
336+
pub const fn size_of_val<T: ?Sized>(val: &T) -> usize {
336337
intrinsics::size_of_val(val)
337338
}
338339

@@ -466,9 +467,10 @@ pub const fn align_of<T>() -> usize {
466467
/// ```
467468
#[inline]
468469
#[stable(feature = "rust1", since = "1.0.0")]
470+
#[rustc_const_unstable(feature = "const_align_of_val", issue = "46571")]
469471
#[allow(deprecated)]
470-
pub fn align_of_val<T: ?Sized>(val: &T) -> usize {
471-
min_align_of_val(val)
472+
pub const fn align_of_val<T: ?Sized>(val: &T) -> usize {
473+
intrinsics::min_align_of_val(val)
472474
}
473475

474476
/// Returns the [ABI]-required minimum alignment of the type of the value that `val` points to.

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,21 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
120120
self.write_scalar(location.ptr, dest)?;
121121
}
122122

123+
sym::min_align_of_val | sym::size_of_val => {
124+
let place = self.deref_operand(args[0])?;
125+
let (size, align) = self
126+
.size_and_align_of(place.meta, place.layout)?
127+
.ok_or_else(|| err_unsup_format!("`extern type` does not have known layout"))?;
128+
129+
let result = match intrinsic_name {
130+
sym::min_align_of_val => align.bytes(),
131+
sym::size_of_val => size.bytes(),
132+
_ => bug!(),
133+
};
134+
135+
self.write_scalar(Scalar::from_machine_usize(result, self), dest)?;
136+
}
137+
123138
sym::min_align_of
124139
| sym::pref_align_of
125140
| sym::needs_drop

0 commit comments

Comments
 (0)