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(...@@ -693,10 +693,11 @@ fn varDecl(
693 defer init_scope.instructions.deinit(mod.gpa);693 defer init_scope.instructions.deinit(mod.gpa);
694694
695 var resolve_inferred_alloc: ?*zir.Inst = null;695 var resolve_inferred_alloc: ?*zir.Inst = null;
696 var opt_type_inst: ?*zir.Inst = null;
696 if (node.getTypeNode()) |type_node| {697 if (node.getTypeNode()) |type_node| {
697 const type_inst = try typeExpr(mod, &init_scope.base, type_node);698 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 opt_type_inst = type_inst;
699 init_scope.rl_ptr = alloc;700 init_scope.rl_ptr = try addZIRUnOp(mod, &init_scope.base, name_src, .alloc, type_inst);
700 } else {701 } else {
701 const alloc = try addZIRNoOpT(mod, &init_scope.base, name_src, .alloc_inferred);702 const alloc = try addZIRNoOpT(mod, &init_scope.base, name_src, .alloc_inferred);
702 resolve_inferred_alloc = &alloc.base;703 resolve_inferred_alloc = &alloc.base;
...@@ -720,12 +721,17 @@ fn varDecl(...@@ -720,12 +721,17 @@ fn varDecl(
720 parent_zir.appendAssumeCapacity(src_inst);721 parent_zir.appendAssumeCapacity(src_inst);
721 }722 }
722 assert(parent_zir.items.len == expected_len);723 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
723 const sub_scope = try block_arena.create(Scope.LocalVal);729 const sub_scope = try block_arena.create(Scope.LocalVal);
724 sub_scope.* = .{730 sub_scope.* = .{
725 .parent = scope,731 .parent = scope,
726 .gen_zir = scope.getGenZIR(),732 .gen_zir = scope.getGenZIR(),
727 .name = ident_name,733 .name = ident_name,
728 .inst = init_inst,734 .inst = casted_init,
729 };735 };
730 return &sub_scope.base;736 return &sub_scope.base;
731 }737 }
...@@ -2866,7 +2872,7 @@ fn asRlPtr(...@@ -2866,7 +2872,7 @@ fn asRlPtr(
2866 parent_zir.appendAssumeCapacity(src_inst);2872 parent_zir.appendAssumeCapacity(src_inst);
2867 }2873 }
2868 assert(parent_zir.items.len == expected_len);2874 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);
2870 return rvalue(mod, scope, rl, casted_result);2876 return rvalue(mod, scope, rl, casted_result);
2871 } else {2877 } else {
2872 try parent_zir.appendSlice(mod.gpa, as_scope.instructions.items);2878 try parent_zir.appendSlice(mod.gpa, as_scope.instructions.items);