summaryrefslogtreecommitdiffstats
path: root/tests/ui/consts/const-size_of-align_of.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
commit218caa410aa38c29984be31a5229b9fa717560ee (patch)
treec54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/ui/consts/const-size_of-align_of.rs
parentReleasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-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-align_of.rs')
-rw-r--r--tests/ui/consts/const-size_of-align_of.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/ui/consts/const-size_of-align_of.rs b/tests/ui/consts/const-size_of-align_of.rs
new file mode 100644
index 000000000..0c63dc84a
--- /dev/null
+++ b/tests/ui/consts/const-size_of-align_of.rs
@@ -0,0 +1,51 @@
+// run-pass
+#![allow(dead_code)]
+
+use std::mem;
+
+// Get around the limitations of CTFE in today's Rust.
+const fn choice_u64(c: bool, a: u64, b: u64) -> u64 {
+ (-(c as i64) as u64) & a | (-(!c as i64) as u64) & b
+}
+
+const fn max_usize(a: usize, b: usize) -> usize {
+ choice_u64(a > b, a as u64, b as u64) as usize
+}
+
+const fn align_to(size: usize, align: usize) -> usize {
+ (size + (align - 1)) & !(align - 1)
+}
+
+const fn packed_union_size_of<A, B>() -> usize {
+ max_usize(mem::size_of::<A>(), mem::size_of::<B>())
+}
+
+const fn union_align_of<A, B>() -> usize {
+ max_usize(mem::align_of::<A>(), mem::align_of::<B>())
+}
+
+const fn union_size_of<A, B>() -> usize {
+ align_to(packed_union_size_of::<A, B>(), union_align_of::<A, B>())
+}
+
+macro_rules! fake_union {
+ ($name:ident { $a:ty, $b:ty }) => (
+ struct $name {
+ _align: ([$a; 0], [$b; 0]),
+ _bytes: [u8; union_size_of::<$a, $b>()]
+ }
+ )
+}
+
+// Check that we can (poorly) emulate unions by
+// calling size_of and align_of at compile-time.
+fake_union!(U { u16, [u8; 3] });
+
+fn test(u: U) {
+ assert_eq!(mem::size_of_val(&u._bytes), 4);
+}
+
+fn main() {
+ assert_eq!(mem::size_of::<U>(), 4);
+ assert_eq!(mem::align_of::<U>(), 2);
+}