authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-11 13:27:01-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-11 13:27:01-04:00
logfc8d8812404e79716e15fd301058032485e8bb64
tree5e868ca349bb395525d727fa6347eee58a24f5a2
parent06f307ff77adac0451b10643a033714c1d309010
signaturelock-open Commit is signed but in an unrecognized format.

fix struct and array init when result casted to anyerror!?T


4 files changed, 44 insertions(+), 28 deletions(-)

BRANCH_TODO-1
......@@ -1,7 +1,6 @@
11Scratch pad for stuff to do before merging master
22=================================================
33
4struct & array init when the result is casted to anyerror!T
54struct & array init when the result is casted to anyerror!?T
65
76uncomment all the behavior tests
src/all_types.hpp+5-5
......@@ -2343,10 +2343,6 @@ enum IrInstructionId {
23432343};
23442344
23452345struct IrInstruction {
2346 IrInstructionId id;
2347 // true if this instruction was generated by zig and not from user code
2348 bool is_gen;
2349
23502346 Scope *scope;
23512347 AstNode *source_node;
23522348 ConstExprValue value;
......@@ -2360,6 +2356,9 @@ struct IrInstruction {
23602356 // with this child field.
23612357 IrInstruction *child;
23622358 IrBasicBlock *owner_bb;
2359 IrInstructionId id;
2360 // true if this instruction was generated by zig and not from user code
2361 bool is_gen;
23632362};
23642363
23652364struct IrInstructionDeclVarSrc {
......@@ -3102,8 +3101,9 @@ struct IrInstructionUnwrapErrCode {
31023101struct IrInstructionUnwrapErrPayload {
31033102 IrInstruction base;
31043103
3105 IrInstruction *value;
31063104 bool safety_check_on;
3105 bool initializing;
3106 IrInstruction *value;
31073107};
31083108
31093109struct IrInstructionOptionalWrap {
src/codegen.cpp+6-1
......@@ -4908,7 +4908,7 @@ static LLVMValueRef ir_render_unwrap_err_code(CodeGen *g, IrExecutable *executab
49084908static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *executable,
49094909 IrInstructionUnwrapErrPayload *instruction)
49104910{
4911 bool want_safety = ir_want_runtime_safety(g, &instruction->base) && instruction->safety_check_on &&
4911 bool want_safety = instruction->safety_check_on && ir_want_runtime_safety(g, &instruction->base) &&
49124912 g->errors_by_index.length > 1;
49134913 if (!want_safety && !type_has_bits(instruction->base.value.type))
49144914 return nullptr;
......@@ -4944,6 +4944,11 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu
49444944 }
49454945
49464946 if (type_has_bits(payload_type)) {
4947 if (instruction->initializing) {
4948 LLVMValueRef err_tag_ptr = LLVMBuildStructGEP(g->builder, err_union_handle, err_union_err_index, "");
4949 LLVMValueRef ok_err_val = LLVMConstNull(get_llvm_type(g, g->err_tag_type));
4950 gen_store_untyped(g, ok_err_val, err_tag_ptr, 0, false);
4951 }
49474952 return LLVMBuildStructGEP(g->builder, err_union_handle, err_union_payload_index, "");
49484953 } else {
49494954 return nullptr;
src/ir.cpp+33-21
......@@ -189,6 +189,8 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
189189 ResultLoc *result_loc, ZigType *value_type, IrInstruction *value);
190190static IrInstruction *ir_analyze_unwrap_optional_payload(IrAnalyze *ira, IrInstruction *source_instr,
191191 IrInstruction *base_ptr, bool safety_check_on, bool initializing);
192static IrInstruction *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInstruction *source_instr,
193 IrInstruction *base_ptr, bool safety_check_on, bool initializing);
192194
193195static ConstExprValue *const_ptr_pointee_unchecked(CodeGen *g, ConstExprValue *const_val) {
194196 assert(get_src_ptr_type(const_val->type) != nullptr);
......@@ -2424,11 +2426,12 @@ static IrInstruction *ir_build_unwrap_err_code(IrBuilder *irb, Scope *scope, Ast
24242426}
24252427
24262428static IrInstruction *ir_build_unwrap_err_payload(IrBuilder *irb, Scope *scope, AstNode *source_node,
2427 IrInstruction *value, bool safety_check_on)
2429 IrInstruction *value, bool safety_check_on, bool initializing)
24282430{
24292431 IrInstructionUnwrapErrPayload *instruction = ir_build_instruction<IrInstructionUnwrapErrPayload>(irb, scope, source_node);
24302432 instruction->value = value;
24312433 instruction->safety_check_on = safety_check_on;
2434 instruction->initializing = initializing;
24322435
24332436 ir_ref_instruction(value, irb->current_basic_block);
24342437
......@@ -3572,7 +3575,7 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,
35723575 }
35733576
35743577 ir_set_cursor_at_end_and_append_block(irb, continue_block);
3575 IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, scope, node, err_union_ptr, false);
3578 IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, scope, node, err_union_ptr, false, false);
35763579 if (lval == LValPtr)
35773580 return unwrapped_ptr;
35783581 else
......@@ -5578,7 +5581,7 @@ static IrInstruction *ir_gen_catch_unreachable(IrBuilder *irb, Scope *scope, Ast
55785581 if (err_union_ptr == irb->codegen->invalid_instruction)
55795582 return irb->codegen->invalid_instruction;
55805583
5581 IrInstruction *payload_ptr = ir_build_unwrap_err_payload(irb, scope, source_node, err_union_ptr, true);
5584 IrInstruction *payload_ptr = ir_build_unwrap_err_payload(irb, scope, source_node, err_union_ptr, true, false);
55825585 if (payload_ptr == irb->codegen->invalid_instruction)
55835586 return irb->codegen->invalid_instruction;
55845587
......@@ -5915,7 +5918,7 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
59155918 ir_set_cursor_at_end_and_append_block(irb, body_block);
59165919 if (var_symbol) {
59175920 IrInstruction *payload_ptr = ir_build_unwrap_err_payload(irb, payload_scope, symbol_node,
5918 err_val_ptr, false);
5921 err_val_ptr, false, false);
59195922 IrInstruction *var_ptr = node->data.while_expr.var_is_ptr ?
59205923 ir_build_ref(irb, payload_scope, symbol_node, payload_ptr, true, false) : payload_ptr;
59215924 ir_build_var_decl_src(irb, payload_scope, symbol_node, payload_var, nullptr, var_ptr);
......@@ -6694,7 +6697,7 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
66946697 ZigVar *var = ir_create_var(irb, node, subexpr_scope,
66956698 var_symbol, var_is_const, var_is_const, is_shadowable, var_is_comptime);
66966699
6697 IrInstruction *payload_ptr = ir_build_unwrap_err_payload(irb, subexpr_scope, node, err_val_ptr, false);
6700 IrInstruction *payload_ptr = ir_build_unwrap_err_payload(irb, subexpr_scope, node, err_val_ptr, false, false);
66986701 IrInstruction *var_ptr = var_is_ptr ?
66996702 ir_build_ref(irb, subexpr_scope, node, payload_ptr, true, false) : payload_ptr;
67006703 ir_build_var_decl_src(irb, subexpr_scope, node, var, nullptr, var_ptr);
......@@ -7316,7 +7319,7 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
73167319 ir_mark_gen(ir_build_br(irb, err_scope, node, end_block, is_comptime));
73177320
73187321 ir_set_cursor_at_end_and_append_block(irb, ok_block);
7319 IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, parent_scope, node, err_union_ptr, false);
7322 IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, parent_scope, node, err_union_ptr, false, false);
73207323 IrInstruction *unwrapped_payload = ir_build_load_ptr(irb, parent_scope, node, unwrapped_ptr);
73217324 ir_build_end_expr(irb, parent_scope, node, unwrapped_payload, &peer_parent->peers[1].base);
73227325 IrBasicBlock *after_ok_block = irb->current_basic_block;
......@@ -14947,6 +14950,8 @@ static IrInstruction *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrIn
1494714950 ZigType *actual_elem_type = result_loc->value.type->data.pointer.child_type;
1494814951 if (actual_elem_type->id == ZigTypeIdOptional && implicit_elem_type->id != ZigTypeIdOptional) {
1494914952 return ir_analyze_unwrap_optional_payload(ira, &instruction->base, result_loc, false, true);
14953 } else if (actual_elem_type->id == ZigTypeIdErrorUnion && implicit_elem_type->id != ZigTypeIdErrorUnion) {
14954 return ir_analyze_unwrap_error_payload(ira, &instruction->base, result_loc, false, true);
1495014955 }
1495114956 return result_loc;
1495214957}
......@@ -22136,14 +22141,10 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira, IrI
2213622141 return result;
2213722142}
2213822143
22139static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
22140 IrInstructionUnwrapErrPayload *instruction)
22144static IrInstruction *ir_analyze_unwrap_error_payload(IrAnalyze *ira, IrInstruction *source_instr,
22145 IrInstruction *base_ptr, bool safety_check_on, bool initializing)
2214122146{
22142 assert(instruction->value->child);
22143 IrInstruction *value = instruction->value->child;
22144 if (type_is_invalid(value->value.type))
22145 return ira->codegen->invalid_instruction;
22146 ZigType *ptr_type = value->value.type;
22147 ZigType *ptr_type = base_ptr->value.type;
2214722148
2214822149 // This will be a pointer type because unwrap err payload IR instruction operates on a pointer to a thing.
2214922150 assert(ptr_type->id == ZigTypeIdPointer);
......@@ -22153,7 +22154,7 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
2215322154 return ira->codegen->invalid_instruction;
2215422155
2215522156 if (type_entry->id != ZigTypeIdErrorUnion) {
22156 ir_add_error(ira, value,
22157 ir_add_error(ira, base_ptr,
2215722158 buf_sprintf("expected error union type, found '%s'", buf_ptr(&type_entry->name)));
2215822159 return ira->codegen->invalid_instruction;
2215922160 }
......@@ -22165,23 +22166,23 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
2216522166 ZigType *result_type = get_pointer_to_type_extra(ira->codegen, payload_type,
2216622167 ptr_type->data.pointer.is_const, ptr_type->data.pointer.is_volatile,
2216722168 PtrLenSingle, 0, 0, 0, false);
22168 if (instr_is_comptime(value)) {
22169 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
22169 if (instr_is_comptime(base_ptr)) {
22170 ConstExprValue *ptr_val = ir_resolve_const(ira, base_ptr, UndefBad);
2217022171 if (!ptr_val)
2217122172 return ira->codegen->invalid_instruction;
2217222173 if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar) {
22173 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, instruction->base.source_node);
22174 ConstExprValue *err_union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
2217422175 if (err_union_val == nullptr)
2217522176 return ira->codegen->invalid_instruction;
2217622177 if (err_union_val->special != ConstValSpecialRuntime) {
2217722178 ErrorTableEntry *err = err_union_val->data.x_err_union.error_set->data.x_err_set;
2217822179 if (err != nullptr) {
22179 ir_add_error(ira, &instruction->base,
22180 ir_add_error(ira, source_instr,
2218022181 buf_sprintf("caught unexpected error '%s'", buf_ptr(&err->name)));
2218122182 return ira->codegen->invalid_instruction;
2218222183 }
2218322184
22184 IrInstruction *result = ir_const(ira, &instruction->base, result_type);
22185 IrInstruction *result = ir_const(ira, source_instr, result_type);
2218522186 result->value.data.x_ptr.special = ConstPtrSpecialRef;
2218622187 result->value.data.x_ptr.data.ref.pointee = err_union_val->data.x_err_union.payload;
2218722188 return result;
......@@ -22189,12 +22190,23 @@ static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
2218922190 }
2219022191 }
2219122192
22192 IrInstruction *result = ir_build_unwrap_err_payload(&ira->new_irb,
22193 instruction->base.scope, instruction->base.source_node, value, instruction->safety_check_on);
22193 IrInstruction *result = ir_build_unwrap_err_payload(&ira->new_irb, source_instr->scope,
22194 source_instr->source_node, base_ptr, safety_check_on, initializing);
2219422195 result->value.type = result_type;
2219522196 return result;
2219622197}
2219722198
22199static IrInstruction *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
22200 IrInstructionUnwrapErrPayload *instruction)
22201{
22202 assert(instruction->value->child);
22203 IrInstruction *value = instruction->value->child;
22204 if (type_is_invalid(value->value.type))
22205 return ira->codegen->invalid_instruction;
22206
22207 return ir_analyze_unwrap_error_payload(ira, &instruction->base, value, instruction->safety_check_on, false);
22208}
22209
2219822210static IrInstruction *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstructionFnProto *instruction) {
2219922211 AstNode *proto_node = instruction->base.source_node;
2220022212 assert(proto_node->type == NodeTypeFnProto);