Skip to content

Commit af9d2f6

Browse files
committed
Auto merge of rust-lang#74930 - ecstatic-morse:const-size-align-of-val, r=oli-obk
Make `mem::size_of_val` and `mem::align_of_val` unstably const Implements rust-lang#46571 but does not stabilize it. I wanted this while working on something today. The only reason not to immediately stabilize are concerns around [custom DSTs](rust-lang#46571 (comment)). That proposal has made zero progress in the last two years and const eval is rich enough to support pretty much any user-defined `len` function as long as nightly features are allowed (`raw_ptr_deref`). Currently, this raises a `const_err` lint when passed an `extern type`. r? @oli-obk cc @rust-lang/wg-const-eval
2 parents 5e184c9 + f9ebfe8 commit af9d2f6

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

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.

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)]

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.

0 commit comments

Comments
 (0)