]> git.proxmox.com Git - rustc.git/blob - tests/ui/transmutability/unions/repr/should_handle_align.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / ui / transmutability / unions / repr / should_handle_align.rs
1 // check-pass
2 //! The presence of an `align(X)` annotation must be accounted for.
3
4 #![crate_type = "lib"]
5 #![feature(transmutability)]
6 #![allow(dead_code, incomplete_features, non_camel_case_types)]
7
8 mod assert {
9 use std::mem::{Assume, BikeshedIntrinsicFrom};
10 pub struct Context;
11
12 pub fn is_maybe_transmutable<Src, Dst>()
13 where
14 Dst: BikeshedIntrinsicFrom<Src, Context, {
15 Assume {
16 alignment: true,
17 lifetimes: true,
18 safety: true,
19 validity: true,
20 }
21 }>
22 {}
23 }
24
25 fn should_pad_explicitly_aligned_field() {
26 #[derive(Clone, Copy)] #[repr(u8)] enum V0u8 { V = 0 }
27 #[derive(Clone, Copy)] #[repr(u8)] enum V1u8 { V = 1 }
28
29 #[repr(C)]
30 pub union Uninit {
31 a: (),
32 b: V1u8,
33 }
34
35 #[repr(C, align(2))]
36 pub union align_2 {
37 a: V0u8,
38 }
39
40 #[repr(C)] struct ImplicitlyPadded(align_2, V0u8);
41 #[repr(C)] struct ExplicitlyPadded(V0u8, Uninit, V0u8);
42
43 // An implementation that (incorrectly) does not place a padding byte after
44 // `align_2` will, incorrectly, reject the following transmutations.
45 assert::is_maybe_transmutable::<ImplicitlyPadded, ExplicitlyPadded>();
46 assert::is_maybe_transmutable::<ExplicitlyPadded, ImplicitlyPadded>();
47 }