Skip to content

Commit b4ada32

Browse files
authored
Merge pull request #8 from geofft/master
Document `#[repr(u8)] enum` to enable LLVM optimizations
2 parents ec0ddbb + 6875569 commit b4ada32

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/other-reprs.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ enum representation in C is implementation defined, so this is really a "best
3737
guess". In particular, this may be incorrect when the C code of interest is
3838
compiled with certain flags.
3939

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+
4047

4148

4249
# 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.
4754
However Rust will not allow you to create an enum where two variants have the
4855
same discriminant.
4956

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.
5268

5369
These reprs have no effect on a struct.
5470

0 commit comments

Comments
 (0)