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