authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-24 17:45:34-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-24 17:47:39-07:00
log5c68afef94b0b80823e033bd6965fcda74e19ebe
treee66fb82c14c731edce688b50150ccc38b585b729
parent9a1d5001d4bf1f28bd0f23e8b936d677e0e5aac8

AstGen: fix const locals with comptime initializations

`const foo = comptime ...` generated invalid ZIR when the initialization expression contained an array literal because the validate_array_init_comptime instruction assumed that the corresponding alloc instruction was comptime. The solution is to look slightly ahead and notice that the initialization expression would be comptime-known and affect the alloc instruction tag accordingly.

2 files changed, 25 insertions(+), 3 deletions(-)

src/AstGen.zig+6-3
......@@ -2671,6 +2671,9 @@ fn varDecl(
26712671 return &sub_scope.base;
26722672 }
26732673
2674 const is_comptime = gz.force_comptime or
2675 tree.nodes.items(.tag)[var_decl.ast.init_node] == .@"comptime";
2676
26742677 // Detect whether the initialization expression actually uses the
26752678 // result location pointer.
26762679 var init_scope = gz.makeSubBlock(scope);
......@@ -2692,7 +2695,7 @@ fn varDecl(
26922695 .type_inst = type_inst,
26932696 .align_inst = align_inst,
26942697 .is_const = true,
2695 .is_comptime = gz.force_comptime,
2698 .is_comptime = is_comptime,
26962699 });
26972700 init_scope.instructions_top = gz.instructions.items.len;
26982701 }
......@@ -2700,7 +2703,7 @@ fn varDecl(
27002703 } else {
27012704 const alloc = if (align_inst == .none) alloc: {
27022705 init_scope.instructions_top = gz.instructions.items.len;
2703 const tag: Zir.Inst.Tag = if (gz.force_comptime)
2706 const tag: Zir.Inst.Tag = if (is_comptime)
27042707 .alloc_inferred_comptime
27052708 else
27062709 .alloc_inferred;
......@@ -2711,7 +2714,7 @@ fn varDecl(
27112714 .type_inst = .none,
27122715 .align_inst = align_inst,
27132716 .is_const = true,
2714 .is_comptime = gz.force_comptime,
2717 .is_comptime = is_comptime,
27152718 });
27162719 init_scope.instructions_top = gz.instructions.items.len;
27172720 break :alloc ref;
test/behavior/eval.zig+19
......@@ -859,3 +859,22 @@ test "debug variable type resolved through indirect zero-bit types" {
859859 const slice: []const T = &[_]T{};
860860 _ = slice;
861861}
862
863test "const local with comptime init through array init" {
864 const E1 = enum {
865 A,
866 fn a() void {}
867 };
868
869 const S = struct {
870 fn declarations(comptime T: type) []const std.builtin.Type.Declaration {
871 return @typeInfo(T).Enum.decls;
872 }
873 };
874
875 const decls = comptime [_][]const std.builtin.Type.Declaration{
876 S.declarations(E1),
877 };
878
879 try comptime expect(decls[0][0].name[0] == 'a');
880}