Skip to content

Way too much (unhelpful) help is provided when mistakenly calling .borrow_mut() on a struct #100894

Closed
@saethlin

Description

@saethlin

Given the following code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=7da23c03f267a3483798d0615ee24563

fn main() {
    let t = Thing { x: 0, y: 0, z: 0 };
    *t.borrow_mut() = 1;
}

struct Thing {
    x: i32,
    y: i32,
    z: i32,
}

The current output is:

error[[E0599]](https://doc.rust-lang.org/nightly/error-index.html#E0599): no method named `borrow_mut` found for struct `Thing` in the current scope
 --> src/main.rs:3:8
  |
3 |     *t.borrow_mut() = 1;
  |        ^^^^^^^^^^ method not found in `Thing`
...
6 | struct Thing {
  | ------------ method `borrow_mut` not found for this struct
  |
  = help: items from traits can only be used if the trait is in scope
help: one of the expressions' fields has a method of the same name
  |
3 |     *t.x.borrow_mut() = 1;
  |        ++
help: one of the expressions' fields has a method of the same name
  |
3 |     *t.y.borrow_mut() = 1;
  |        ++
help: one of the expressions' fields has a method of the same name
  |
3 |     *t.z.borrow_mut() = 1;
  |        ++
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
  |
1 | [use std::borrow::BorrowMut;](https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=7da23c03f267a3483798d0615ee24563#)
  |

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

The output gets really out of control if you have a struct with 50 fields or so, which is what I'm currently looking at.

At the moment this only happens on nightly.

Metadata

Metadata

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions