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

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

Closed
saethlin opened this issue Aug 22, 2022 · 3 comments · Fixed by #100898
Closed
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

@saethlin
Copy link
Member

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.

@saethlin saethlin 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 Aug 22, 2022
@compiler-errors
Copy link
Member

ugh I added this. I can cut it down.

@compiler-errors compiler-errors self-assigned this Aug 22, 2022
@saethlin
Copy link
Member Author

FWIW, I was aiming for a field in the struct that is a RefCell. I just did struct.borrow_mut().field instead of struct.field.borrow_mut().

IMO the biggest problem with this suggestion at the moment is that it's suggesting that I call a method on a trait that's not in scope, when the RefCell method is, and that seems like the better suggestion to me. But maybe that's just bias from this particular scenario.

@compiler-errors
Copy link
Member

compiler-errors commented Aug 23, 2022

IMO the biggest problem with this suggestion at the moment is that it's suggesting that I call a method on a trait that's not in scope

Yeah, I fixed that too. PR incoming.

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Aug 27, 2022
…ds, r=spastorino

Do not report too many expr field candidates

When considering "this expressions' field has a {field/method}" suggestions:
1. Don't report methods that are out of scope
2. Use `span_suggestions` instead of reporting each field candidate, which caps the number of suggestions to 4
4. Blacklist some common traits like `Clone` and `Deref`

Fixes rust-lang#100894
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 28, 2022
…ds, r=spastorino

Do not report too many expr field candidates

When considering "this expressions' field has a {field/method}" suggestions:
1. Don't report methods that are out of scope
2. Use `span_suggestions` instead of reporting each field candidate, which caps the number of suggestions to 4
4. Blacklist some common traits like `Clone` and `Deref`

Fixes rust-lang#100894
@bors bors closed this as completed in 091017c Aug 30, 2022
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