Skip to content

Clarify operand evaluation order in compound assignment with primitiv… #1941

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/expressions/operator-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -903,12 +903,16 @@ r[expr.compound-assign.operand-order]
Evaluation of compound assignment expressions depends on the types of the operators.

r[expr.compound-assign.primitive-order]
If both types are primitives, then the modifying operand will be evaluated first followed by the assigned operand.
If both types are primitives and the expression is non-generic (i.e., directly uses concrete types), then the modifying operand will be evaluated first followed by the assigned operand.
It will then set the value of the assigned operand's place to the value of performing the operation of the operator with the values of the assigned operand and modifying operand.

> [!NOTE]
> This is different than other expressions in that the right operand is evaluated before the left one.

> [!NOTE]
> This right-before-left evaluation only occurs in non-generic code involving primitive types.
> In all other cases---including generic contexts or non-primitive types---the expression is desugared into a trait method call, and operands are evaluated left to right.

r[expr.compound-assign.trait]
Otherwise, this expression is syntactic sugar for calling the function of the overloading compound assignment trait of the operator (see the table earlier in this chapter).
A mutable borrow of the assigned operand is automatically taken.
Expand Down Expand Up @@ -937,9 +941,10 @@ r[expr.compound-assign.result]
Like assignment expressions, compound assignment expressions always produce [the unit value][unit].

> [!WARNING]
> The evaluation order of operands swaps depending on the types of the operands:
> with primitive types the right-hand side will get evaluated first, while with non-primitive types the left-hand side will get evaluated first.
> Try not to write code that depends on the evaluation order of operands in compound assignment expressions.
> The evaluation order of operands varies depending on how the expression is desugared.
> Non-generic primitive assignments may evaluate the right-hand side first.
> Trait-based assignments, including all generic cases, evaluate left-hand side first.
> Avoid writing code that depends on operand evaluation order.
> See [this test] for an example of using this dependency.

[`Try`]: core::ops::Try
Expand Down