]> git.lizzy.rs Git - rust.git/blob - src/librustc_error_codes/error_codes/E0517.md
Rollup merge of #66576 - pnkfelix:more-robust-gdb-vec-printer, r=alexcrichton
[rust.git] / src / librustc_error_codes / error_codes / E0517.md
1 This error indicates that a `#[repr(..)]` attribute was placed on an
2 unsupported item.
3
4 Examples of erroneous code:
5
6 ```compile_fail,E0517
7 #[repr(C)]
8 type Foo = u8;
9
10 #[repr(packed)]
11 enum Foo {Bar, Baz}
12
13 #[repr(u8)]
14 struct Foo {bar: bool, baz: bool}
15
16 #[repr(C)]
17 impl Foo {
18     // ...
19 }
20 ```
21
22 * The `#[repr(C)]` attribute can only be placed on structs and enums.
23 * The `#[repr(packed)]` and `#[repr(simd)]` attributes only work on structs.
24 * The `#[repr(u8)]`, `#[repr(i16)]`, etc attributes only work on enums.
25
26 These attributes do not work on typedefs, since typedefs are just aliases.
27
28 Representations like `#[repr(u8)]`, `#[repr(i64)]` are for selecting the
29 discriminant size for enums with no data fields on any of the variants, e.g.
30 `enum Color {Red, Blue, Green}`, effectively setting the size of the enum to
31 the size of the provided type. Such an enum can be cast to a value of the same
32 type as well. In short, `#[repr(u8)]` makes the enum behave like an integer
33 with a constrained set of allowed values.
34
35 Only field-less enums can be cast to numerical primitives, so this attribute
36 will not apply to structs.
37
38 `#[repr(packed)]` reduces padding to make the struct size smaller. The
39 representation of enums isn't strictly defined in Rust, and this attribute
40 won't work on enums.
41
42 `#[repr(simd)]` will give a struct consisting of a homogeneous series of machine
43 types (i.e., `u8`, `i32`, etc) a representation that permits vectorization via
44 SIMD. This doesn't make much sense for enums since they don't consist of a
45 single list of data.