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

Casting owned type to raw pointer has unclear error message #84213

Closed
nschoellhorn opened this issue Apr 15, 2021 · 0 comments · Fixed by #84228
Closed

Casting owned type to raw pointer has unclear error message #84213

nschoellhorn opened this issue Apr 15, 2021 · 0 comments · Fixed by #84228
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

@nschoellhorn
Copy link

Given the following code:

struct Something {
    pub field: u32,
}

fn main() {
    let something = Something {
        field: 1337,
    };
    
    let pointer_to_something = something as *const Something;
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=1a1c55703dee4ed2078b5c704bd1f379

The current output is:

error[E0605]: non-primitive cast: `Something` as `*const Something`
  --> src/main.rs:10:41
   |
10 |     let pointer_to_something = something as *const Something;
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object

I think, that in the context of casting to a pointer, the output in this case should make it clear that the error could be fixed by taking a reference to the type and cast the reference to a raw pointer.
Example:

error[E0605]: non-primitive cast: `Something` as `*const Something`
  --> src/main.rs:10:41
   |
10 |     let pointer_to_something = something as *const Something;
   |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot cast an owned type to a raw pointer
   |     consider borrowing here: &something as *const Something
@nschoellhorn nschoellhorn 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 Apr 15, 2021
@bors bors closed this as completed in 710da44 Apr 16, 2021
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.

1 participant