Debian Codegraph @

debian/rust-vendor/zerocopy-derive/tests/ui/union.rs

raw
1 // Copyright 2019 The Fuchsia Authors
2 //
3 // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4 // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6 // This file may not be copied, modified, or distributed except according to
7 // those terms.
8
9 #[macro_use]
10 extern crate zerocopy_renamed;
11
12 #[path = "../include.rs"]
13 mod util;
14
15 use std::mem::ManuallyDrop;
16
17 use self::util::util::AU16;
18
19 fn main() {}
20
21 //
22 // Immutable errors
23 //
24
25 #[derive(Immutable)]
26 //~[msrv, stable, nightly]^ ERROR: the trait bound `UnsafeCell<()>: zerocopy_renamed::Immutable` is not satisfied
27 #[zerocopy(crate = "zerocopy_renamed")]
28 union Immutable1 {
29 a: ManuallyDrop<core::cell::UnsafeCell<()>>,
30 }
31
32 //
33 // IntoBytes errors
34 //
35
36 #[derive(IntoBytes)]
37 //~[msrv, stable, nightly]^ ERROR: unsupported on types with type parameters
38 #[zerocopy(crate = "zerocopy_renamed")]
39 #[repr(C)]
40 union IntoBytes1<T> {
41 foo: ManuallyDrop<T>,
42 }
43
44 #[derive(IntoBytes)]
45 //~[msrv]^ ERROR: the trait bound `(): PaddingFree<IntoBytes2, 1_usize>` is not satisfied
46 //~[stable, nightly]^^ ERROR: `IntoBytes2` has 1 total byte(s) of padding
47 #[zerocopy(crate = "zerocopy_renamed")]
48 #[repr(C)]
49 union IntoBytes2 {
50 foo: u8,
51 bar: [u8; 2],
52 }
53
54 // Need a `repr` attribute
55 #[derive(IntoBytes)]
56 //~[msrv, stable, nightly]^ ERROR: must be #[repr(C)], #[repr(packed)], or #[repr(transparent)]
57 #[zerocopy(crate = "zerocopy_renamed")]
58 union IntoBytes3 {
59 foo: u8,
60 }
61
62 // `repr(packed(2))` isn't equivalent to `repr(packed)`
63 #[derive(IntoBytes)]
64 //~[msrv, stable, nightly]^ ERROR: must be #[repr(C)], #[repr(packed)], or #[repr(transparent)]
65 #[zerocopy(crate = "zerocopy_renamed")]
66 #[repr(packed(2))]
67 union IntoBytes4 {
68 foo: u8,
69 }
70
71 //
72 // Unaligned errors
73 //
74
75 #[derive(Unaligned)]
76 #[zerocopy(crate = "zerocopy_renamed")]
77 #[repr(C, align(2))]
78 //~[msrv, stable, nightly]^ ERROR: cannot derive `Unaligned` on type with alignment greater than 1
79 union Unaligned1 {
80 foo: i16,
81 bar: AU16,
82 }
83
84 // Transparent unions are unstable; see issue #60405
85 // <https://github.com/rust-lang/rust/issues/60405> for more information.
86
87 // #[derive(Unaligned)]
88 // #[repr(transparent, align(2))]
89 // union Unaligned2 {
90 // foo: u8,
91 // }
92
93 #[derive(Unaligned)]
94 #[zerocopy(crate = "zerocopy_renamed")]
95 #[repr(packed, align(2))]
96 //~[msrv, stable, nightly]^ ERROR: this conflicts with another representation hint
97 union Unaligned3 {
98 //~[stable, nightly]^ ERROR: type has conflicting packed and align representation hints
99 foo: u8,
100 }
101
102 #[derive(Unaligned)]
103 #[zerocopy(crate = "zerocopy_renamed")]
104 #[repr(align(1), align(2))]
105 //~[msrv, stable, nightly]^ ERROR: this conflicts with another representation hint
106 struct Unaligned4 {
107 foo: u8,
108 }
109
110 #[derive(Unaligned)]
111 #[zerocopy(crate = "zerocopy_renamed")]
112 #[repr(align(2), align(4))]
113 //~[msrv, stable, nightly]^ ERROR: this conflicts with another representation hint
114 struct Unaligned5 {
115 foo: u8,
116 }
117
118 #[derive(Unaligned)]
119 //~[msrv, stable, nightly]^ ERROR: must have #[repr(C)], #[repr(transparent)], or #[repr(packed)] attribute in order to guarantee this type's alignment
120 #[zerocopy(crate = "zerocopy_renamed")]
121 union Unaligned6 {
122 foo: i16,
123 bar: AU16,
124 }
125
126 #[derive(Unaligned)]
127 //~[msrv, stable, nightly]^ ERROR: must have #[repr(C)], #[repr(transparent)], or #[repr(packed)] attribute in order to guarantee this type's alignment
128 #[zerocopy(crate = "zerocopy_renamed")]
129 #[repr(packed(2))]
130 union Unaligned7 {
131 //~[stable, nightly]^ ERROR: packed type cannot transitively contain a `#[repr(align)]` type
132 foo: i16,
133 bar: AU16,
134 }