aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2021-01-21 02:46:33 -0700
committerAndrew Kelley <andrew@ziglang.org>2021-01-31 21:09:22 -0700
commit06bb360dd296288db33844d682188e33116d7ab6 (patch)
tree78b849386440654b93ba40ed44f82932ce592b95 /src
parent2f992e1bb3ff0beb01bd7763a7937000a88f445e (diff)
downloadzig-06bb360dd296288db33844d682188e33116d7ab6.tar.gz
zig-06bb360dd296288db33844d682188e33116d7ab6.zip
astgen: respect a const local's type annotation
Diffstat (limited to 'src')
-rw-r--r--src/astgen.zig14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/astgen.zig b/src/astgen.zig
index 296bec1f21..49f60aa6ba 100644
--- a/src/astgen.zig
+++ b/src/astgen.zig
@@ -693,10 +693,11 @@ fn varDecl(
defer init_scope.instructions.deinit(mod.gpa);
var resolve_inferred_alloc: ?*zir.Inst = null;
+ var opt_type_inst: ?*zir.Inst = null;
if (node.getTypeNode()) |type_node| {
const type_inst = try typeExpr(mod, &init_scope.base, type_node);
- const alloc = try addZIRUnOp(mod, &init_scope.base, name_src, .alloc, type_inst);
- init_scope.rl_ptr = alloc;
+ opt_type_inst = type_inst;
+ init_scope.rl_ptr = try addZIRUnOp(mod, &init_scope.base, name_src, .alloc, type_inst);
} else {
const alloc = try addZIRNoOpT(mod, &init_scope.base, name_src, .alloc_inferred);
resolve_inferred_alloc = &alloc.base;
@@ -720,12 +721,17 @@ fn varDecl(
parent_zir.appendAssumeCapacity(src_inst);
}
assert(parent_zir.items.len == expected_len);
+ const casted_init = if (opt_type_inst) |type_inst|
+ try addZIRBinOp(mod, scope, type_inst.src, .as, type_inst, init_inst)
+ else
+ init_inst;
+
const sub_scope = try block_arena.create(Scope.LocalVal);
sub_scope.* = .{
.parent = scope,
.gen_zir = scope.getGenZIR(),
.name = ident_name,
- .inst = init_inst,
+ .inst = casted_init,
};
return &sub_scope.base;
}
@@ -2866,7 +2872,7 @@ fn asRlPtr(
parent_zir.appendAssumeCapacity(src_inst);
}
assert(parent_zir.items.len == expected_len);
- const casted_result = try addZIRBinOp(mod, scope, result.src, .as, dest_type, result);
+ const casted_result = try addZIRBinOp(mod, scope, dest_type.src, .as, dest_type, result);
return rvalue(mod, scope, rl, casted_result);
} else {
try parent_zir.appendSlice(mod.gpa, as_scope.instructions.items);