]>
Commit | Line | Data |
---|---|---|
064997fb FG |
1 | //! The variants of an enum 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 { | |
f2b60f7d | 9 | use std::mem::{Assume, BikeshedIntrinsicFrom}; |
064997fb FG |
10 | |
11 | pub fn is_transmutable<Src, Dst, Context>() | |
12 | where | |
f2b60f7d FG |
13 | Dst: BikeshedIntrinsicFrom<Src, Context, { |
14 | Assume::ALIGNMENT | |
15 | .and(Assume::LIFETIMES) | |
16 | .and(Assume::SAFETY) | |
17 | .and(Assume::VALIDITY) | |
18 | }> | |
064997fb FG |
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, u8)] | |
32 | enum Lopsided { | |
33 | Smol(Zst), | |
34 | Lorg(V0), | |
35 | } | |
36 | ||
37 | #[repr(C)] struct Src(V0, Zst, V2); | |
38 | #[repr(C)] struct Dst(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 | } |