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 error message when trying to assign to a field through raw pointer #91210

Closed
thomcc opened this issue Nov 25, 2021 · 2 comments · Fixed by #91364
Closed

Bad error message when trying to assign to a field through raw pointer #91210

thomcc opened this issue Nov 25, 2021 · 2 comments · Fixed by #91364
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@thomcc
Copy link
Member

thomcc commented Nov 25, 2021

If I try to assign to a value named read through a raw pointer, I get a particularly bad error message

struct Foo { read: i32 }

fn blah(x: *mut Foo) {
    x.read = 4;
}
error[E0615]: attempted to take value of method `read` on type `*mut Foo`
 --> src/lib.rs:4:7
  |
4 |     x.read = 4;
  |       ^^^^ method, not a field
  |
  = help: methods are immutable and cannot be assigned to

For more information about this error, try `rustc --explain E0615`.
error: could not compile `playground` due to previous error

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

I think this happens because it finds core::ptr::read's method version on *mut T. It would be nice if we suggested1 something like (*x).read instead.

Footnotes

  1. I guess theres a bit of trickery here since... well, technically for non-copy thats being initialized for the first time, the right thing would be addr_of_mut!((*x).read).write(val)... So maybe a fix shouldnt be offered, just a better diagnostic.

@thomcc
Copy link
Member Author

thomcc commented Nov 25, 2021

@rustbot modify labels: +A-diagnostics

@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Nov 25, 2021
@thomcc
Copy link
Member Author

thomcc commented Nov 25, 2021

If the field isn't "shadowed" by a method name, it says something like

error[E0609]: no field `red` on type `*mut Foo`
 --> src/lib.rs:4:7
  |
4 |     x.red = 4;
  |     --^^^
  |     |
  |     help: `x` is a raw pointer; try dereferencing it: `(*x).red`

which is probbaly just what it should say always.

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants