-
Notifications
You must be signed in to change notification settings - Fork 12.7k
/
eval-order.rs
76 lines (60 loc) · 1.7 KB
/
eval-order.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Test evaluation order of operands of the compound assignment operators
// run-pass
use std::ops::AddAssign;
enum Side {
Lhs,
Rhs,
}
// In the following tests, we place our value into a wrapper type so that we
// can do an element access as the outer place expression. If we just had the
// block expression, it'd be a value expression and not compile.
struct Wrapper<T>(T);
// Evaluation order for `a op= b` where typeof(a) and typeof(b) are primitives
// is first `b` then `a`.
fn primitive_compound() {
let mut side_order = vec![];
let mut int = Wrapper(0);
{
side_order.push(Side::Lhs);
int
}.0 += {
side_order.push(Side::Rhs);
0
};
assert!(matches!(side_order[..], [Side::Rhs, Side::Lhs]));
}
// Evaluation order for `a op=b` otherwise is first `a` then `b`.
fn generic_compound<T: AddAssign<T> + Default>() {
let mut side_order = vec![];
let mut add_assignable: Wrapper<T> = Wrapper(Default::default());
{
side_order.push(Side::Lhs);
add_assignable
}.0 += {
side_order.push(Side::Rhs);
Default::default()
};
assert!(matches!(side_order[..], [Side::Lhs, Side::Rhs]));
}
fn custom_compound() {
struct Custom;
impl AddAssign<()> for Custom {
fn add_assign(&mut self, _: ()) {
// this block purposely left blank
}
}
let mut side_order = vec![];
let mut custom = Wrapper(Custom);
{
side_order.push(Side::Lhs);
custom
}.0 += {
side_order.push(Side::Rhs);
};
assert!(matches!(side_order[..], [Side::Lhs, Side::Rhs]));
}
fn main() {
primitive_compound();
generic_compound::<i32>();
custom_compound();
}