From 88fd4943a65fe7b18c0468255c54f9dee591dd1b Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Wed, 29 Jul 2020 14:58:06 -0700 Subject: [PATCH] Test `{align,size}_of_val` in a const context --- ...st-size_of_val-align_of_val-extern-type.rs | 14 ++++++ ...ize_of_val-align_of_val-extern-type.stderr | 20 +++++++++ .../consts/const-size_of_val-align_of_val.rs | 45 +++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 src/test/ui/consts/const-size_of_val-align_of_val-extern-type.rs create mode 100644 src/test/ui/consts/const-size_of_val-align_of_val-extern-type.stderr create mode 100644 src/test/ui/consts/const-size_of_val-align_of_val.rs diff --git a/src/test/ui/consts/const-size_of_val-align_of_val-extern-type.rs b/src/test/ui/consts/const-size_of_val-align_of_val-extern-type.rs new file mode 100644 index 0000000000000..96a8a8452edb0 --- /dev/null +++ b/src/test/ui/consts/const-size_of_val-align_of_val-extern-type.rs @@ -0,0 +1,14 @@ +#![feature(extern_types)] +#![feature(core_intrinsics)] +#![feature(const_size_of_val, const_align_of_val)] + +use std::intrinsics::{size_of_val, min_align_of_val}; + +extern { + type Opaque; +} + +const _SIZE: usize = unsafe { size_of_val(&4 as *const i32 as *const Opaque) }; //~ ERROR +const _ALIGN: usize = unsafe { min_align_of_val(&4 as *const i32 as *const Opaque) }; //~ ERROR + +fn main() {} diff --git a/src/test/ui/consts/const-size_of_val-align_of_val-extern-type.stderr b/src/test/ui/consts/const-size_of_val-align_of_val-extern-type.stderr new file mode 100644 index 0000000000000..d3f1b04d25154 --- /dev/null +++ b/src/test/ui/consts/const-size_of_val-align_of_val-extern-type.stderr @@ -0,0 +1,20 @@ +error: any use of this value will cause an error + --> $DIR/const-size_of_val-align_of_val-extern-type.rs:11:31 + | +LL | const _SIZE: usize = unsafe { size_of_val(&4 as *const i32 as *const Opaque) }; + | ------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--- + | | + | `extern type` does not have known layout + | + = note: `#[deny(const_err)]` on by default + +error: any use of this value will cause an error + --> $DIR/const-size_of_val-align_of_val-extern-type.rs:12:32 + | +LL | const _ALIGN: usize = unsafe { min_align_of_val(&4 as *const i32 as *const Opaque) }; + | -------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--- + | | + | `extern type` does not have known layout + +error: aborting due to 2 previous errors + diff --git a/src/test/ui/consts/const-size_of_val-align_of_val.rs b/src/test/ui/consts/const-size_of_val-align_of_val.rs new file mode 100644 index 0000000000000..e8e6f1d390099 --- /dev/null +++ b/src/test/ui/consts/const-size_of_val-align_of_val.rs @@ -0,0 +1,45 @@ +// run-pass + +#![feature(const_size_of_val, const_align_of_val)] + +use std::mem; + +struct Foo(u32); + +#[derive(Clone, Copy)] +struct Bar { + _x: u8, + _y: u16, + _z: u8, +} + +union Ugh { + _a: [u8; 3], + _b: Bar, +} + +const FOO: Foo = Foo(4); +const BAR: Bar = Bar { _x: 4, _y: 1, _z: 2 }; +const UGH: Ugh = Ugh { _a: [0; 3] }; + +const SIZE_OF_FOO: usize = mem::size_of_val(&FOO); +const SIZE_OF_BAR: usize = mem::size_of_val(&BAR); +const SIZE_OF_UGH: usize = mem::size_of_val(&UGH); + +const ALIGN_OF_FOO: usize = mem::align_of_val(&FOO); +const ALIGN_OF_BAR: usize = mem::align_of_val(&BAR); +const ALIGN_OF_UGH: usize = mem::align_of_val(&UGH); + +const SIZE_OF_SLICE: usize = mem::size_of_val("foobar".as_bytes()); + +fn main() { + assert_eq!(SIZE_OF_FOO, mem::size_of::()); + assert_eq!(SIZE_OF_BAR, mem::size_of::()); + assert_eq!(SIZE_OF_UGH, mem::size_of::()); + + assert_eq!(ALIGN_OF_FOO, mem::align_of::()); + assert_eq!(ALIGN_OF_BAR, mem::align_of::()); + assert_eq!(ALIGN_OF_UGH, mem::align_of::()); + + assert_eq!(SIZE_OF_SLICE, "foobar".len()); +}