-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Given the following code:
struct Something {
pub field: u32,
}
fn main() {
let something = Something {
field: 1337,
};
let pointer_to_something = something as *const Something;
}
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
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.