rust-lang / rust-lang/rust

Diagnostics should not assume omission of impl DerefMut is unintended

Open
#153,954 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
struct A;

impl A {
    fn edit(&mut self) {}
}

struct B(A);

impl std::ops::Deref for B {
    type Target = A;
    fn deref(&self) -> &A { &self.0 }
}

fn main() {
    let b = B(A);
    b.edit();
}
Current output
error[E0596]: cannot borrow data in dereference of `B` as mutable
  --> src/main.rs:16:5
   |
16 |     b.edit();
   |     ^ cannot borrow as mutable
   |
   = help: trait `DerefMut` is required to modify through a dereference, but it is not implemented for `B`

For more information about this error, try `rustc --explain E0596`.
error: could not compile `playground` (bin "playground") due to 1 previous error
Desired output
error[E0599]: no method named `edit` found for struct `B` in the current scope
  --> src/main.rs:11:7
   |
 7 | struct B(A);
   | -------- method `edit` not found for this struct
...
11 |     b.edit();
   |       ^^^^ method not found in `B`
   |
help: one of the expressions' fields has a method of the same name
   |
11 |     b.0.edit();
   |       ++

The above is the output shown when impl Deref is removed. It could be amended to mention the lack of a DerefMut impl but should also read well when the latter trait is intentionally not implemented.

Rationale and extra context

Motivation: it may be that the derived type B can safely implement read support for its component A via Deref, but that other actions must be performed on edits and thus that DerefMut cannot be implemented.

Other cases

Note also: if B does implement fn edit(&mut self) {} but this is inaccessible due to privacy rules, the existing diagnostic does not mention fn B::edit at all.

Rust Version

Tested against both stable (1.94.0) and nightly (2026-03-12) on play.rust-lang.org

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the supplied Rust example on stable and nightly, then compare the E0596 output with the E0599 output after removing Deref. Trace the compiler diagnostics for method lookup and mutable dereference, including the privacy case. Done means the diagnostic no longer assumes missing DerefMut is unintended and remains useful for inaccessible methods.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.