aboutsummaryrefslogtreecommitdiff
path: root/src/AstGen.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-02-08 20:03:17 -0700
committerAndrew Kelley <andrew@ziglang.org>2022-02-08 20:03:17 -0700
commit7c1061784b8b126633cfe84f46280f7bf72beffc (patch)
treeeb99e5eb52d22b9f7d17cfd62dcfb19e3c26296a /src/AstGen.zig
parent210ee1067b06c14692432b7887077003e52b2137 (diff)
downloadzig-7c1061784b8b126633cfe84f46280f7bf72beffc.tar.gz
zig-7c1061784b8b126633cfe84f46280f7bf72beffc.zip
stage2: fix inferred comptime constant locals
`const` declarations inside comptime blocks were not getting properly evaluated at compile-time. To accomplish this there is a new ZIR instruction, `alloc_inferred_comptime`. Actually we already had one named that, but it got renamed to `alloc_inferred_comptime_mut` to match the naming convention with the other similar instructions.
Diffstat (limited to 'src/AstGen.zig')
-rw-r--r--src/AstGen.zig25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/AstGen.zig b/src/AstGen.zig
index 5f6d05b7f5..9bc10f25e8 100644
--- a/src/AstGen.zig
+++ b/src/AstGen.zig
@@ -2084,10 +2084,11 @@ fn unusedResultExpr(gz: *GenZir, scope: *Scope, statement: Ast.Node.Index) Inner
.param_anytype_comptime,
.alloc,
.alloc_mut,
- .alloc_comptime,
+ .alloc_comptime_mut,
.alloc_inferred,
.alloc_inferred_mut,
.alloc_inferred_comptime,
+ .alloc_inferred_comptime_mut,
.array_cat,
.array_mul,
.array_type,
@@ -2613,7 +2614,7 @@ fn varDecl(
.type_inst = type_inst,
.align_inst = align_inst,
.is_const = true,
- .is_comptime = false,
+ .is_comptime = gz.force_comptime,
});
init_scope.instructions_top = gz.instructions.items.len;
}
@@ -2621,14 +2622,18 @@ fn varDecl(
} else {
const alloc = if (align_inst == .none) alloc: {
init_scope.instructions_top = gz.instructions.items.len;
- break :alloc try init_scope.addNode(.alloc_inferred, node);
+ const tag: Zir.Inst.Tag = if (gz.force_comptime)
+ .alloc_inferred_comptime
+ else
+ .alloc_inferred;
+ break :alloc try init_scope.addNode(tag, node);
} else alloc: {
const ref = try gz.addAllocExtended(.{
.node = node,
.type_inst = .none,
.align_inst = align_inst,
.is_const = true,
- .is_comptime = false,
+ .is_comptime = gz.force_comptime,
});
init_scope.instructions_top = gz.instructions.items.len;
break :alloc ref;
@@ -2716,7 +2721,10 @@ fn varDecl(
const type_inst = try typeExpr(gz, scope, var_decl.ast.type_node);
const alloc = alloc: {
if (align_inst == .none) {
- const tag: Zir.Inst.Tag = if (is_comptime) .alloc_comptime else .alloc_mut;
+ const tag: Zir.Inst.Tag = if (is_comptime)
+ .alloc_comptime_mut
+ else
+ .alloc_mut;
break :alloc try gz.addUnNode(tag, type_inst, node);
} else {
break :alloc try gz.addAllocExtended(.{
@@ -2732,7 +2740,10 @@ fn varDecl(
} else a: {
const alloc = alloc: {
if (align_inst == .none) {
- const tag: Zir.Inst.Tag = if (is_comptime) .alloc_inferred_comptime else .alloc_inferred_mut;
+ const tag: Zir.Inst.Tag = if (is_comptime)
+ .alloc_inferred_comptime_mut
+ else
+ .alloc_inferred_mut;
break :alloc try gz.addNode(tag, node);
} else {
break :alloc try gz.addAllocExtended(.{
@@ -5441,7 +5452,7 @@ fn forExpr(
const len = try parent_gz.addUnNode(.indexable_ptr_len, array_ptr, for_full.ast.cond_expr);
const index_ptr = blk: {
- const alloc_tag: Zir.Inst.Tag = if (is_inline) .alloc_comptime else .alloc;
+ const alloc_tag: Zir.Inst.Tag = if (is_inline) .alloc_comptime_mut else .alloc;
const index_ptr = try parent_gz.addUnNode(alloc_tag, .usize_type, node);
// initialize to zero
_ = try parent_gz.addBin(.store, index_ptr, .zero_usize);