authorgravatar for kcbanner@gmail.comCasey Banner <kcbanner@gmail.com> 2023-11-11 16:48:11-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-11-12 18:22:57-05:00
log53500a57684665e08a2e18e7a736aacfa6548062
tree4504dfb16ce7655307c72adce1f8a97704f8c331
parent70d8baaec11ca370b73fce72d7f3dfce2277455b

sema: fixup underflows during struct / ptr array init when using -fstrip


4 files changed, 45 insertions(+), 2 deletions(-)

src/Sema.zig+2-2
...@@ -4814,7 +4814,7 @@ fn validateStructInit(...@@ -4814,7 +4814,7 @@ fn validateStructInit(
48144814
4815 // Possible performance enhancement: save the `block_index` between iterations4815 // Possible performance enhancement: save the `block_index` between iterations
4816 // of the for loop.4816 // of the for loop.
4817 var block_index = block.instructions.items.len - 1;4817 var block_index = block.instructions.items.len -| 1;
4818 while (block_index > 0) : (block_index -= 1) {4818 while (block_index > 0) : (block_index -= 1) {
4819 const store_inst = block.instructions.items[block_index];4819 const store_inst = block.instructions.items[block_index];
4820 if (Air.indexToRef(store_inst) == field_ptr_ref) {4820 if (Air.indexToRef(store_inst) == field_ptr_ref) {
...@@ -5070,7 +5070,7 @@ fn zirValidatePtrArrayInit(...@@ -5070,7 +5070,7 @@ fn zirValidatePtrArrayInit(
50705070
5071 // Possible performance enhancement: save the `block_index` between iterations5071 // Possible performance enhancement: save the `block_index` between iterations
5072 // of the for loop.5072 // of the for loop.
5073 var block_index = block.instructions.items.len - 1;5073 var block_index = block.instructions.items.len -| 1;
5074 while (block_index > 0) : (block_index -= 1) {5074 while (block_index > 0) : (block_index -= 1) {
5075 const store_inst = block.instructions.items[block_index];5075 const store_inst = block.instructions.items[block_index];
5076 if (Air.indexToRef(store_inst) == elem_ptr_ref) {5076 if (Air.indexToRef(store_inst) == elem_ptr_ref) {
test/standalone.zig+4
...@@ -234,6 +234,10 @@ pub const build_cases = [_]BuildCase{...@@ -234,6 +234,10 @@ pub const build_cases = [_]BuildCase{
234 .build_root = "test/standalone/strip_empty_loop",234 .build_root = "test/standalone/strip_empty_loop",
235 .import = @import("standalone/strip_empty_loop/build.zig"),235 .import = @import("standalone/strip_empty_loop/build.zig"),
236 },236 },
237 .{
238 .build_root = "test/standalone/strip_struct_init",
239 .import = @import("standalone/strip_struct_init/build.zig"),
240 },
237 .{241 .{
238 .build_root = "test/standalone/cmakedefine",242 .build_root = "test/standalone/cmakedefine",
239 .import = @import("standalone/cmakedefine/build.zig"),243 .import = @import("standalone/cmakedefine/build.zig"),
test/standalone/strip_struct_init/build.zig created+16
...@@ -0,0 +1,16 @@
1const std = @import("std");
2
3pub fn build(b: *std.Build) void {
4 const test_step = b.step("test", "Test it");
5 b.default_step = test_step;
6
7 const optimize: std.builtin.OptimizeMode = .Debug;
8
9 const main = b.addTest(.{
10 .root_source_file = .{ .path = "main.zig" },
11 .optimize = optimize,
12 });
13 main.strip = true;
14
15 test_step.dependOn(&b.addRunArtifact(main).step);
16}
test/standalone/strip_struct_init/main.zig created+23
...@@ -0,0 +1,23 @@
1fn Func(comptime Type: type) type {
2 return struct { value: Type };
3}
4
5inline fn func(value: anytype) Func(@TypeOf(value)) {
6 return .{ .value = value };
7}
8
9test {
10 _ = func(type);
11}
12
13test {
14 const S = struct { field: u32 };
15 comptime var arr: [1]S = undefined;
16 arr[0] = .{ .field = 0 };
17}
18
19test {
20 const S = struct { u32 };
21 comptime var arr: [1]S = undefined;
22 arr[0] = .{0};
23}