aboutsummaryrefslogtreecommitdiff
path: root/src/AstGen.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-03-24 17:45:34 -0700
committerAndrew Kelley <andrew@ziglang.org>2022-03-24 17:47:39 -0700
commit5c68afef94b0b80823e033bd6965fcda74e19ebe (patch)
treee66fb82c14c731edce688b50150ccc38b585b729 /src/AstGen.zig
parent9a1d5001d4bf1f28bd0f23e8b936d677e0e5aac8 (diff)
downloadzig-5c68afef94b0b80823e033bd6965fcda74e19ebe.tar.gz
zig-5c68afef94b0b80823e033bd6965fcda74e19ebe.zip
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.
Diffstat (limited to 'src/AstGen.zig')
-rw-r--r--src/AstGen.zig9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/AstGen.zig b/src/AstGen.zig
index 4221f0cd14..aba4610c5d 100644
--- a/src/AstGen.zig
+++ b/src/AstGen.zig
@@ -2671,6 +2671,9 @@ fn varDecl(
return &sub_scope.base;
}
+ const is_comptime = gz.force_comptime or
+ tree.nodes.items(.tag)[var_decl.ast.init_node] == .@"comptime";
+
// Detect whether the initialization expression actually uses the
// result location pointer.
var init_scope = gz.makeSubBlock(scope);
@@ -2692,7 +2695,7 @@ fn varDecl(
.type_inst = type_inst,
.align_inst = align_inst,
.is_const = true,
- .is_comptime = gz.force_comptime,
+ .is_comptime = is_comptime,
});
init_scope.instructions_top = gz.instructions.items.len;
}
@@ -2700,7 +2703,7 @@ fn varDecl(
} else {
const alloc = if (align_inst == .none) alloc: {
init_scope.instructions_top = gz.instructions.items.len;
- const tag: Zir.Inst.Tag = if (gz.force_comptime)
+ const tag: Zir.Inst.Tag = if (is_comptime)
.alloc_inferred_comptime
else
.alloc_inferred;
@@ -2711,7 +2714,7 @@ fn varDecl(
.type_inst = .none,
.align_inst = align_inst,
.is_const = true,
- .is_comptime = gz.force_comptime,
+ .is_comptime = is_comptime,
});
init_scope.instructions_top = gz.instructions.items.len;
break :alloc ref;