Skip to content

Confusing diagnostics when calling FnMut through DerefMut. #72225

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
nbdd0121 opened this issue May 15, 2020 · 0 comments · Fixed by #72280
Closed

Confusing diagnostics when calling FnMut through DerefMut. #72225

nbdd0121 opened this issue May 15, 2020 · 0 comments · Fixed by #72280
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nbdd0121
Copy link
Contributor

The following code

struct A {
    x: Box<dyn FnMut()>
}

fn test(a: &std::sync::Mutex<A>) {
    let mut guard = a.lock().unwrap();
    // This works: (&mut guard.x)();
    (guard.x)();
}

produces the following error

error[E0596]: cannot borrow data in a dereference of `std::sync::MutexGuard<'_, A>` as mutable
 --> src/lib.rs:7:5
  |
7 |     (guard.x)();
  |     ^^^^^^^^^ cannot borrow as mutable
  |
  = help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `std::sync::MutexGuard<'_, A>`

The help message is especially confusing, since DerefMut is clearly implemeneted for std::sync::MutexGuard<'_, A>. This might be related to #68590.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 15, 2020
@estebank estebank added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label May 15, 2020
@bors bors closed this as completed in 70622db Jun 19, 2020
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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.

3 participants