Skip to content

Commit be514d7

Browse files
committed
Document #[repr(u8)] enum to enable LLVM optimizations
See rust-lang/rust#27303 and rust-lang/libc#48 for discussion of the optimization, or just try compiling `unsafe {free(malloc(16))}` with various Rust definitions of `void *` in the FFI binding.
1 parent 616b984 commit be514d7

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/other-reprs.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ same discriminant.
5050
On non-C-like enums, this will inhibit certain optimizations like the null-
5151
pointer optimization.
5252

53+
For C-like enums, these may also be useful to enable certain LLVM optimizations.
54+
For instance, LLVM will recognize extern functions named `malloc` and `free` and
55+
optimize away unnecessary calls, as long as the functions are of the right type:
56+
`malloc` returns a `*mut u8`, and `free` takes one. The libc crate uses
57+
`#[repr(u8)]` on its `c_void` type to enable this optimization. However, note
58+
that this optimization stops working if the enum is non-C-like, even if the only
59+
data is `()` or `PhantomData`!
60+
5361
These reprs have no effect on a struct.
5462

5563

0 commit comments

Comments
 (0)