aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/struct.zig
diff options
context:
space:
mode:
authorJacob Young <jacobly0@users.noreply.github.com>2023-08-10 03:25:35 -0400
committerAndrew Kelley <andrew@ziglang.org>2023-08-11 11:01:47 -0700
commit8b9161179d08a3f1dc22ea61e6165a0c638bfae3 (patch)
tree9766bd828e3db022e50808c89c250c3e01b6609b /test/behavior/struct.zig
parentb835fd90cef1447904d3b009c9662ba4c0ea77d4 (diff)
downloadzig-8b9161179d08a3f1dc22ea61e6165a0c638bfae3.tar.gz
zig-8b9161179d08a3f1dc22ea61e6165a0c638bfae3.zip
Sema: avoid deleting runtime side-effects in comptime initializers
Closes #16744
Diffstat (limited to 'test/behavior/struct.zig')
-rw-r--r--test/behavior/struct.zig25
1 files changed, 25 insertions, 0 deletions
diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig
index 1c08e7b5fa..636ac8c2a5 100644
--- a/test/behavior/struct.zig
+++ b/test/behavior/struct.zig
@@ -1738,3 +1738,28 @@ test "struct init with no result pointer sets field result types" {
try expect(y == x);
}
+
+test "runtime side-effects in comptime-known struct init" {
+ var side_effects: u4 = 0;
+ const S = struct { a: u4, b: u4, c: u4, d: u4 };
+ const init = S{
+ .d = blk: {
+ side_effects += 8;
+ break :blk 8;
+ },
+ .c = blk: {
+ side_effects += 4;
+ break :blk 4;
+ },
+ .b = blk: {
+ side_effects += 2;
+ break :blk 2;
+ },
+ .a = blk: {
+ side_effects += 1;
+ break :blk 1;
+ },
+ };
+ try expectEqual(S{ .a = 1, .b = 2, .c = 4, .d = 8 }, init);
+ try expectEqual(@as(u4, std.math.maxInt(u4)), side_effects);
+}