@@ -37,6 +37,13 @@ enum representation in C is implementation defined, so this is really a "best
37
37
guess". In particular, this may be incorrect when the C code of interest is
38
38
compiled with certain flags.
39
39
40
+ * "C-like" enums with ` repr(C) ` or ` repr(u*) ` still may not be set to an
41
+ integer value without a corresponding variant, even though this is
42
+ permitted behavior in C or C++. It is undefined behavior to (unsafely)
43
+ construct an instance of an enum that does not match one of its
44
+ variants. (This allows exhaustive matches to continue to be written and
45
+ compiled as normal.)
46
+
40
47
41
48
42
49
# repr(u8), repr(u16), repr(u32), repr(u64)
@@ -47,8 +54,17 @@ ask Rust to allow this by setting the overflowing element to explicitly be 0.
47
54
However Rust will not allow you to create an enum where two variants have the
48
55
same discriminant.
49
56
50
- On non-C-like enums, this will inhibit certain optimizations like the null-
51
- pointer optimization.
57
+ If a C-like enum is missing a ` repr(u*) ` or ` repr(C) ` , it is treated
58
+ like a one-member struct for ABI compatibility purposes. This means
59
+ that, for instance, some ABIs will unconditionally pass it on the stack
60
+ instead of in a register. Adding a ` repr ` causes it to be treated
61
+ exactly like the specified integer size for ABI purposes.
62
+
63
+ However, a non-C-like enum will always be treated like a struct (even if
64
+ the only data is ` () ` or ` PhantomData ` ). Adding ` repr ` simply forces the
65
+ discriminant to be represented in memory by the specified integer size.
66
+ This does have the effect of inhibiting certain optimizations like the
67
+ null-pointer optimization.
52
68
53
69
These reprs have no effect on a struct.
54
70
0 commit comments