Skip to content

Commit 2186722

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 1ce0cf0 + 88fd494 commit 2186722

7 files changed

+103
-3
lines changed

library/core/src/intrinsics.rs

+2
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

+2
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

+5-3
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

+15
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(extern_types)]
2+
#![feature(core_intrinsics)]
3+
#![feature(const_size_of_val, const_align_of_val)]
4+
5+
use std::intrinsics::{size_of_val, min_align_of_val};
6+
7+
extern {
8+
type Opaque;
9+
}
10+
11+
const _SIZE: usize = unsafe { size_of_val(&4 as *const i32 as *const Opaque) }; //~ ERROR
12+
const _ALIGN: usize = unsafe { min_align_of_val(&4 as *const i32 as *const Opaque) }; //~ ERROR
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: any use of this value will cause an error
2+
--> $DIR/const-size_of_val-align_of_val-extern-type.rs:11:31
3+
|
4+
LL | const _SIZE: usize = unsafe { size_of_val(&4 as *const i32 as *const Opaque) };
5+
| ------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---
6+
| |
7+
| `extern type` does not have known layout
8+
|
9+
= note: `#[deny(const_err)]` on by default
10+
11+
error: any use of this value will cause an error
12+
--> $DIR/const-size_of_val-align_of_val-extern-type.rs:12:32
13+
|
14+
LL | const _ALIGN: usize = unsafe { min_align_of_val(&4 as *const i32 as *const Opaque) };
15+
| -------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^---
16+
| |
17+
| `extern type` does not have known layout
18+
19+
error: aborting due to 2 previous errors
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// run-pass
2+
3+
#![feature(const_size_of_val, const_align_of_val)]
4+
5+
use std::mem;
6+
7+
struct Foo(u32);
8+
9+
#[derive(Clone, Copy)]
10+
struct Bar {
11+
_x: u8,
12+
_y: u16,
13+
_z: u8,
14+
}
15+
16+
union Ugh {
17+
_a: [u8; 3],
18+
_b: Bar,
19+
}
20+
21+
const FOO: Foo = Foo(4);
22+
const BAR: Bar = Bar { _x: 4, _y: 1, _z: 2 };
23+
const UGH: Ugh = Ugh { _a: [0; 3] };
24+
25+
const SIZE_OF_FOO: usize = mem::size_of_val(&FOO);
26+
const SIZE_OF_BAR: usize = mem::size_of_val(&BAR);
27+
const SIZE_OF_UGH: usize = mem::size_of_val(&UGH);
28+
29+
const ALIGN_OF_FOO: usize = mem::align_of_val(&FOO);
30+
const ALIGN_OF_BAR: usize = mem::align_of_val(&BAR);
31+
const ALIGN_OF_UGH: usize = mem::align_of_val(&UGH);
32+
33+
const SIZE_OF_SLICE: usize = mem::size_of_val("foobar".as_bytes());
34+
35+
fn main() {
36+
assert_eq!(SIZE_OF_FOO, mem::size_of::<Foo>());
37+
assert_eq!(SIZE_OF_BAR, mem::size_of::<Bar>());
38+
assert_eq!(SIZE_OF_UGH, mem::size_of::<Ugh>());
39+
40+
assert_eq!(ALIGN_OF_FOO, mem::align_of::<Foo>());
41+
assert_eq!(ALIGN_OF_BAR, mem::align_of::<Bar>());
42+
assert_eq!(ALIGN_OF_UGH, mem::align_of::<Ugh>());
43+
44+
assert_eq!(SIZE_OF_SLICE, "foobar".len());
45+
}

0 commit comments

Comments
 (0)