Skip to content

Commit 864ca4a

Browse files
committed
Revise text about compound assignment eval order
1 parent c613c1e commit 864ca4a

File tree

1 file changed

+50
-31
lines changed

1 file changed

+50
-31
lines changed

src/expressions/operator-expr.md

Lines changed: 50 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -902,9 +902,8 @@ Attempting to use a value expression is a compiler error rather than promoting i
902902
r[expr.compound-assign.operand-order]
903903
Evaluation of compound assignment expressions depends on the types of the operands.
904904

905-
r[expr.compound-assign.primitive-order]
906-
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.
907-
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.
905+
r[expr.compound-assign.primitives]
906+
If the types of both operands are known, prior to monomorphization, to be primitive, the right hand side is evaluated first, the left hand side is evaluated next, and the place given by the evaluation of the left hand side is mutated by applying the operator to the values of both sides.
908907

909908
```rust
910909
# use core::{num::Wrapping, ops::AddAssign};
@@ -948,49 +947,69 @@ fn main() {
948947
```
949948

950949
> [!NOTE]
951-
> This is different than other expressions in that the right operand is evaluated before the left one.
952-
953-
> [!NOTE]
954-
> This right-before-left evaluation only occurs in non-generic code involving primitive types.
955-
> 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.
950+
> This is unusual. Elsewhere left to right evaluation is the norm.
951+
>
952+
> See the [eval order test] for more examples.
956953
957954
r[expr.compound-assign.trait]
958-
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).
959-
A mutable borrow of the assigned operand is automatically taken.
955+
Otherwise, this expression is syntactic sugar for using the corresponding trait for the operator (see [expr.arith-logic.behavior]) and calling its method with the left hand side as the [receiver] and the right hand side as the next argument.
960956

961-
For example, the following expression statements in `example` are equivalent:
957+
For example, the following two statements are equivalent:
962958

963959
```rust
964-
# struct Addable;
965960
# use std::ops::AddAssign;
966-
967-
impl AddAssign<Addable> for Addable {
968-
/* */
969-
# fn add_assign(&mut self, other: Addable) {}
970-
}
971-
972-
fn example() {
973-
# let (mut a1, a2) = (Addable, Addable);
974-
a1 += a2;
975-
976-
# let (mut a1, a2) = (Addable, Addable);
977-
AddAssign::add_assign(&mut a1, a2);
961+
fn f<T: AddAssign + Copy>(mut x: T, y: T) {
962+
x += y; // Statement 1.
963+
x.add_assign(y); // Statement 2.
978964
}
979965
```
980966

967+
> [!NOTE]
968+
> Surprisingly, desugaring this further to a fully qualified method call is not equivalent, as there is special borrow checker behavior when the mutable reference to the first operand is taken via [autoref].
969+
>
970+
> ```rust
971+
> # use std::ops::AddAssign;
972+
> fn f<T: AddAssign + Copy>(mut x: T) {
973+
> // Here we used `x` as both the LHS and the RHS. Because the
974+
> // mutable borrow of the LHS needed to call the trait method
975+
> // is taken implicitly by autoref, this is OK.
976+
> x += x; //~ OK
977+
> x.add_assign(x); //~ OK
978+
> }
979+
> ```
980+
>
981+
> ```rust,compile_fail,E0503
982+
> # use std::ops::AddAssign;
983+
> fn f<T: AddAssign + Copy>(mut x: T) {
984+
> // We can't desugar the above to the below, as once we take the
985+
> // mutable borrow of `x` to pass the first argument, we can't
986+
> // pass `x` by value in the second argument because the mutable
987+
> // reference is still live.
988+
> <T as AddAssign>::add_assign(&mut x, x);
989+
> //~^ ERROR cannot use `x` because it was mutably borrowed
990+
> }
991+
> ```
992+
>
993+
> ```rust,compile_fail,E0503
994+
> # use std::ops::AddAssign;
995+
> fn f<T: AddAssign + Copy>(mut x: T) {
996+
> // As above.
997+
> (&mut x).add_assign(x);
998+
> //~^ ERROR cannot use `x` because it was mutably borrowed
999+
> }
1000+
> ```
1001+
9811002
r[expr.compound-assign.result]
982-
Like assignment expressions, compound assignment expressions always produce [the unit value][unit].
1003+
As with normal assignment expressions, compound assignment expressions always produce [the unit value][unit].
9831004
9841005
> [!WARNING]
985-
> The evaluation order of operands varies depending on how the expression is desugared.
986-
> Non-generic primitive assignments may evaluate the right-hand side first.
987-
> Trait-based assignments, including all generic cases, evaluate left-hand side first.
988-
> Avoid writing code that depends on operand evaluation order.
989-
> See [this test] for an example of using this dependency.
1006+
> Avoid writing code that depends on the evaluation order of operands in compound assignments as it can be unusual and surprising.
9901007
9911008
[`Try`]: core::ops::Try
1009+
[autoref]: expr.method.candidate-receivers-refs
9921010
[copies or moves]: ../expressions.md#moved-and-copied-types
9931011
[dropping]: ../destructors.md
1012+
[eval order test]: https://github.com/rust-lang/rust/blob/1.58.0/src/test/ui/expr/compound-assignment/eval-order.rs
9941013
[explicit discriminants]: ../items/enumerations.md#explicit-discriminants
9951014
[field-less enums]: ../items/enumerations.md#field-less-enum
9961015
[grouped expression]: grouped-expr.md
@@ -1007,10 +1026,10 @@ Like assignment expressions, compound assignment expressions always produce [the
10071026
[Unit-only enums]: ../items/enumerations.md#unit-only-enum
10081027
[value expression]: ../expressions.md#place-expressions-and-value-expressions
10091028
[temporary value]: ../expressions.md#temporaries
1010-
[this test]: https://github.com/rust-lang/rust/blob/1.58.0/src/test/ui/expr/compound-assignment/eval-order.rs
10111029
[float-float]: https://github.com/rust-lang/rust/issues/15536
10121030
[Function pointer]: ../types/function-pointer.md
10131031
[Function item]: ../types/function-item.md
1032+
[receiver]: expr.method.intro
10141033
[undefined behavior]: ../behavior-considered-undefined.md
10151034
[Underscore expressions]: ./underscore-expr.md
10161035
[range expressions]: ./range-expr.md

0 commit comments

Comments
 (0)