aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorarbrk1 <arbrk1@users.noreply.github.com>2024-01-02 13:08:26 +0300
committerGitHub <noreply@github.com>2024-01-02 12:08:26 +0200
commit024540de15160bc9ced67d0b10c3f4eddf4a7846 (patch)
tree5ccdd699de1ed46450b08c780494ac57727aa831 /test
parent25a556107cbd10cbddf530e633f70c688498d4e3 (diff)
downloadzig-024540de15160bc9ced67d0b10c3f4eddf4a7846.tar.gz
zig-024540de15160bc9ced67d0b10c3f4eddf4a7846.zip
Liveness: fix branch operands becoming aliased
Diffstat (limited to 'test')
-rw-r--r--test/behavior/if.zig33
1 files changed, 33 insertions, 0 deletions
diff --git a/test/behavior/if.zig b/test/behavior/if.zig
index c4dfd04f71..bef02fd1ce 100644
--- a/test/behavior/if.zig
+++ b/test/behavior/if.zig
@@ -167,3 +167,36 @@ test "if-@as-if chain" {
try expect(num_frames == 4);
}
+
+fn returnTrue() bool {
+ return true;
+}
+
+test "if value shouldn't be load-elided if used later (structs)" {
+ const Foo = struct { x: i32 };
+
+ var a = Foo{ .x = 1 };
+ var b = Foo{ .x = 1 };
+
+ const c = if (@call(.never_inline, returnTrue, .{})) a else b;
+ // The second variable is superfluous with the current
+ // state of codegen optimizations, but in future
+ // "if (smthg) a else a" may be optimized simply into "a".
+
+ a.x = 2;
+ b.x = 3;
+
+ try std.testing.expectEqual(c.x, 1);
+}
+
+test "if value shouldn't be load-elided if used later (optionals)" {
+ var a: ?i32 = 1;
+ var b: ?i32 = 1;
+
+ const c = if (@call(.never_inline, returnTrue, .{})) a else b;
+
+ a = 2;
+ b = 3;
+
+ try std.testing.expectEqual(c, 1);
+}