authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-15 12:28:21-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-15 12:28:21-04:00
log6bf193af192ffaf3465958a243ec8fc8941cfe4d
tree7271c30c7c6657765535002421c65a2377171477
parent60025a37045835c8bbf914eccdbeba6d6e2c275f
signaturelock-open Commit is signed but in an unrecognized format.

better result location semantics with optionals and return locations

somewhere along this branch, #1901 has been fixed.

14 files changed, 281 insertions(+), 169 deletions(-)

BRANCH_TODO+1-4
......@@ -4,10 +4,7 @@ Scratch pad for stuff to do before merging master
44uncomment all the behavior tests
55diff master branch to make sure
66
7restore test_runner.zig to master branch
8 - also the default panic function and unexpected_error_tracing. see the commit
9 that adds this text to BRANCH_TODO file.
10 - and std/specia/bootstrap.zig
7restore bootstrap.zig to master
118
129get an empty file compiling successfully (with no panic fn override)
1310
src/all_types.hpp+21-3
......@@ -2257,7 +2257,8 @@ enum IrInstructionId {
22572257 IrInstructionIdHandle,
22582258 IrInstructionIdAlignOf,
22592259 IrInstructionIdOverflowOp,
2260 IrInstructionIdTestErr,
2260 IrInstructionIdTestErrSrc,
2261 IrInstructionIdTestErrGen,
22612262 IrInstructionIdUnwrapErrCode,
22622263 IrInstructionIdUnwrapErrPayload,
22632264 IrInstructionIdErrWrapCode,
......@@ -2292,6 +2293,7 @@ enum IrInstructionId {
22922293 IrInstructionIdAlignCast,
22932294 IrInstructionIdImplicitCast,
22942295 IrInstructionIdResolveResult,
2296 IrInstructionIdResultPtr,
22952297 IrInstructionIdOpaqueType,
22962298 IrInstructionIdSetAlignStack,
22972299 IrInstructionIdArgType,
......@@ -3082,10 +3084,16 @@ struct IrInstructionAlignOf {
30823084};
30833085
30843086// returns true if error, returns false if not error
3085struct IrInstructionTestErr {
3087struct IrInstructionTestErrSrc {
30863088 IrInstruction base;
30873089
3088 IrInstruction *value;
3090 IrInstruction *base_ptr;
3091};
3092
3093struct IrInstructionTestErrGen {
3094 IrInstruction base;
3095
3096 IrInstruction *err_union;
30893097};
30903098
30913099// Takes an error union pointer, returns a pointer to the error code.
......@@ -3596,6 +3604,7 @@ struct IrInstructionImplicitCast {
35963604 ResultLoc *result_loc;
35973605};
35983606
3607// This one is for writing through the result pointer.
35993608struct IrInstructionResolveResult {
36003609 IrInstruction base;
36013610
......@@ -3603,6 +3612,15 @@ struct IrInstructionResolveResult {
36033612 IrInstruction *ty;
36043613};
36053614
3615// This one is when you want to read the value of the result.
3616// You have to give the value in case it is comptime.
3617struct IrInstructionResultPtr {
3618 IrInstruction base;
3619
3620 ResultLoc *result_loc;
3621 IrInstruction *result;
3622};
3623
36063624struct IrInstructionPtrOfArrayToSlice {
36073625 IrInstruction base;
36083626
src/codegen.cpp+25-11
......@@ -1323,7 +1323,9 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
13231323 LLVMBuildRetVoid(g->builder);
13241324
13251325 LLVMPositionBuilderAtEnd(g->builder, prev_block);
1326 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1326 if (!g->strip_debug_symbols) {
1327 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1328 }
13271329
13281330 g->add_error_return_trace_addr_fn_val = fn_val;
13291331 return fn_val;
......@@ -1454,7 +1456,9 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
14541456 LLVMBuildBr(g->builder, loop_block);
14551457
14561458 LLVMPositionBuilderAtEnd(g->builder, prev_block);
1457 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1459 if (!g->strip_debug_symbols) {
1460 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1461 }
14581462
14591463 g->merge_err_ret_traces_fn_val = fn_val;
14601464 return fn_val;
......@@ -1510,7 +1514,9 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
15101514 LLVMBuildRetVoid(g->builder);
15111515
15121516 LLVMPositionBuilderAtEnd(g->builder, prev_block);
1513 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1517 if (!g->strip_debug_symbols) {
1518 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1519 }
15141520
15151521 g->return_err_fn = fn_val;
15161522 return fn_val;
......@@ -1638,7 +1644,9 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
16381644 gen_panic(g, msg_slice, err_ret_trace_arg);
16391645
16401646 LLVMPositionBuilderAtEnd(g->builder, prev_block);
1641 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1647 if (!g->strip_debug_symbols) {
1648 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
1649 }
16421650
16431651 g->safety_crash_err_fn = fn_val;
16441652 return fn_val;
......@@ -4353,7 +4361,9 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
43534361 g->cur_fn = prev_cur_fn;
43544362 g->cur_fn_val = prev_cur_fn_val;
43554363 LLVMPositionBuilderAtEnd(g->builder, prev_block);
4356 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
4364 if (!g->strip_debug_symbols) {
4365 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
4366 }
43574367
43584368 enum_type->data.enumeration.name_function = fn_val;
43594369 return fn_val;
......@@ -4880,10 +4890,10 @@ static LLVMValueRef ir_render_overflow_op(CodeGen *g, IrExecutable *executable,
48804890 return overflow_bit;
48814891}
48824892
4883static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrInstructionTestErr *instruction) {
4884 ZigType *err_union_type = instruction->value->value.type;
4893static LLVMValueRef ir_render_test_err(CodeGen *g, IrExecutable *executable, IrInstructionTestErrGen *instruction) {
4894 ZigType *err_union_type = instruction->err_union->value.type;
48854895 ZigType *payload_type = err_union_type->data.error_union.payload_type;
4886 LLVMValueRef err_union_handle = ir_llvm_value(g, instruction->value);
4896 LLVMValueRef err_union_handle = ir_llvm_value(g, instruction->err_union);
48874897
48884898 LLVMValueRef err_val;
48894899 if (type_has_bits(payload_type)) {
......@@ -5276,7 +5286,9 @@ static LLVMValueRef get_coro_alloc_helper_fn_val(CodeGen *g, LLVMTypeRef alloc_f
52765286 g->cur_fn = prev_cur_fn;
52775287 g->cur_fn_val = prev_cur_fn_val;
52785288 LLVMPositionBuilderAtEnd(g->builder, prev_block);
5279 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
5289 if (!g->strip_debug_symbols) {
5290 LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
5291 }
52805292
52815293 g->coro_alloc_helper_fn_val = fn_val;
52825294 return fn_val;
......@@ -5549,10 +5561,12 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
55495561 case IrInstructionIdAllocaGen:
55505562 case IrInstructionIdImplicitCast:
55515563 case IrInstructionIdResolveResult:
5564 case IrInstructionIdResultPtr:
55525565 case IrInstructionIdContainerInitList:
55535566 case IrInstructionIdSliceSrc:
55545567 case IrInstructionIdRef:
55555568 case IrInstructionIdBitCastSrc:
5569 case IrInstructionIdTestErrSrc:
55565570 zig_unreachable();
55575571
55585572 case IrInstructionIdDeclVarGen:
......@@ -5635,8 +5649,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
56355649 return ir_render_handle(g, executable, (IrInstructionHandle *)instruction);
56365650 case IrInstructionIdOverflowOp:
56375651 return ir_render_overflow_op(g, executable, (IrInstructionOverflowOp *)instruction);
5638 case IrInstructionIdTestErr:
5639 return ir_render_test_err(g, executable, (IrInstructionTestErr *)instruction);
5652 case IrInstructionIdTestErrGen:
5653 return ir_render_test_err(g, executable, (IrInstructionTestErrGen *)instruction);
56405654 case IrInstructionIdUnwrapErrCode:
56415655 return ir_render_unwrap_err_code(g, executable, (IrInstructionUnwrapErrCode *)instruction);
56425656 case IrInstructionIdUnwrapErrPayload:
src/ir.cpp+86-28
......@@ -756,8 +756,12 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionOverflowOp *) {
756756 return IrInstructionIdOverflowOp;
757757}
758758
759static constexpr IrInstructionId ir_instruction_id(IrInstructionTestErr *) {
760 return IrInstructionIdTestErr;
759static constexpr IrInstructionId ir_instruction_id(IrInstructionTestErrSrc *) {
760 return IrInstructionIdTestErrSrc;
761}
762
763static constexpr IrInstructionId ir_instruction_id(IrInstructionTestErrGen *) {
764 return IrInstructionIdTestErrGen;
761765}
762766
763767static constexpr IrInstructionId ir_instruction_id(IrInstructionUnwrapErrCode *) {
......@@ -900,6 +904,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionResolveResult *)
900904 return IrInstructionIdResolveResult;
901905}
902906
907static constexpr IrInstructionId ir_instruction_id(IrInstructionResultPtr *) {
908 return IrInstructionIdResultPtr;
909}
910
903911static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrOfArrayToSlice *) {
904912 return IrInstructionIdPtrOfArrayToSlice;
905913}
......@@ -2418,13 +2426,26 @@ static IrInstruction *ir_build_align_of(IrBuilder *irb, Scope *scope, AstNode *s
24182426 return &instruction->base;
24192427}
24202428
2421static IrInstruction *ir_build_test_err(IrBuilder *irb, Scope *scope, AstNode *source_node,
2422 IrInstruction *value)
2429static IrInstruction *ir_build_test_err_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
2430 IrInstruction *base_ptr)
24232431{
2424 IrInstructionTestErr *instruction = ir_build_instruction<IrInstructionTestErr>(irb, scope, source_node);
2425 instruction->value = value;
2432 IrInstructionTestErrSrc *instruction = ir_build_instruction<IrInstructionTestErrSrc>(irb, scope, source_node);
2433 instruction->base_ptr = base_ptr;
24262434
2427 ir_ref_instruction(value, irb->current_basic_block);
2435 ir_ref_instruction(base_ptr, irb->current_basic_block);
2436
2437 return &instruction->base;
2438}
2439
2440static IrInstruction *ir_build_test_err_gen(IrAnalyze *ira, IrInstruction *source_instruction,
2441 IrInstruction *err_union)
2442{
2443 IrInstructionTestErrGen *instruction = ir_build_instruction<IrInstructionTestErrGen>(
2444 &ira->new_irb, source_instruction->scope, source_instruction->source_node);
2445 instruction->base.value.type = ira->codegen->builtin_types.entry_bool;
2446 instruction->err_union = err_union;
2447
2448 ir_ref_instruction(err_union, ira->new_irb.current_basic_block);
24282449
24292450 return &instruction->base;
24302451}
......@@ -2844,6 +2865,18 @@ static IrInstruction *ir_build_resolve_result(IrBuilder *irb, Scope *scope, AstN
28442865 return &instruction->base;
28452866}
28462867
2868static IrInstruction *ir_build_result_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node,
2869 ResultLoc *result_loc, IrInstruction *result)
2870{
2871 IrInstructionResultPtr *instruction = ir_build_instruction<IrInstructionResultPtr>(irb, scope, source_node);
2872 instruction->result_loc = result_loc;
2873 instruction->result = result;
2874
2875 ir_ref_instruction(result, irb->current_basic_block);
2876
2877 return &instruction->base;
2878}
2879
28472880static IrInstruction *ir_build_opaque_type(IrBuilder *irb, Scope *scope, AstNode *source_node) {
28482881 IrInstructionOpaqueType *instruction = ir_build_instruction<IrInstructionOpaqueType>(irb, scope, source_node);
28492882
......@@ -3531,7 +3564,9 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,
35313564 ir_gen_defers_for_block(irb, scope, outer_scope, false);
35323565 }
35333566
3534 IrInstruction *is_err = ir_build_test_err(irb, scope, node, return_value);
3567 IrInstruction *ret_ptr = ir_build_result_ptr(irb, scope, node, &result_loc_ret->base,
3568 return_value);
3569 IrInstruction *is_err = ir_build_test_err_src(irb, scope, node, ret_ptr);
35353570
35363571 bool should_inline = ir_should_inline(irb->exec, scope);
35373572 IrInstruction *is_comptime;
......@@ -3577,8 +3612,7 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,
35773612 IrInstruction *err_union_ptr = ir_gen_node_extra(irb, expr_node, scope, LValPtr, nullptr);
35783613 if (err_union_ptr == irb->codegen->invalid_instruction)
35793614 return irb->codegen->invalid_instruction;
3580 IrInstruction *err_union_val = ir_build_load_ptr(irb, scope, node, err_union_ptr);
3581 IrInstruction *is_err_val = ir_build_test_err(irb, scope, node, err_union_val);
3615 IrInstruction *is_err_val = ir_build_test_err_src(irb, scope, node, err_union_ptr);
35823616
35833617 IrBasicBlock *return_block = ir_create_basic_block(irb, scope, "ErrRetReturn");
35843618 IrBasicBlock *continue_block = ir_create_basic_block(irb, scope, "ErrRetContinue");
......@@ -5940,8 +5974,7 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
59405974 LValPtr, nullptr);
59415975 if (err_val_ptr == irb->codegen->invalid_instruction)
59425976 return err_val_ptr;
5943 IrInstruction *err_val = ir_build_load_ptr(irb, scope, node->data.while_expr.condition, err_val_ptr);
5944 IrInstruction *is_err = ir_build_test_err(irb, scope, node->data.while_expr.condition, err_val);
5977 IrInstruction *is_err = ir_build_test_err_src(irb, scope, node->data.while_expr.condition, err_val_ptr);
59455978 IrBasicBlock *after_cond_block = irb->current_basic_block;
59465979 IrInstruction *void_else_result = else_node ? nullptr : ir_mark_gen(ir_build_const_void(irb, scope, node));
59475980 IrInstruction *cond_br_inst;
......@@ -6722,7 +6755,7 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
67226755 return err_val_ptr;
67236756
67246757 IrInstruction *err_val = ir_build_load_ptr(irb, scope, node, err_val_ptr);
6725 IrInstruction *is_err = ir_build_test_err(irb, scope, node, err_val);
6758 IrInstruction *is_err = ir_build_test_err_src(irb, scope, node, err_val_ptr);
67266759
67276760 IrBasicBlock *ok_block = ir_create_basic_block(irb, scope, "TryOk");
67286761 IrBasicBlock *else_block = ir_create_basic_block(irb, scope, "TryElse");
......@@ -7330,8 +7363,7 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
73307363 if (err_union_ptr == irb->codegen->invalid_instruction)
73317364 return irb->codegen->invalid_instruction;
73327365
7333 IrInstruction *err_union_val = ir_build_load_ptr(irb, parent_scope, node, err_union_ptr);
7334 IrInstruction *is_err = ir_build_test_err(irb, parent_scope, node, err_union_val);
7366 IrInstruction *is_err = ir_build_test_err_src(irb, parent_scope, node, err_union_ptr);
73357367
73367368 IrInstruction *is_comptime;
73377369 if (ir_should_inline(irb->exec, parent_scope)) {
......@@ -15010,7 +15042,9 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
1501015042 return result_loc;
1501115043 ir_assert(result_loc->value.type->id == ZigTypeIdPointer, suspend_source_instr);
1501215044 ZigType *actual_elem_type = result_loc->value.type->data.pointer.child_type;
15013 if (actual_elem_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional) {
15045 if (actual_elem_type->id == ZigTypeIdOptional && value_type->id != ZigTypeIdOptional &&
15046 value_type->id != ZigTypeIdNull)
15047 {
1501415048 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, result_loc, false, true);
1501515049 } else if (actual_elem_type->id == ZigTypeIdErrorUnion && value_type->id != ZigTypeIdErrorUnion) {
1501615050 if (value_type->id == ZigTypeIdErrorSet) {
......@@ -22190,15 +22224,34 @@ static IrInstruction *ir_analyze_instruction_overflow_op(IrAnalyze *ira, IrInstr
2219022224 return result;
2219122225}
2219222226
22193static IrInstruction *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstructionTestErr *instruction) {
22194 IrInstruction *value = instruction->value->child;
22195 if (type_is_invalid(value->value.type))
22227static IrInstruction *ir_analyze_instruction_result_ptr(IrAnalyze *ira, IrInstructionResultPtr *instruction) {
22228 IrInstruction *result = instruction->result->child;
22229 if (type_is_invalid(result->value.type))
22230 return result;
22231
22232 if (instruction->result_loc->written && instruction->result_loc->resolved_loc != nullptr &&
22233 !instr_is_comptime(result))
22234 {
22235 IrInstruction *result_ptr = instruction->result_loc->resolved_loc;
22236 // Convert the pointer to the result type. They should be the same, except this will resolve
22237 // inferred error sets.
22238 ZigType *new_ptr_type = get_pointer_to_type(ira->codegen, result->value.type, true);
22239 return ir_analyze_ptr_cast(ira, &instruction->base, result_ptr, new_ptr_type, &instruction->base, false);
22240 }
22241 return ir_get_ref(ira, &instruction->base, result, true, false);
22242}
22243
22244static IrInstruction *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstructionTestErrSrc *instruction) {
22245 IrInstruction *base_ptr = instruction->base_ptr->child;
22246 if (type_is_invalid(base_ptr->value.type))
2219622247 return ira->codegen->invalid_instruction;
2219722248
22249 IrInstruction *value = ir_get_deref(ira, &instruction->base, base_ptr, nullptr);
2219822250 ZigType *type_entry = value->value.type;
22199 if (type_is_invalid(type_entry)) {
22251 if (type_is_invalid(type_entry))
2220022252 return ira->codegen->invalid_instruction;
22201 } else if (type_entry->id == ZigTypeIdErrorUnion) {
22253
22254 if (type_entry->id == ZigTypeIdErrorUnion) {
2220222255 if (instr_is_comptime(value)) {
2220322256 ConstExprValue *err_union_val = ir_resolve_const(ira, value, UndefBad);
2220422257 if (!err_union_val)
......@@ -22221,10 +22274,7 @@ static IrInstruction *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstruct
2222122274 return ir_const_bool(ira, &instruction->base, false);
2222222275 }
2222322276
22224 IrInstruction *result = ir_build_test_err(&ira->new_irb,
22225 instruction->base.scope, instruction->base.source_node, value);
22226 result->value.type = ira->codegen->builtin_types.entry_bool;
22227 return result;
22277 return ir_build_test_err_gen(ira, &instruction->base, value);
2222822278 } else if (type_entry->id == ZigTypeIdErrorSet) {
2222922279 return ir_const_bool(ira, &instruction->base, true);
2223022280 } else {
......@@ -24343,6 +24393,7 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2434324393 case IrInstructionIdAllocaGen:
2434424394 case IrInstructionIdSliceGen:
2434524395 case IrInstructionIdRefGen:
24396 case IrInstructionIdTestErrGen:
2434624397 zig_unreachable();
2434724398
2434824399 case IrInstructionIdReturn:
......@@ -24497,8 +24548,8 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2449724548 return ir_analyze_instruction_align_of(ira, (IrInstructionAlignOf *)instruction);
2449824549 case IrInstructionIdOverflowOp:
2449924550 return ir_analyze_instruction_overflow_op(ira, (IrInstructionOverflowOp *)instruction);
24500 case IrInstructionIdTestErr:
24501 return ir_analyze_instruction_test_err(ira, (IrInstructionTestErr *)instruction);
24551 case IrInstructionIdTestErrSrc:
24552 return ir_analyze_instruction_test_err(ira, (IrInstructionTestErrSrc *)instruction);
2450224553 case IrInstructionIdUnwrapErrCode:
2450324554 return ir_analyze_instruction_unwrap_err_code(ira, (IrInstructionUnwrapErrCode *)instruction);
2450424555 case IrInstructionIdUnwrapErrPayload:
......@@ -24543,6 +24594,8 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2454324594 return ir_analyze_instruction_implicit_cast(ira, (IrInstructionImplicitCast *)instruction);
2454424595 case IrInstructionIdResolveResult:
2454524596 return ir_analyze_instruction_resolve_result(ira, (IrInstructionResolveResult *)instruction);
24597 case IrInstructionIdResultPtr:
24598 return ir_analyze_instruction_result_ptr(ira, (IrInstructionResultPtr *)instruction);
2454624599 case IrInstructionIdOpaqueType:
2454724600 return ir_analyze_instruction_opaque_type(ira, (IrInstructionOpaqueType *)instruction);
2454824601 case IrInstructionIdSetAlignStack:
......@@ -24672,6 +24725,9 @@ ZigType *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutable *new_
2467224725 continue;
2467324726 }
2467424727
24728 if (ira->codegen->verbose_ir) {
24729 fprintf(stderr, "analyze #%zu\n", old_instruction->debug_id);
24730 }
2467524731 IrInstruction *new_instruction = ir_analyze_instruction_base(ira, old_instruction);
2467624732 if (new_instruction != nullptr) {
2467724733 ir_assert(new_instruction->value.type != nullptr || new_instruction->value.type != nullptr, old_instruction);
......@@ -24808,7 +24864,8 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2480824864 case IrInstructionIdReturnAddress:
2480924865 case IrInstructionIdFrameAddress:
2481024866 case IrInstructionIdHandle:
24811 case IrInstructionIdTestErr:
24867 case IrInstructionIdTestErrSrc:
24868 case IrInstructionIdTestErrGen:
2481224869 case IrInstructionIdFnProto:
2481324870 case IrInstructionIdTestComptime:
2481424871 case IrInstructionIdPtrCastSrc:
......@@ -24860,6 +24917,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2486024917 case IrInstructionIdHasDecl:
2486124918 case IrInstructionIdAllocaSrc:
2486224919 case IrInstructionIdAllocaGen:
24920 case IrInstructionIdResultPtr:
2486324921 return false;
2486424922
2486524923 case IrInstructionIdAsm:
src/ir_print.cpp+25-8
......@@ -961,9 +961,15 @@ static void ir_print_overflow_op(IrPrint *irp, IrInstructionOverflowOp *instruct
961961 fprintf(irp->f, ")");
962962}
963963
964static void ir_print_test_err(IrPrint *irp, IrInstructionTestErr *instruction) {
964static void ir_print_test_err_src(IrPrint *irp, IrInstructionTestErrSrc *instruction) {
965965 fprintf(irp->f, "@testError(");
966 ir_print_other_instruction(irp, instruction->value);
966 ir_print_other_instruction(irp, instruction->base_ptr);
967 fprintf(irp->f, ")");
968}
969
970static void ir_print_test_err_gen(IrPrint *irp, IrInstructionTestErrGen *instruction) {
971 fprintf(irp->f, "@testError(");
972 ir_print_other_instruction(irp, instruction->err_union);
967973 fprintf(irp->f, ")");
968974}
969975
......@@ -976,10 +982,7 @@ static void ir_print_unwrap_err_code(IrPrint *irp, IrInstructionUnwrapErrCode *i
976982static void ir_print_unwrap_err_payload(IrPrint *irp, IrInstructionUnwrapErrPayload *instruction) {
977983 fprintf(irp->f, "ErrorUnionFieldPayload(");
978984 ir_print_other_instruction(irp, instruction->value);
979 fprintf(irp->f, ")");
980 if (!instruction->safety_check_on) {
981 fprintf(irp->f, " // no safety");
982 }
985 fprintf(irp->f, ")safety=%d,init=%d",instruction->safety_check_on, instruction->initializing);
983986}
984987
985988static void ir_print_optional_wrap(IrPrint *irp, IrInstructionOptionalWrap *instruction) {
......@@ -1301,6 +1304,14 @@ static void ir_print_resolve_result(IrPrint *irp, IrInstructionResolveResult *in
13011304 fprintf(irp->f, ")");
13021305}
13031306
1307static void ir_print_result_ptr(IrPrint *irp, IrInstructionResultPtr *instruction) {
1308 fprintf(irp->f, "ResultPtr(");
1309 ir_print_result_loc(irp, instruction->result_loc);
1310 fprintf(irp->f, ",");
1311 ir_print_other_instruction(irp, instruction->result);
1312 fprintf(irp->f, ")");
1313}
1314
13041315static void ir_print_opaque_type(IrPrint *irp, IrInstructionOpaqueType *instruction) {
13051316 fprintf(irp->f, "@OpaqueType()");
13061317}
......@@ -1837,8 +1848,11 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
18371848 case IrInstructionIdOverflowOp:
18381849 ir_print_overflow_op(irp, (IrInstructionOverflowOp *)instruction);
18391850 break;
1840 case IrInstructionIdTestErr:
1841 ir_print_test_err(irp, (IrInstructionTestErr *)instruction);
1851 case IrInstructionIdTestErrSrc:
1852 ir_print_test_err_src(irp, (IrInstructionTestErrSrc *)instruction);
1853 break;
1854 case IrInstructionIdTestErrGen:
1855 ir_print_test_err_gen(irp, (IrInstructionTestErrGen *)instruction);
18421856 break;
18431857 case IrInstructionIdUnwrapErrCode:
18441858 ir_print_unwrap_err_code(irp, (IrInstructionUnwrapErrCode *)instruction);
......@@ -1939,6 +1953,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
19391953 case IrInstructionIdResolveResult:
19401954 ir_print_resolve_result(irp, (IrInstructionResolveResult *)instruction);
19411955 break;
1956 case IrInstructionIdResultPtr:
1957 ir_print_result_ptr(irp, (IrInstructionResultPtr *)instruction);
1958 break;
19421959 case IrInstructionIdOpaqueType:
19431960 ir_print_opaque_type(irp, (IrInstructionOpaqueType *)instruction);
19441961 break;
std/os.zig+1-1
......@@ -2487,7 +2487,7 @@ pub fn toPosixPath(file_path: []const u8) ![PATH_MAX]u8 {
24872487/// if this happens the fix is to add the error code to the corresponding
24882488/// switch expression, possibly introduce a new error in the error set, and
24892489/// send a patch to Zig.
2490pub const unexpected_error_tracing = false;
2490pub const unexpected_error_tracing = builtin.mode == .Debug;
24912491
24922492pub const UnexpectedError = error{
24932493 /// The Operating System returned an undocumented error code.
std/special/panic.zig+19-4
......@@ -7,8 +7,23 @@ const builtin = @import("builtin");
77const std = @import("std");
88
99pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
10 const stderr = std.io.getStdErr() catch std.process.abort();
11 stderr.write("panic: ") catch std.process.abort();
12 stderr.write(msg) catch std.process.abort();
13 std.process.abort();
10 @setCold(true);
11 switch (builtin.os) {
12 .freestanding => {
13 while (true) {}
14 },
15 .wasi => {
16 std.debug.warn("{}", msg);
17 _ = std.os.wasi.proc_raise(std.os.wasi.SIGABRT);
18 unreachable;
19 },
20 .uefi => {
21 // TODO look into using the debug info and logging helpful messages
22 std.os.abort();
23 },
24 else => {
25 const first_trace_addr = @returnAddress();
26 std.debug.panicExtra(error_return_trace, first_trace_addr, "{}", msg);
27 },
28 }
1429}
std/special/test_runner.zig+8-14
......@@ -2,34 +2,28 @@ const std = @import("std");
22const io = std.io;
33const builtin = @import("builtin");
44const test_fn_list = builtin.test_functions;
5const warn = std.debug.warn;
56
6pub fn main() void {
7 const stderr = io.getStdErr() catch std.process.abort();
8
7pub fn main() !void {
98 var ok_count: usize = 0;
109 var skip_count: usize = 0;
1110 for (test_fn_list) |test_fn, i| {
12 stderr.write("test ") catch std.process.abort();
13 stderr.write(test_fn.name) catch std.process.abort();
11 warn("{}/{} {}...", i + 1, test_fn_list.len, test_fn.name);
1412
1513 if (test_fn.func()) |_| {
1614 ok_count += 1;
17 stderr.write("...OK\n") catch std.process.abort();
15 warn("OK\n");
1816 } else |err| switch (err) {
1917 error.SkipZigTest => {
2018 skip_count += 1;
21 stderr.write("...SKIP\n") catch std.process.abort();
22 },
23 else => {
24 stderr.write("error: ") catch std.process.abort();
25 stderr.write(@errorName(err)) catch std.process.abort();
26 std.process.abort();
19 warn("SKIP\n");
2720 },
21 else => return err,
2822 }
2923 }
3024 if (ok_count == test_fn_list.len) {
31 stderr.write("All tests passed.\n") catch std.process.abort();
25 warn("All tests passed.\n");
3226 } else {
33 stderr.write("Some tests skipped.\n") catch std.process.abort();
27 warn("{} passed; {} skipped.\n", ok_count, skip_count);
3428 }
3529}
test/stage1/behavior.zig+9-9
......@@ -40,46 +40,46 @@ comptime {
4040 //_ = @import("behavior/bugs/920.zig");
4141 _ = @import("behavior/byval_arg_var.zig");
4242 //_ = @import("behavior/cancel.zig");
43 _ = @import("behavior/cast.zig"); // TODO
43 _ = @import("behavior/cast.zig");
4444 _ = @import("behavior/const_slice_child.zig");
4545 //_ = @import("behavior/coroutine_await_struct.zig");
4646 //_ = @import("behavior/coroutines.zig");
4747 _ = @import("behavior/defer.zig");
4848 _ = @import("behavior/enum.zig");
4949 _ = @import("behavior/enum_with_members.zig");
50 //_ = @import("behavior/error.zig");
50 _ = @import("behavior/error.zig"); // TODO
5151 _ = @import("behavior/eval.zig"); // TODO
5252 _ = @import("behavior/field_parent_ptr.zig");
5353 _ = @import("behavior/fn.zig");
5454 _ = @import("behavior/fn_in_struct_in_comptime.zig");
5555 _ = @import("behavior/for.zig");
56 _ = @import("behavior/generics.zig"); // TODO
56 _ = @import("behavior/generics.zig");
5757 _ = @import("behavior/hasdecl.zig");
5858 _ = @import("behavior/if.zig");
59 //_ = @import("behavior/import.zig");
59 _ = @import("behavior/import.zig");
6060 _ = @import("behavior/incomplete_struct_param_tld.zig");
6161 _ = @import("behavior/inttoptr.zig");
6262 _ = @import("behavior/ir_block_deps.zig");
63 //_ = @import("behavior/math.zig");
63 _ = @import("behavior/math.zig");
6464 _ = @import("behavior/merge_error_sets.zig");
6565 _ = @import("behavior/misc.zig"); // TODO
6666 _ = @import("behavior/namespace_depends_on_compile_var.zig");
6767 _ = @import("behavior/new_stack_call.zig");
6868 _ = @import("behavior/null.zig");
6969 _ = @import("behavior/optional.zig"); // TODO
70 //_ = @import("behavior/pointers.zig");
70 _ = @import("behavior/pointers.zig");
7171 _ = @import("behavior/popcount.zig");
7272 _ = @import("behavior/ptrcast.zig"); // TODO
7373 _ = @import("behavior/pub_enum.zig");
7474 _ = @import("behavior/ref_var_in_if_after_if_2nd_switch_prong.zig");
7575 _ = @import("behavior/reflection.zig");
7676 _ = @import("behavior/sizeof_and_typeof.zig");
77 //_ = @import("behavior/slice.zig");
77 _ = @import("behavior/slice.zig");
7878 _ = @import("behavior/slicetobytes.zig");
7979 //_ = @import("behavior/struct.zig");
8080 _ = @import("behavior/struct_contains_null_ptr_itself.zig");
8181 _ = @import("behavior/struct_contains_slice_of_itself.zig");
82 //_ = @import("behavior/switch.zig");
82 _ = @import("behavior/switch.zig");
8383 //_ = @import("behavior/switch_prong_err_enum.zig");
8484 _ = @import("behavior/switch_prong_implicit_cast.zig");
8585 _ = @import("behavior/syntax.zig");
......@@ -87,7 +87,7 @@ comptime {
8787 _ = @import("behavior/truncate.zig");
8888 _ = @import("behavior/try.zig");
8989 _ = @import("behavior/type_info.zig");
90 //_ = @import("behavior/typename.zig");
90 _ = @import("behavior/typename.zig");
9191 _ = @import("behavior/undefined.zig");
9292 _ = @import("behavior/underscore.zig");
9393 _ = @import("behavior/union.zig");
test/stage1/behavior/cast.zig+8-8
......@@ -124,14 +124,14 @@ fn returnNullLitFromOptionalTypeErrorRef() anyerror!?*A {
124124 return null;
125125}
126126
127//test "peer type resolution: ?T and T" {
128// expect(peerTypeTAndOptionalT(true, false).? == 0);
129// expect(peerTypeTAndOptionalT(false, false).? == 3);
130// comptime {
131// expect(peerTypeTAndOptionalT(true, false).? == 0);
132// expect(peerTypeTAndOptionalT(false, false).? == 3);
133// }
134//}
127test "peer type resolution: ?T and T" {
128 expect(peerTypeTAndOptionalT(true, false).? == 0);
129 expect(peerTypeTAndOptionalT(false, false).? == 3);
130 comptime {
131 expect(peerTypeTAndOptionalT(true, false).? == 0);
132 expect(peerTypeTAndOptionalT(false, false).? == 3);
133 }
134}
135135fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize {
136136 if (c) {
137137 return if (b) null else usize(0);
test/stage1/behavior/error.zig+42-42
......@@ -249,48 +249,48 @@ fn intLiteral(str: []const u8) !?i64 {
249249 return error.T;
250250}
251251
252test "nested error union function call in optional unwrap" {
253 const S = struct {
254 const Foo = struct {
255 a: i32,
256 };
257
258 fn errorable() !i32 {
259 var x: Foo = (try getFoo()) orelse return error.Other;
260 return x.a;
261 }
262
263 fn errorable2() !i32 {
264 var x: Foo = (try getFoo2()) orelse return error.Other;
265 return x.a;
266 }
267
268 fn errorable3() !i32 {
269 var x: Foo = (try getFoo3()) orelse return error.Other;
270 return x.a;
271 }
272
273 fn getFoo() anyerror!?Foo {
274 return Foo{ .a = 1234 };
275 }
276
277 fn getFoo2() anyerror!?Foo {
278 return error.Failure;
279 }
280
281 fn getFoo3() anyerror!?Foo {
282 return null;
283 }
284 };
285 expect((try S.errorable()) == 1234);
286 expectError(error.Failure, S.errorable2());
287 expectError(error.Other, S.errorable3());
288 comptime {
289 expect((try S.errorable()) == 1234);
290 expectError(error.Failure, S.errorable2());
291 expectError(error.Other, S.errorable3());
292 }
293}
252//test "nested error union function call in optional unwrap" {
253// const S = struct {
254// const Foo = struct {
255// a: i32,
256// };
257//
258// fn errorable() !i32 {
259// var x: Foo = (try getFoo()) orelse return error.Other;
260// return x.a;
261// }
262//
263// fn errorable2() !i32 {
264// var x: Foo = (try getFoo2()) orelse return error.Other;
265// return x.a;
266// }
267//
268// fn errorable3() !i32 {
269// var x: Foo = (try getFoo3()) orelse return error.Other;
270// return x.a;
271// }
272//
273// fn getFoo() anyerror!?Foo {
274// return Foo{ .a = 1234 };
275// }
276//
277// fn getFoo2() anyerror!?Foo {
278// return error.Failure;
279// }
280//
281// fn getFoo3() anyerror!?Foo {
282// return null;
283// }
284// };
285// expect((try S.errorable()) == 1234);
286// expectError(error.Failure, S.errorable2());
287// expectError(error.Other, S.errorable3());
288// comptime {
289// expect((try S.errorable()) == 1234);
290// expectError(error.Failure, S.errorable2());
291// expectError(error.Other, S.errorable3());
292// }
293//}
294294
295295test "widen cast integer payload of error union function call" {
296296 const S = struct {
test/stage1/behavior/generics.zig+13-13
......@@ -80,19 +80,19 @@ test "function with return type type" {
8080 expect(list2.prealloc_items.len == 8);
8181}
8282
83//test "generic struct" {
84// var a1 = GenNode(i32){
85// .value = 13,
86// .next = null,
87// };
88// var b1 = GenNode(bool){
89// .value = true,
90// .next = null,
91// };
92// expect(a1.value == 13);
93// expect(a1.value == a1.getVal());
94// expect(b1.getVal());
95//}
83test "generic struct" {
84 var a1 = GenNode(i32){
85 .value = 13,
86 .next = null,
87 };
88 var b1 = GenNode(bool){
89 .value = true,
90 .next = null,
91 };
92 expect(a1.value == 13);
93 expect(a1.value == a1.getVal());
94 expect(b1.getVal());
95}
9696fn GenNode(comptime T: type) type {
9797 return struct {
9898 value: T,
test/stage1/behavior/optional.zig+1-2
......@@ -76,6 +76,5 @@ test "unwrap function call with optional pointer return value" {
7676 }
7777 };
7878 S.entry();
79 // TODO https://github.com/ziglang/zig/issues/1901
80 //comptime S.entry();
79 comptime S.entry();
8180}
test/stage1/behavior/while.zig+22-22
......@@ -82,28 +82,28 @@ test "while with else" {
8282 expect(got_else == 1);
8383}
8484
85//test "while with optional as condition" {
86// numbers_left = 10;
87// var sum: i32 = 0;
88// while (getNumberOrNull()) |value| {
89// sum += value;
90// }
91// expect(sum == 45);
92//}
93//
94//test "while with optional as condition with else" {
95// numbers_left = 10;
96// var sum: i32 = 0;
97// var got_else: i32 = 0;
98// while (getNumberOrNull()) |value| {
99// sum += value;
100// expect(got_else == 0);
101// } else {
102// got_else += 1;
103// }
104// expect(sum == 45);
105// expect(got_else == 1);
106//}
85test "while with optional as condition" {
86 numbers_left = 10;
87 var sum: i32 = 0;
88 while (getNumberOrNull()) |value| {
89 sum += value;
90 }
91 expect(sum == 45);
92}
93
94test "while with optional as condition with else" {
95 numbers_left = 10;
96 var sum: i32 = 0;
97 var got_else: i32 = 0;
98 while (getNumberOrNull()) |value| {
99 sum += value;
100 expect(got_else == 0);
101 } else {
102 got_else += 1;
103 }
104 expect(sum == 45);
105 expect(got_else == 1);
106}
107107
108108test "while with error union condition" {
109109 numbers_left = 10;