Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add help when trying to use literal 0 as raw pointer #107517

Closed
Gaelan opened this issue Jan 31, 2023 · 1 comment · Fixed by #107553
Closed

Add help when trying to use literal 0 as raw pointer #107517

Gaelan opened this issue Jan 31, 2023 · 1 comment · Fixed by #107553
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Gaelan
Copy link

Gaelan commented Jan 31, 2023

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

@Gaelan Gaelan added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 31, 2023
@edward-shen
Copy link
Contributor

Seems like an easy thing to fix and a reasonable line of logic?

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants