diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:19:13 +0000 |
commit | 218caa410aa38c29984be31a5229b9fa717560ee (patch) | |
tree | c54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/ui/consts/const-size_of_val-align_of_val.rs | |
parent | Releasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff) | |
download | rustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip |
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/consts/const-size_of_val-align_of_val.rs')
-rw-r--r-- | tests/ui/consts/const-size_of_val-align_of_val.rs | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/ui/consts/const-size_of_val-align_of_val.rs b/tests/ui/consts/const-size_of_val-align_of_val.rs new file mode 100644 index 000000000..e8323e4ae --- /dev/null +++ b/tests/ui/consts/const-size_of_val-align_of_val.rs @@ -0,0 +1,55 @@ +// run-pass + +#![feature(const_size_of_val, const_align_of_val)] +#![feature(const_size_of_val_raw, const_align_of_val_raw, layout_for_ptr)] + +use std::{mem, ptr}; + +struct Foo(#[allow(unused_tuple_struct_fields)] 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()); + +const SIZE_OF_DANGLING: usize = unsafe { mem::size_of_val_raw(0x100 as *const i32) }; +const SIZE_OF_BIG: usize = + unsafe { mem::size_of_val_raw(ptr::slice_from_raw_parts(0 as *const u8, isize::MAX as usize)) }; +const ALIGN_OF_DANGLING: usize = unsafe { mem::align_of_val_raw(0x100 as *const i16) }; + +fn main() { + assert_eq!(SIZE_OF_FOO, mem::size_of::<Foo>()); + assert_eq!(SIZE_OF_BAR, mem::size_of::<Bar>()); + assert_eq!(SIZE_OF_UGH, mem::size_of::<Ugh>()); + + assert_eq!(ALIGN_OF_FOO, mem::align_of::<Foo>()); + assert_eq!(ALIGN_OF_BAR, mem::align_of::<Bar>()); + assert_eq!(ALIGN_OF_UGH, mem::align_of::<Ugh>()); + + assert_eq!(SIZE_OF_DANGLING, mem::size_of::<i32>()); + assert_eq!(SIZE_OF_BIG, isize::MAX as usize); + assert_eq!(ALIGN_OF_DANGLING, mem::align_of::<i16>()); + + assert_eq!(SIZE_OF_SLICE, "foobar".len()); +} |