Skip to content

Confusing diagnostics when calling FnMut through DerefMut. #72225

Closed
@nbdd0121

Description

@nbdd0121

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-confusingDiagnostics: Confusing error or lint that should be reworked.T-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