]> git.proxmox.com Git - rustc.git/blob - tests/ui/transmutability/enums/repr/should_require_well_defined_layout.rs
New upstream version 1.68.2+dfsg1
[rustc.git] / tests / ui / transmutability / enums / repr / should_require_well_defined_layout.rs
1 //! An enum must have a well-defined layout to participate in a transmutation.
2
3 #![crate_type = "lib"]
4 #![feature(repr128)]
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_reject_repr_rust() {
26 fn void() {
27 enum repr_rust {}
28 assert::is_maybe_transmutable::<repr_rust, ()>(); //~ ERROR cannot be safely transmuted
29 assert::is_maybe_transmutable::<u128, repr_rust>(); //~ ERROR cannot be safely transmuted
30 }
31
32 fn singleton() {
33 enum repr_rust { V }
34 assert::is_maybe_transmutable::<repr_rust, ()>(); //~ ERROR cannot be safely transmuted
35 assert::is_maybe_transmutable::<u128, repr_rust>(); //~ ERROR cannot be safely transmuted
36 }
37
38 fn duplex() {
39 enum repr_rust { A, B }
40 assert::is_maybe_transmutable::<repr_rust, ()>(); //~ ERROR cannot be safely transmuted
41 assert::is_maybe_transmutable::<u128, repr_rust>(); //~ ERROR cannot be safely transmuted
42 }
43 }
44
45 fn should_accept_primitive_reprs()
46 {
47 fn should_accept_repr_i8() {
48 #[repr(i8)] enum repr_i8 { V }
49 assert::is_maybe_transmutable::<repr_i8, ()>();
50 assert::is_maybe_transmutable::<i8, repr_i8>();
51 }
52
53 fn should_accept_repr_u8() {
54 #[repr(u8)] enum repr_u8 { V }
55 assert::is_maybe_transmutable::<repr_u8, ()>();
56 assert::is_maybe_transmutable::<u8, repr_u8>();
57 }
58
59 fn should_accept_repr_i16() {
60 #[repr(i16)] enum repr_i16 { V }
61 assert::is_maybe_transmutable::<repr_i16, ()>();
62 assert::is_maybe_transmutable::<i16, repr_i16>();
63 }
64
65 fn should_accept_repr_u16() {
66 #[repr(u16)] enum repr_u16 { V }
67 assert::is_maybe_transmutable::<repr_u16, ()>();
68 assert::is_maybe_transmutable::<u16, repr_u16>();
69 }
70
71 fn should_accept_repr_i32() {
72 #[repr(i32)] enum repr_i32 { V }
73 assert::is_maybe_transmutable::<repr_i32, ()>();
74 assert::is_maybe_transmutable::<i32, repr_i32>();
75 }
76
77 fn should_accept_repr_u32() {
78 #[repr(u32)] enum repr_u32 { V }
79 assert::is_maybe_transmutable::<repr_u32, ()>();
80 assert::is_maybe_transmutable::<u32, repr_u32>();
81 }
82
83 fn should_accept_repr_i64() {
84 #[repr(i64)] enum repr_i64 { V }
85 assert::is_maybe_transmutable::<repr_i64, ()>();
86 assert::is_maybe_transmutable::<i64, repr_i64>();
87 }
88
89 fn should_accept_repr_u64() {
90 #[repr(u64)] enum repr_u64 { V }
91 assert::is_maybe_transmutable::<repr_u64, ()>();
92 assert::is_maybe_transmutable::<u64, repr_u64>();
93 }
94
95 fn should_accept_repr_i128() {
96 #[repr(i128)] enum repr_i128 { V }
97 assert::is_maybe_transmutable::<repr_i128, ()>();
98 assert::is_maybe_transmutable::<i128, repr_i128>();
99 }
100
101 fn should_accept_repr_u128() {
102 #[repr(u128)] enum repr_u128 { V }
103 assert::is_maybe_transmutable::<repr_u128, ()>();
104 assert::is_maybe_transmutable::<u128, repr_u128>();
105 }
106
107 fn should_accept_repr_isize() {
108 #[repr(isize)] enum repr_isize { V }
109 assert::is_maybe_transmutable::<repr_isize, ()>();
110 assert::is_maybe_transmutable::<isize, repr_isize>();
111 }
112
113 fn should_accept_repr_usize() {
114 #[repr(usize)] enum repr_usize { V }
115 assert::is_maybe_transmutable::<repr_usize, ()>();
116 assert::is_maybe_transmutable::<usize, repr_usize>();
117 }
118 }
119
120 fn should_accept_repr_C() {
121 #[repr(C)] enum repr_c { V }
122 assert::is_maybe_transmutable::<repr_c, ()>();
123 assert::is_maybe_transmutable::<i128, repr_c>();
124 }