@@ -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
+ The term "C-like enum" only means that the enum doesn't have data in any
58
+ of its variants. A C-like enum without a ` repr(u*) ` or ` repr(C) ` is
59
+ still a Rust native type, and does not have a stable ABI representation.
60
+ Adding a ` repr ` causes it to be treated exactly like the specified
61
+ integer size for ABI purposes.
62
+
63
+ A non-C-like enum is a Rust type with no guaranteed ABI (even if the
64
+ only data is ` PhantomData ` or something else with zero size).
65
+
66
+ Adding an explicit ` repr ` to an enum suppresses the null-pointer
67
+ optimization.
52
68
53
69
These reprs have no effect on a struct.
54
70
0 commit comments