]> git.proxmox.com Git - rustc.git/blob - tests/ui/transmutability/unions/should_pad_variants.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / ui / transmutability / unions / should_pad_variants.rs
1 //! The variants of a union must be padded with uninit bytes such that they have
2 //! the same length (in bytes).
3
4 #![crate_type = "lib"]
5 #![feature(transmutability)]
6 #![allow(dead_code)]
7
8 mod assert {
9 use std::mem::{Assume, BikeshedIntrinsicFrom};
10
11 pub fn is_transmutable<Src, Dst, Context>()
12 where
13 Dst: BikeshedIntrinsicFrom<Src, Context, {
14 Assume::ALIGNMENT
15 .and(Assume::LIFETIMES)
16 .and(Assume::SAFETY)
17 .and(Assume::VALIDITY)
18 }>
19 {}
20 }
21
22 #[derive(Clone, Copy)]
23 #[repr(C)] struct Zst;
24
25 #[derive(Clone, Copy)]
26 #[repr(u8)] enum V0 { V = 0 }
27
28 #[derive(Clone, Copy)]
29 #[repr(u8)] enum V2 { V = 2 }
30
31 #[repr(C)]
32 union Lopsided {
33 smol: Zst,
34 lorg: V0,
35 }
36
37 #[repr(C)] struct Src(V0, Zst, V2);
38 #[repr(C)] struct Dst(V0, Lopsided, V2);
39
40 fn should_pad_variants() {
41 struct Context;
42 // If the implementation (incorrectly) fails to pad `Lopsided::smol` with
43 // an uninitialized byte, this transmutation might be (wrongly) accepted:
44 assert::is_transmutable::<Src, Dst, Context>(); //~ ERROR cannot be safely transmuted
45 }