Skip to content

Commit 88fd494

Browse files
Test {align,size}_of_val in a const context
1 parent 9caf0b5 commit 88fd494

3 files changed

+79
-0
lines changed
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+

Diff for: src/test/ui/consts/const-size_of_val-align_of_val.rs

+45
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)