authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-21 02:46:33-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-31 21:09:22-07:00
log06bb360dd296288db33844d682188e33116d7ab6
tree78b849386440654b93ba40ed44f82932ce592b95
parent2f992e1bb3ff0beb01bd7763a7937000a88f445e

astgen: respect a const local's type annotation


1 files changed, 10 insertions(+), 4 deletions(-)

src/astgen.zig+10-4
......@@ -693,10 +693,11 @@ fn varDecl(
693693 defer init_scope.instructions.deinit(mod.gpa);
694694
695695 var resolve_inferred_alloc: ?*zir.Inst = null;
696 var opt_type_inst: ?*zir.Inst = null;
696697 if (node.getTypeNode()) |type_node| {
697698 const type_inst = try typeExpr(mod, &init_scope.base, type_node);
698 const alloc = try addZIRUnOp(mod, &init_scope.base, name_src, .alloc, type_inst);
699 init_scope.rl_ptr = alloc;
699 opt_type_inst = type_inst;
700 init_scope.rl_ptr = try addZIRUnOp(mod, &init_scope.base, name_src, .alloc, type_inst);
700701 } else {
701702 const alloc = try addZIRNoOpT(mod, &init_scope.base, name_src, .alloc_inferred);
702703 resolve_inferred_alloc = &alloc.base;
......@@ -720,12 +721,17 @@ fn varDecl(
720721 parent_zir.appendAssumeCapacity(src_inst);
721722 }
722723 assert(parent_zir.items.len == expected_len);
724 const casted_init = if (opt_type_inst) |type_inst|
725 try addZIRBinOp(mod, scope, type_inst.src, .as, type_inst, init_inst)
726 else
727 init_inst;
728
723729 const sub_scope = try block_arena.create(Scope.LocalVal);
724730 sub_scope.* = .{
725731 .parent = scope,
726732 .gen_zir = scope.getGenZIR(),
727733 .name = ident_name,
728 .inst = init_inst,
734 .inst = casted_init,
729735 };
730736 return &sub_scope.base;
731737 }
......@@ -2866,7 +2872,7 @@ fn asRlPtr(
28662872 parent_zir.appendAssumeCapacity(src_inst);
28672873 }
28682874 assert(parent_zir.items.len == expected_len);
2869 const casted_result = try addZIRBinOp(mod, scope, result.src, .as, dest_type, result);
2875 const casted_result = try addZIRBinOp(mod, scope, dest_type.src, .as, dest_type, result);
28702876 return rvalue(mod, scope, rl, casted_result);
28712877 } else {
28722878 try parent_zir.appendSlice(mod.gpa, as_scope.instructions.items);