1 // Test evaluation order of operands of the compound assignment operators
5 use std::ops::AddAssign;
12 // In the following tests, we place our value into a wrapper type so that we
13 // can do an element access as the outer place expression. If we just had the
14 // block expression, it'd be a value expression and not compile.
17 // Evaluation order for `a op= b` where typeof(a) and typeof(b) are primitives
18 // is first `b` then `a`.
19 fn primitive_compound() {
20 let mut side_order = vec![];
21 let mut int = Wrapper(0);
24 side_order.push(Side::Lhs);
27 side_order.push(Side::Rhs);
31 assert!(matches!(side_order[..], [Side::Rhs, Side::Lhs]));
34 // Evaluation order for `a op=b` otherwise is first `a` then `b`.
35 fn generic_compound<T: AddAssign<T> + Default>() {
36 let mut side_order = vec![];
37 let mut add_assignable: Wrapper<T> = Wrapper(Default::default());
40 side_order.push(Side::Lhs);
43 side_order.push(Side::Rhs);
47 assert!(matches!(side_order[..], [Side::Lhs, Side::Rhs]));
50 fn custom_compound() {
53 impl AddAssign<()> for Custom {
54 fn add_assign(&mut self, _: ()) {
55 // this block purposely left blank
59 let mut side_order = vec![];
60 let mut custom = Wrapper(Custom);
63 side_order.push(Side::Lhs);
66 side_order.push(Side::Rhs);
69 assert!(matches!(side_order[..], [Side::Lhs, Side::Rhs]));
74 generic_compound::<i32>();