Skip to content

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

Closed
@thomcc

Description

@thomcc

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions