summaryrefslogtreecommitdiffstats
path: root/tests/ui/expr/compound-assignment
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:03 +0000
commit64d98f8ee037282c35007b64c2649055c56af1db (patch)
tree5492bcf97fce41ee1c0b1cc2add283f3e66cdab0 /tests/ui/expr/compound-assignment
parentAdding debian version 1.67.1+dfsg1-1. (diff)
downloadrustc-64d98f8ee037282c35007b64c2649055c56af1db.tar.xz
rustc-64d98f8ee037282c35007b64c2649055c56af1db.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/expr/compound-assignment')
-rw-r--r--tests/ui/expr/compound-assignment/eval-order.rs76
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/ui/expr/compound-assignment/eval-order.rs b/tests/ui/expr/compound-assignment/eval-order.rs
new file mode 100644
index 000000000..658adae19
--- /dev/null
+++ b/tests/ui/expr/compound-assignment/eval-order.rs
@@ -0,0 +1,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();
+}