Commit 53500a5768

kcbanner <kcbanner@gmail.com>
2023-11-11 22:48:11
sema: fixup underflows during struct / ptr array init when using -fstrip
1 parent 70d8baa
Changed files (4)
src
test
standalone
strip_struct_init
src/Sema.zig
@@ -4814,7 +4814,7 @@ fn validateStructInit(
 
             // Possible performance enhancement: save the `block_index` between iterations
             // of the for loop.
-            var block_index = block.instructions.items.len - 1;
+            var block_index = block.instructions.items.len -| 1;
             while (block_index > 0) : (block_index -= 1) {
                 const store_inst = block.instructions.items[block_index];
                 if (Air.indexToRef(store_inst) == field_ptr_ref) {
@@ -5070,7 +5070,7 @@ fn zirValidatePtrArrayInit(
 
         // Possible performance enhancement: save the `block_index` between iterations
         // of the for loop.
-        var block_index = block.instructions.items.len - 1;
+        var block_index = block.instructions.items.len -| 1;
         while (block_index > 0) : (block_index -= 1) {
             const store_inst = block.instructions.items[block_index];
             if (Air.indexToRef(store_inst) == elem_ptr_ref) {
test/standalone/strip_struct_init/build.zig
@@ -0,0 +1,16 @@
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+    const test_step = b.step("test", "Test it");
+    b.default_step = test_step;
+
+    const optimize: std.builtin.OptimizeMode = .Debug;
+
+    const main = b.addTest(.{
+        .root_source_file = .{ .path = "main.zig" },
+        .optimize = optimize,
+    });
+    main.strip = true;
+
+    test_step.dependOn(&b.addRunArtifact(main).step);
+}
test/standalone/strip_struct_init/main.zig
@@ -0,0 +1,23 @@
+fn Func(comptime Type: type) type {
+    return struct { value: Type };
+}
+
+inline fn func(value: anytype) Func(@TypeOf(value)) {
+    return .{ .value = value };
+}
+
+test {
+    _ = func(type);
+}
+
+test {
+    const S = struct { field: u32 };
+    comptime var arr: [1]S = undefined;
+    arr[0] = .{ .field = 0 };
+}
+
+test {
+    const S = struct { u32 };
+    comptime var arr: [1]S = undefined;
+    arr[0] = .{0};
+}
test/standalone.zig
@@ -234,6 +234,10 @@ pub const build_cases = [_]BuildCase{
         .build_root = "test/standalone/strip_empty_loop",
         .import = @import("standalone/strip_empty_loop/build.zig"),
     },
+    .{
+        .build_root = "test/standalone/strip_struct_init",
+        .import = @import("standalone/strip_struct_init/build.zig"),
+    },
     .{
         .build_root = "test/standalone/cmakedefine",
         .import = @import("standalone/cmakedefine/build.zig"),