]> git.lizzy.rs Git - rust.git/blob - tests/ui/derives/deriving-with-repr-packed.rs
Extend `BYTE_SLICE_IN_PACKED_STRUCT_WITH_DERIVE`.
[rust.git] / tests / ui / derives / deriving-with-repr-packed.rs
1 // Check that deriving certain builtin traits on certain packed structs cause
2 // errors. To avoid potentially misaligned references, field copies must be
3 // used, which involves adding `T: Copy` bounds.
4
5 #[derive(Copy, Clone, Default, PartialEq, Eq)]
6 #[repr(packed)]
7 pub struct Foo<T>(T, T, T);
8
9 // This one is fine because the fields all impl `Copy`.
10 #[derive(Default, Hash)]
11 #[repr(packed)]
12 pub struct Bar(u32, u32, u32);
13
14 // This one is fine because it's not packed.
15 #[derive(Debug, Default)]
16 struct Y(usize);
17
18 // This one has an error because `Y` doesn't impl `Copy`.
19 // Note: there is room for improvement in the error message.
20 #[derive(Debug, Default)]
21 #[repr(packed)]
22 struct X(Y);
23 //~^ ERROR cannot move out of `self` which is behind a shared reference
24
25 // This is currently allowed, but will be phased out at some point. From
26 // `zerovec` within icu4x-0.9.0.
27 #[derive(Debug)]
28 #[repr(packed)]
29 struct FlexZeroSlice {
30     width: u8,
31     data: [u8],
32     //~^ WARNING byte slice in a packed struct that derives a built-in trait
33     //~^^ this was previously accepted
34 }
35
36 // Again, currently allowed, but will be phased out.
37 #[derive(Debug)]
38 #[repr(packed)]
39 struct WithStr {
40     width: u8,
41     data: str,
42     //~^ WARNING string slice in a packed struct that derives a built-in trait
43     //~^^ this was previously accepted
44 }
45
46 fn main() {}