Skip to content

Poor error message when the LHS of an assignment must be dereferenced #46276

Closed
@ghost

Description

Code (also available on playground):

use std::sync::Mutex;

fn main() {
    let a = Mutex::new(7);
    let mut a = a.lock().unwrap();
    a += 1;
}

Error:

error[E0368]: binary assignment operation `+=` cannot be applied to type `std::sync::MutexGuard<'_, {integer}>`
 --> src/main.rs:6:5
  |
6 |     a += 1;
  |     -^^^^^
  |     |
  |     cannot use `+=` on type `std::sync::MutexGuard<'_, {integer}>`

Okay, but why cannot += be applied to Mutexuard<'_, {integer}>? And what can we do about it? :)

I'd prefer if the error message included something like:

help: consider dereferencing the left-hand side: `*a += 1`

Such a help message could be printed whenever the LHS of an assignment must be dereferenced. The same applies to situations in which we must dereference more than once, e.g **a.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions