Open sfackler opened 5 years ago
@sfackler I don't think there is a good and simple way to fix this
cc @ralfj
The code that generates that use of uninitialized
tests the "call ABI" of a type. It does this by initializing a value of that type to a byte pattern where each byte contains a different value, and then passing it by value to some C code that test that these bytes did not change value.
On Rust >= 1.36.0 we could just use MaybeUninit<T>
for this, and pass the MaybeUninit
to C instead (its repr(transparent)
so the call ABI of the T
is used). But on Rust < 1.36.0... I don't think there is anything that we can do.
If the Rust version is old enough repr(transparent)
isn't available, so we can't use any kind of wrapper type to avoid violating the validity invariant (and if repr(transparent)
is available, we can really provide our own MaybeUninit
type without proper unions and other features that were stabilized later or haven't even been stabilized yet). Without any of that, there isn't really a way to avoid violating the validity invariant somewhere.
warning: the type
unsafe extern "C" fn() -> !
@sfackler - slightly unrelated, but if you are using unsafe extern "C" fn() -> !
in your library FFI as a "return value" (or output argument) of C, you need to make sure that C guarantees that it never sets it to null, or else that's UB. Using Option<unsafe extern "C" fn() -> !>
fixes that issue in your library, but not in ctest tests because creating an uninitialized Option
is UB as well.