Description
The original problem got fixed by avoiding the broken code paths; this issue now tracks fixing those code paths.
Original issue
In the ffi_helpers
crate we have a Nullable
trait which gives you a generic way to do null pointer checks.
pub trait Nullable {
const NULL: Self;
fn is_null(&self) -> bool;
}
A user recently reported that the crate no longer compiles on nightly
(Michael-F-Bryan/ffi_helpers#2) because type validation detects that std::ptr::null()
and std::ptr::null_mut()
create uninitialized raw pointers.
Compiling playground v0.0.1 (/playground)
error[E0080]: it is undefined behavior to use this value
--> src/lib.rs:17:5
|
17 | const NULL: Self = std::ptr::null_mut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized raw pointer
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground`.
I can reproduce this on the playground with the latest nightly, 1.44.0-nightly (2020-04-19 dbf8b6bf116c7bece298)
.
Is the use shown in that playground example (*self == Self::NULL
on a *mut T
) actually UB?
Also, I noticed that calling the is_null()
method defined on a raw pointer with <*const T>::is_null(*self)
doesn't trigger this error, implying that the problem isn't with declaring a constant that contains a null pointer (const NULL: Self = std::ptr::null_mut()
), but the fact that we're using it for a comparison. Was this intended, or is it just an oversight in the error detection code?
Full example with output
pub trait Nullable {
const NULL: Self;
fn is_null(&self) -> bool;
}
impl<T> Nullable for *const T {
const NULL: Self = std::ptr::null();
#[inline]
fn is_null(&self) -> bool {
<*const T>::is_null(*self)
}
}
impl<T> Nullable for *mut T {
const NULL: Self = std::ptr::null_mut();
#[inline]
fn is_null(&self) -> bool {
*self == Self::NULL
}
}
Compiling playground v0.0.1 (/playground)
error[E0080]: it is undefined behavior to use this value
--> src/lib.rs:17:5
|
17 | const NULL: Self = std::ptr::null_mut();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered uninitialized raw pointer
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground`.
To learn more, run the command again with --verbose.
CC: @RalfJung because it looks like they were the last person to touch that error message (9ee4d1a).
This issue has been assigned to @jumbatm via this comment.