Closed
Description
Code
extern "C" {
// hypothetical C function which takes a nullable pointer
fn foo(ptr: *const u8);
}
fn main() {
unsafe { foo(0); }
}
Current output
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/main.rs:6:18
|
6 | unsafe { foo(0); }
| --- ^ expected `*const u8`, found `usize`
| |
| arguments to this function are incorrect
|
= note: expected raw pointer `*const u8`
found type `usize`
note: function defined here
--> src/main.rs:2:8
|
2 | fn foo(ptr: *const u8);
| ^^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error
Desired output
Compiling playground v0.0.1 (/playground)
error[E0308]: mismatched types
--> src/main.rs:6:18
|
6 | unsafe { foo(0); }
| --- ^ expected `*const u8`, found `usize`
| |
| arguments to this function are incorrect
|
= note: expected raw pointer `*const u8`
found type `usize`
= help: if you're trying to create a null pointer, use
std::ptr::null()
note: function defined here
--> src/main.rs:2:8
|
2 | fn foo(ptr: *const u8);
| ^^^
For more information about this error, try `rustc --explain E0308`.
error: could not compile `playground` due to previous error
Rationale and extra context
(Note the added help line.)
C programers could reasonably expect 0
in a raw-pointer position to mean null. Seems like this could be a fairly simple check to save them a google.
(For reference: I'm a fairly experienced Rust programmer, and a novice C programmer, but I haven't done much FFI. I couldn't remember what std::ptr::null
was called, so I tried 0; I was pretty sure it wasn't going to work, but I hoped I might at least get a helpful error.)
Other cases
No response
Anything else?
No response