authorgravatar for mitchell.hashimoto@gmail.comMitchell Hashimoto <mitchell.hashimoto@gmail.com> 2022-03-15 13:29:54-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-15 19:55:21-04:00
log7d0b6956c0935807f0e5862c45da857e8e065c6b
treed2b3df54d4bf185e326d4b4363958b5ed210698c
parent2c434cddd6624cfd5c71f081b9445936d25c7703

stage2: resolve type fully when resolving inferred allocs

We must resolve the type fully so that pointer children (i.e. slices) are resolved. Additionally, we must resolve even if we can know the value at comptime because the `alloc_inferred` ZIR always produces a constant in the AIR. Fixes #11181

3 files changed, 38 insertions(+), 1 deletions(-)

src/Sema.zig+6-1
......@@ -2672,11 +2672,16 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
26722672 // ZIR handles it later, so we can just use the ty ref here.
26732673 air_datas[ptr_inst].ty_pl.ty = air_datas[bitcast_inst].ty_op.ty;
26742674
2675 // Unless the block is comptime, `alloc_inferred` always produces
2676 // a runtime constant. The final inferred type needs to be
2677 // fully resolved so it can be lowered in codegen.
2678 try sema.resolveTypeFully(block, ty_src, final_elem_ty);
2679
26752680 return;
26762681 }
26772682
26782683 try sema.requireRuntimeBlock(block, src);
2679 try sema.resolveTypeLayout(block, ty_src, final_elem_ty);
2684 try sema.resolveTypeFully(block, ty_src, final_elem_ty);
26802685
26812686 // Change it to a normal alloc.
26822687 sema.air_instructions.set(ptr_inst, .{
test/behavior.zig+1
......@@ -66,6 +66,7 @@ test {
6666 _ = @import("behavior/bugs/11046.zig");
6767 _ = @import("behavior/bugs/11139.zig");
6868 _ = @import("behavior/bugs/11165.zig");
69 _ = @import("behavior/bugs/11181.zig");
6970 _ = @import("behavior/call.zig");
7071 _ = @import("behavior/cast.zig");
7172 _ = @import("behavior/comptime_memory.zig");
test/behavior/bugs/11181.zig created+31
......@@ -0,0 +1,31 @@
1const builtin = @import("builtin");
2
3test "const inferred array of slices" {
4 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
5 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
6 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
7
8 const T = struct { v: bool };
9
10 const decls = [_][]const T{
11 &[_]T{
12 .{ .v = false },
13 },
14 };
15 _ = decls;
16}
17
18test "var inferred array of slices" {
19 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
20 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
21 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
22
23 const T = struct { v: bool };
24
25 var decls = [_][]const T{
26 &[_]T{
27 .{ .v = false },
28 },
29 };
30 _ = decls;
31}