authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-02 13:54:58-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-02 13:54:58-04:00
loga5cb0f77d11bdcc504fe3e6afa928c88de821518
treea110d7eead2bb71bca56154114c5ff9bd4dfaa5e
parent6cb4cac5cd1c68f41c62a3c23b09513988337c8d
signaturelock-open Commit is signed but in an unrecognized format.

assignment participates in result location

fix one regression with optionals but there are more

2 files changed, 25 insertions(+), 6 deletions(-)

src/ir.cpp+15-6
...@@ -4001,12 +4001,20 @@ static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, Scope *scope, AstNode *no...@@ -4001,12 +4001,20 @@ static IrInstruction *ir_gen_bin_op_id(IrBuilder *irb, Scope *scope, AstNode *no
40014001
4002static IrInstruction *ir_gen_assign(IrBuilder *irb, Scope *scope, AstNode *node) {4002static IrInstruction *ir_gen_assign(IrBuilder *irb, Scope *scope, AstNode *node) {
4003 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, scope, LValPtr, nullptr);4003 IrInstruction *lvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op1, scope, LValPtr, nullptr);
4004 IrInstruction *rvalue = ir_gen_node(irb, node->data.bin_op_expr.op2, scope);4004 if (lvalue == irb->codegen->invalid_instruction)
4005 return irb->codegen->invalid_instruction;
40054006
4006 if (lvalue == irb->codegen->invalid_instruction || rvalue == irb->codegen->invalid_instruction)4007 ResultLocInstruction *result_loc_inst = allocate<ResultLocInstruction>(1);
4008 result_loc_inst->base.id = ResultLocIdInstruction;
4009 result_loc_inst->base.source_instruction = lvalue;
4010 ir_ref_instruction(lvalue, irb->current_basic_block);
4011 ir_build_reset_result(irb, scope, node, &result_loc_inst->base);
4012
4013 IrInstruction *rvalue = ir_gen_node_extra(irb, node->data.bin_op_expr.op2, scope, LValNone,
4014 &result_loc_inst->base);
4015 if (rvalue == irb->codegen->invalid_instruction)
4007 return irb->codegen->invalid_instruction;4016 return irb->codegen->invalid_instruction;
40084017
4009 ir_build_store_ptr(irb, scope, node, lvalue, rvalue);
4010 return ir_build_const_void(irb, scope, node);4018 return ir_build_const_void(irb, scope, node);
4011}4019}
40124020
...@@ -17477,6 +17485,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct...@@ -17477,6 +17485,7 @@ static IrInstruction *ir_analyze_instruction_elem_ptr(IrAnalyze *ira, IrInstruct
17477 return result;17485 return result;
17478 } else if (is_slice(array_type)) {17486 } else if (is_slice(array_type)) {
17479 ConstExprValue *ptr_field = &array_ptr_val->data.x_struct.fields[slice_ptr_index];17487 ConstExprValue *ptr_field = &array_ptr_val->data.x_struct.fields[slice_ptr_index];
17488 ir_assert(ptr_field != nullptr, &elem_ptr_instruction->base);
17480 if (ptr_field->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {17489 if (ptr_field->data.x_ptr.special == ConstPtrSpecialHardCodedAddr) {
17481 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,17490 IrInstruction *result = ir_build_elem_ptr(&ira->new_irb, elem_ptr_instruction->base.scope,
17482 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index, false,17491 elem_ptr_instruction->base.source_node, array_ptr, casted_elem_index, false,
...@@ -17663,7 +17672,7 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction...@@ -17663,7 +17672,7 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction
17663 return ira->codegen->invalid_instruction;17672 return ira->codegen->invalid_instruction;
17664 if (type_is_invalid(struct_val->type))17673 if (type_is_invalid(struct_val->type))
17665 return ira->codegen->invalid_instruction;17674 return ira->codegen->invalid_instruction;
17666 if (struct_val->special == ConstValSpecialUndef && initializing) {17675 if (initializing && struct_val->special == ConstValSpecialUndef) {
17667 struct_val->data.x_struct.fields = create_const_vals(struct_type->data.structure.src_field_count);17676 struct_val->data.x_struct.fields = create_const_vals(struct_type->data.structure.src_field_count);
17668 struct_val->special = ConstValSpecialStatic;17677 struct_val->special = ConstValSpecialStatic;
17669 for (size_t i = 0; i < struct_type->data.structure.src_field_count; i += 1) {17678 for (size_t i = 0; i < struct_type->data.structure.src_field_count; i += 1) {
...@@ -18764,7 +18773,7 @@ static IrInstruction *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInstr...@@ -18764,7 +18773,7 @@ static IrInstruction *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInstr
18764 if (optional_val == nullptr)18773 if (optional_val == nullptr)
18765 return ira->codegen->invalid_instruction;18774 return ira->codegen->invalid_instruction;
1876618775
18767 if (initializing && optional_val->special == ConstValSpecialUndef) {18776 if (initializing) {
18768 switch (type_has_one_possible_value(ira->codegen, child_type)) {18777 switch (type_has_one_possible_value(ira->codegen, child_type)) {
18769 case OnePossibleValueInvalid:18778 case OnePossibleValueInvalid:
18770 return ira->codegen->invalid_instruction;18779 return ira->codegen->invalid_instruction;
...@@ -23260,7 +23269,7 @@ static IrInstruction *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInstruct...@@ -23260,7 +23269,7 @@ static IrInstruction *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInstruct
23260 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);23269 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
23261 if (err_union_val == nullptr)23270 if (err_union_val == nullptr)
23262 return ira->codegen->invalid_instruction;23271 return ira->codegen->invalid_instruction;
23263 if (err_union_val->special == ConstValSpecialUndef && initializing) {23272 if (initializing && err_union_val->special == ConstValSpecialUndef) {
23264 ConstExprValue *vals = create_const_vals(2);23273 ConstExprValue *vals = create_const_vals(2);
23265 ConstExprValue *err_set_val = &vals[0];23274 ConstExprValue *err_set_val = &vals[0];
23266 ConstExprValue *payload_val = &vals[1];23275 ConstExprValue *payload_val = &vals[1];
test/stage1/behavior/eval.zig+10
...@@ -1,5 +1,6 @@...@@ -1,5 +1,6 @@
1const std = @import("std");1const std = @import("std");
2const expect = std.testing.expect;2const expect = std.testing.expect;
3const expectEqual = std.testing.expectEqual;
3const builtin = @import("builtin");4const builtin = @import("builtin");
45
5test "compile time recursion" {6test "compile time recursion" {
...@@ -794,3 +795,12 @@ test "no undeclared identifier error in unanalyzed branches" {...@@ -794,3 +795,12 @@ test "no undeclared identifier error in unanalyzed branches" {
794 lol_this_doesnt_exist = nonsense;795 lol_this_doesnt_exist = nonsense;
795 }796 }
796}797}
798
799test "comptime assign int to optional int" {
800 comptime {
801 var x: ?i32 = null;
802 x = 2;
803 x.? *= 10;
804 expectEqual(20, x.?);
805 }
806}