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

Missing parentheses in invalid cast suggestion with multiple chained casts #89497

Closed
pr2502 opened this issue Oct 3, 2021 · 0 comments · Fixed by #89528
Closed

Missing parentheses in invalid cast suggestion with multiple chained casts #89497

pr2502 opened this issue Oct 3, 2021 · 0 comments · Fixed by #89528
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

@pr2502
Copy link
Contributor

pr2502 commented Oct 3, 2021

Suggestions for invalid casts don't take precedence into account.

Ideally I think it should detect an attempted *const T to &T and/or *mut T to &mut T cast and suggest replacing it with the dereferencing and reborrowing.

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=b31bf32a3d115ea22c88ef192e71ea4f

fn main() {
    let pointer: usize = &1_i32 as *const i32 as usize;
    let reference: &'static i32 = unsafe { pointer as *const i32 as &'static i32 };
}

The current output is:

error[E0605]: non-primitive cast: `*const i32` as `&'static i32`
 --> src/main.rs:3:44
  |
3 |     let reference: &'static i32 = unsafe { pointer as *const i32 as &'static i32 };
  |                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
  |
help: consider borrowing the value
  |
3 |     let reference: &'static i32 = unsafe { &*pointer as *const i32 as &'static i32 };
  |                                            ^^

Ideally the output should look like:

error[E0605]: non-primitive cast: `*const i32` as `&'static i32`
 --> src/main.rs:3:44
  |
3 |     let reference: &'static i32 = unsafe { pointer as *const i32 as &'static i32 };
  |                                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ invalid cast
  |
help: raw pointer cannot be cast to a reference, consider dereferencing and borrowing it
  |
3 |     let reference: &'static i32 = unsafe { &*(pointer as *const i32) };
  |                                            ^^^                     ^
@pr2502 pr2502 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 Oct 3, 2021
Manishearth added a commit to Manishearth/rust that referenced this issue Oct 6, 2021
Fix suggestion to borrow when casting from pointer to reference

Fixes rust-lang#89497.
@bors bors closed this as completed in f5bfa34 Oct 6, 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