]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0732.md
Rollup merge of #92310 - ehuss:rustdoc-ice, r=estebank
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0732.md
1 An `enum` with a discriminant must specify a `#[repr(inttype)]`.
2
3 Erroneous code example:
4
5 ```compile_fail,E0732
6 #![feature(arbitrary_enum_discriminant)]
7
8 enum Enum { // error!
9     Unit = 1,
10     Tuple() = 2,
11     Struct{} = 3,
12 }
13 # fn main() {}
14 ```
15
16 A `#[repr(inttype)]` must be provided on an `enum` if it has a non-unit
17 variant with a discriminant, or where there are both unit variants with
18 discriminants and non-unit variants. This restriction ensures that there
19 is a well-defined way to extract a variant's discriminant from a value;
20 for instance:
21
22 ```
23 #![feature(arbitrary_enum_discriminant)]
24
25 #[repr(u8)]
26 enum Enum {
27     Unit = 3,
28     Tuple(u16) = 2,
29     Struct {
30         a: u8,
31         b: u16,
32     } = 1,
33 }
34
35 fn discriminant(v : &Enum) -> u8 {
36     unsafe { *(v as *const Enum as *const u8) }
37 }
38
39 fn main() {
40     assert_eq!(3, discriminant(&Enum::Unit));
41     assert_eq!(2, discriminant(&Enum::Tuple(5)));
42     assert_eq!(1, discriminant(&Enum::Struct{a: 7, b: 11}));
43 }
44 ```