Skip to content

Commit b77a7e7

Browse files
committed
auto merge of #11539 : dotdash/rust/void_type_fixup, r=alexcrichton
Currently, we have c_void defined to be represented as an empty struct, but LLVM expects C's void* to be represented as i8*. That means we currently generate code in which LLVM doesn't recognize malloc() and free() and can't apply certain optimization that would remove calls to those functions.
2 parents d150f6b + 5902263 commit b77a7e7

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/libstd/libc.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,19 @@ pub mod types {
194194
This type is only useful as a pointer target. Do not use it as a
195195
return type for FFI functions which have the `void` return type in
196196
C. Use the unit type `()` or omit the return type instead.
197+
198+
For LLVM to recognize the void pointer type and by extension
199+
functions like malloc(), we need to have it represented as i8* in
200+
LLVM bitcode. The enum used here ensures this and prevents misuse
201+
of the "raw" type by only having private variants.. We need two
202+
variants, because the compiler complains about the repr attribute
203+
otherwise.
197204
*/
198-
pub enum c_void {}
205+
#[repr(u8)]
206+
pub enum c_void {
207+
priv variant1,
208+
priv variant2
209+
}
199210
pub enum FILE {}
200211
pub enum fpos_t {}
201212
}

0 commit comments

Comments
 (0)