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

Bad suggestion when *raw_ptr is used where a reference is expected #135580

Closed
RalfJung opened this issue Jan 16, 2025 · 1 comment · Fixed by #135601
Closed

Bad suggestion when *raw_ptr is used where a reference is expected #135580

RalfJung opened this issue Jan 16, 2025 · 1 comment · Fixed by #135601
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

@RalfJung
Copy link
Member

RalfJung commented Jan 16, 2025

Code

fn foo(bar: *const [u8]) {
   let baz: &[u8] = unsafe { *bar };
}

Current output

error[E0308]: mismatched types
 --> src/lib.rs:2:30
  |
2 |    let baz: &[u8] = unsafe { *bar };
  |                              ^^^^ expected `&[u8]`, found `[u8]`
  |
help: consider removing deref here
  |
2 -    let baz: &[u8] = unsafe { *bar };
2 +    let baz: &[u8] = unsafe { bar };
  |

Desired output

error[E0308]: mismatched types
 --> src/lib.rs:2:30
  |
2 |    let baz: &[u8] = unsafe { *bar };
  |                              ^^^^ expected `&[u8]`, found `[u8]`
  |
help: consider creating a reference here
  |
2 -    let baz: &[u8] = unsafe { *bar };
2 +    let baz: &[u8] = unsafe { &*bar };
  |

Rationale and extra context

The current suggestion is plain wrong, the result does not build.

Note that one definitely wants the & to go inside the unsafe block to avoid creating a temporary! For this example, the type is non-Copy so a temporary wouldn't even be possible, but in other cases both ways could compiler but they do not behave the same.

Other cases

Rust Version

current nightly

Anything else?

No response

@RalfJung RalfJung 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 16, 2025
@RalfJung RalfJung changed the title Bad suggesting when *raw_ptr is used where a reference is expected Bad suggestion when *raw_ptr is used where a reference is expected Jan 16, 2025
@samueltardieu
Copy link
Contributor

@rustbot claim

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jan 17, 2025
…=jieyouxu

Fix suggestion to convert dereference of raw pointer to ref

Fix rust-lang#135580
@bors bors closed this as completed in 8280407 Jan 17, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Jan 17, 2025
Rollup merge of rust-lang#135601 - samueltardieu:push-xslotxrnooym, r=jieyouxu

Fix suggestion to convert dereference of raw pointer to ref

Fix rust-lang#135580
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