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

This commit is contained in:
kcbanner 2023-11-11 16:48:11 -05:00 committed by Andrew Kelley
parent 70d8baaec1
commit 53500a5768
4 changed files with 45 additions and 2 deletions

View File

@ -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) {

View File

@ -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"),

View File

@ -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);
}

View File

@ -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};
}