rust-lang / rust-lang/reference

Compound assignment desugaring doesn't take into account autoderef

Open
#2,085 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
1.6k
Forks
607
PR merge metrics
PR metrics pending

Description

In the reference, compound assignment expressions with non-primitive operands are described as being desugared into method calls. That is, x += y is turned into x.add_assign(y).

This desugaring is incorrect. The += form does not do autoderef, while the .add_assign form does autoderef. For example:

use std::ops::AddAssign;
fn foo<T: AddAssign<U>, U>(x: &mut &mut T, y: U) {
    // This compiles
    x.add_assign(y);
    // This doesn't compile
    x += y;
}

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 with the compound-assignment section linked in the Rust Reference and compare its stated desugaring with the provided autoderef example. Verify the behavior against the example, then update the reference text so it accurately distinguishes += from an .add_assign method call; done when the documentation no longer implies equivalent autoderef behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.