]> git.lizzy.rs Git - rust.git/blob - tests/ui/derives/deriving-with-repr-packed.rs
Rollup merge of #103236 - tspiteri:redoc-int-adc-sbb, r=m-ou-se
[rust.git] / tests / ui / derives / deriving-with-repr-packed.rs
1 #![deny(unaligned_references)]
2
3 // Check that deriving certain builtin traits on certain packed structs cause
4 // errors. This happens when the derived trait would need to use a potentially
5 // misaligned reference. But there are two cases that are allowed:
6 // - If all the fields within the struct meet the required alignment: 1 for
7 //   `repr(packed)`, or `N` for `repr(packed(N))`.
8 // - If `Default` is the only trait derived, because it doesn't involve any
9 //   references.
10
11 #[derive(Copy, Clone, Default, PartialEq, Eq)]
12 //~^ ERROR `Clone` can't be derived on this `#[repr(packed)]` struct with type or const parameters
13 //~| hard error
14 //~^^^ ERROR `PartialEq` can't be derived on this `#[repr(packed)]` struct with type or const parameters
15 //~| hard error
16 #[repr(packed)]
17 pub struct Foo<T>(T, T, T);
18
19 #[derive(Default, Hash)]
20 //~^ ERROR `Hash` can't be derived on this `#[repr(packed)]` struct that does not derive `Copy`
21 //~| hard error
22 #[repr(packed)]
23 pub struct Bar(u32, u32, u32);
24
25 // This one is fine because the field alignment is 1.
26 #[derive(Default, Hash)]
27 #[repr(packed)]
28 pub struct Bar2(u8, i8, bool);
29
30 // This one is fine because the field alignment is 2, matching `packed(2)`.
31 #[derive(Default, Hash)]
32 #[repr(packed(2))]
33 pub struct Bar3(u16, i16, bool);
34
35 // This one is fine because it's not packed.
36 #[derive(Debug, Default)]
37 struct Y(usize);
38
39 #[derive(Debug, Default)]
40 //~^ ERROR `Debug` can't be derived on this `#[repr(packed)]` struct that does not derive `Copy`
41 //~| hard error
42 #[repr(packed)]
43 struct X(Y);
44
45 fn main() {}