authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-11 18:26:01-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-11 18:26:01-04:00
logce5d50e4ed519d23d7a467bdfee196093a3f5dc2
treec4797424b877cd6b2fdfa15e9a45ee1fdffbe659
parentb3a4ec1bd209c022445b6f70385b3f88517e72f9
signature Commit is signed but in an unrecognized format.

fix runtime if nested inside comptime if


5 files changed, 43 insertions(+), 19 deletions(-)

BRANCH_TODO+1
...@@ -4,6 +4,7 @@ Scratch pad for stuff to do before merging master...@@ -4,6 +4,7 @@ Scratch pad for stuff to do before merging master
4restore test_runner.zig to master branch4restore test_runner.zig to master branch
5 - also the default panic function and unexpected_error_tracing. see the commit5 - also the default panic function and unexpected_error_tracing. see the commit
6 that adds this text to BRANCH_TODO file.6 that adds this text to BRANCH_TODO file.
7 - and std/specia/bootstrap.zig
78
8get an empty file compiling successfully (with no panic fn override)9get an empty file compiling successfully (with no panic fn override)
910
src/all_types.hpp+1
...@@ -3656,6 +3656,7 @@ struct ResultLocPeerParent {...@@ -3656,6 +3656,7 @@ struct ResultLocPeerParent {
3656 ResultLocPeer *peers;3656 ResultLocPeer *peers;
3657 size_t peer_count;3657 size_t peer_count;
3658 ZigType *resolved_type;3658 ZigType *resolved_type;
3659 IrInstruction *is_comptime;
3659};3660};
36603661
3661struct IrSuspendPosition {3662struct IrSuspendPosition {
src/ir.cpp+34-12
...@@ -3885,11 +3885,12 @@ static IrInstruction *ir_gen_bool_and(IrBuilder *irb, Scope *scope, AstNode *nod...@@ -3885,11 +3885,12 @@ static IrInstruction *ir_gen_bool_and(IrBuilder *irb, Scope *scope, AstNode *nod
3885}3885}
38863886
3887static ResultLocPeerParent *create_binary_result_peers(IrInstruction *cond_br_inst,3887static ResultLocPeerParent *create_binary_result_peers(IrInstruction *cond_br_inst,
3888 IrBasicBlock *else_block, IrBasicBlock *endif_block, ResultLoc *parent)3888 IrBasicBlock *else_block, IrBasicBlock *endif_block, ResultLoc *parent, IrInstruction *is_comptime)
3889{3889{
3890 ResultLocPeerParent *peer_parent = allocate<ResultLocPeerParent>(1);3890 ResultLocPeerParent *peer_parent = allocate<ResultLocPeerParent>(1);
3891 peer_parent->base.id = ResultLocIdPeerParent;3891 peer_parent->base.id = ResultLocIdPeerParent;
3892 peer_parent->base.source_instruction = cond_br_inst;3892 peer_parent->base.source_instruction = cond_br_inst;
3893 peer_parent->is_comptime = is_comptime;
3893 peer_parent->parent = parent;3894 peer_parent->parent = parent;
3894 peer_parent->peer_count = 2;3895 peer_parent->peer_count = 2;
3895 peer_parent->peers = allocate<ResultLocPeer>(2);3896 peer_parent->peers = allocate<ResultLocPeer>(2);
...@@ -3931,7 +3932,8 @@ static IrInstruction *ir_gen_orelse(IrBuilder *irb, Scope *parent_scope, AstNode...@@ -3931,7 +3932,8 @@ static IrInstruction *ir_gen_orelse(IrBuilder *irb, Scope *parent_scope, AstNode
3931 IrBasicBlock *end_block = ir_create_basic_block(irb, parent_scope, "OptionalEnd");3932 IrBasicBlock *end_block = ir_create_basic_block(irb, parent_scope, "OptionalEnd");
3932 IrInstruction *cond_br_inst = ir_build_cond_br(irb, parent_scope, node, is_non_null, ok_block, null_block, is_comptime);3933 IrInstruction *cond_br_inst = ir_build_cond_br(irb, parent_scope, node, is_non_null, ok_block, null_block, is_comptime);
39333934
3934 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, ok_block, end_block, result_loc);3935 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, ok_block, end_block, result_loc,
3936 is_comptime);
39353937
3936 ir_set_cursor_at_end_and_append_block(irb, null_block);3938 ir_set_cursor_at_end_and_append_block(irb, null_block);
3937 IrInstruction *null_result = ir_gen_node_extra(irb, op2_node, parent_scope, lval, &peer_parent->peers[0].base);3939 IrInstruction *null_result = ir_gen_node_extra(irb, op2_node, parent_scope, lval, &peer_parent->peers[0].base);
...@@ -5438,7 +5440,8 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode...@@ -5438,7 +5440,8 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
5438 IrInstruction *cond_br_inst = ir_build_cond_br(irb, scope, condition->source_node, condition,5440 IrInstruction *cond_br_inst = ir_build_cond_br(irb, scope, condition->source_node, condition,
5439 then_block, else_block, is_comptime);5441 then_block, else_block, is_comptime);
54405442
5441 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, endif_block, result_loc);5443 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, endif_block,
5444 result_loc, is_comptime);
54425445
5443 ir_set_cursor_at_end_and_append_block(irb, then_block);5446 ir_set_cursor_at_end_and_append_block(irb, then_block);
54445447
...@@ -5459,6 +5462,7 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode...@@ -5459,6 +5462,7 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, Scope *scope, AstNode
5459 return irb->codegen->invalid_instruction;5462 return irb->codegen->invalid_instruction;
5460 } else {5463 } else {
5461 else_expr_result = ir_build_const_void(irb, scope, node);5464 else_expr_result = ir_build_const_void(irb, scope, node);
5465 ir_build_end_expr(irb, scope, node, else_expr_result, &peer_parent->peers[1].base);
5462 }5466 }
5463 IrBasicBlock *after_else_block = irb->current_basic_block;5467 IrBasicBlock *after_else_block = irb->current_basic_block;
5464 if (!instr_is_unreachable(else_expr_result))5468 if (!instr_is_unreachable(else_expr_result))
...@@ -5930,7 +5934,8 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n...@@ -5930,7 +5934,8 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
5930 cond_br_inst = is_err; // for the purposes of the source instruction to create_binary_result_peers5934 cond_br_inst = is_err; // for the purposes of the source instruction to create_binary_result_peers
5931 }5935 }
59325936
5933 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, end_block, result_loc);5937 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, end_block,
5938 result_loc, is_comptime);
59345939
5935 ir_set_cursor_at_end_and_append_block(irb, body_block);5940 ir_set_cursor_at_end_and_append_block(irb, body_block);
5936 if (var_symbol) {5941 if (var_symbol) {
...@@ -6031,7 +6036,8 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n...@@ -6031,7 +6036,8 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
6031 cond_br_inst = is_non_null; // for the purposes of source instruction for create_binary_result_peers6036 cond_br_inst = is_non_null; // for the purposes of source instruction for create_binary_result_peers
6032 }6037 }
60336038
6034 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, end_block, result_loc);6039 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, end_block,
6040 result_loc, is_comptime);
60356041
6036 ir_set_cursor_at_end_and_append_block(irb, body_block);6042 ir_set_cursor_at_end_and_append_block(irb, body_block);
6037 IrInstruction *payload_ptr = ir_build_optional_unwrap_ptr(irb, child_scope, symbol_node, maybe_val_ptr, false, false);6043 IrInstruction *payload_ptr = ir_build_optional_unwrap_ptr(irb, child_scope, symbol_node, maybe_val_ptr, false, false);
...@@ -6113,7 +6119,8 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n...@@ -6113,7 +6119,8 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
6113 cond_br_inst = cond_val; // for the source instruction arg to create_binary_result_peers6119 cond_br_inst = cond_val; // for the source instruction arg to create_binary_result_peers
6114 }6120 }
61156121
6116 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, end_block, result_loc);6122 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, end_block,
6123 result_loc, is_comptime);
6117 ir_set_cursor_at_end_and_append_block(irb, body_block);6124 ir_set_cursor_at_end_and_append_block(irb, body_block);
61186125
6119 ZigList<IrInstruction *> incoming_values = {0};6126 ZigList<IrInstruction *> incoming_values = {0};
...@@ -6244,7 +6251,8 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo...@@ -6244,7 +6251,8 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
6244 IrInstruction *cond_br_inst = ir_mark_gen(ir_build_cond_br(irb, parent_scope, node, cond,6251 IrInstruction *cond_br_inst = ir_mark_gen(ir_build_cond_br(irb, parent_scope, node, cond,
6245 body_block, else_block, is_comptime));6252 body_block, else_block, is_comptime));
62466253
6247 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, end_block, result_loc);6254 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, end_block, result_loc,
6255 is_comptime);
62486256
6249 ir_set_cursor_at_end_and_append_block(irb, body_block);6257 ir_set_cursor_at_end_and_append_block(irb, body_block);
6250 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, parent_scope, node, array_val_ptr, index_val, false, PtrLenSingle);6258 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, parent_scope, node, array_val_ptr, index_val, false, PtrLenSingle);
...@@ -6622,7 +6630,8 @@ static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstN...@@ -6622,7 +6630,8 @@ static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstN
6622 IrInstruction *cond_br_inst = ir_build_cond_br(irb, scope, node, is_non_null,6630 IrInstruction *cond_br_inst = ir_build_cond_br(irb, scope, node, is_non_null,
6623 then_block, else_block, is_comptime);6631 then_block, else_block, is_comptime);
66246632
6625 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, endif_block, result_loc);6633 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, endif_block,
6634 result_loc, is_comptime);
66266635
6627 ir_set_cursor_at_end_and_append_block(irb, then_block);6636 ir_set_cursor_at_end_and_append_block(irb, then_block);
66286637
...@@ -6657,6 +6666,7 @@ static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstN...@@ -6657,6 +6666,7 @@ static IrInstruction *ir_gen_if_optional_expr(IrBuilder *irb, Scope *scope, AstN
6657 return else_expr_result;6666 return else_expr_result;
6658 } else {6667 } else {
6659 else_expr_result = ir_build_const_void(irb, scope, node);6668 else_expr_result = ir_build_const_void(irb, scope, node);
6669 ir_build_end_expr(irb, scope, node, else_expr_result, &peer_parent->peers[1].base);
6660 }6670 }
6661 IrBasicBlock *after_else_block = irb->current_basic_block;6671 IrBasicBlock *after_else_block = irb->current_basic_block;
6662 if (!instr_is_unreachable(else_expr_result))6672 if (!instr_is_unreachable(else_expr_result))
...@@ -6702,7 +6712,8 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *...@@ -6702,7 +6712,8 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
6702 IrInstruction *is_comptime = force_comptime ? ir_build_const_bool(irb, scope, node, true) : ir_build_test_comptime(irb, scope, node, is_err);6712 IrInstruction *is_comptime = force_comptime ? ir_build_const_bool(irb, scope, node, true) : ir_build_test_comptime(irb, scope, node, is_err);
6703 IrInstruction *cond_br_inst = ir_build_cond_br(irb, scope, node, is_err, else_block, ok_block, is_comptime);6713 IrInstruction *cond_br_inst = ir_build_cond_br(irb, scope, node, is_err, else_block, ok_block, is_comptime);
67046714
6705 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, endif_block, result_loc);6715 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, else_block, endif_block,
6716 result_loc, is_comptime);
67066717
6707 ir_set_cursor_at_end_and_append_block(irb, ok_block);6718 ir_set_cursor_at_end_and_append_block(irb, ok_block);
67086719
...@@ -6752,6 +6763,7 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *...@@ -6752,6 +6763,7 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
6752 return else_expr_result;6763 return else_expr_result;
6753 } else {6764 } else {
6754 else_expr_result = ir_build_const_void(irb, scope, node);6765 else_expr_result = ir_build_const_void(irb, scope, node);
6766 ir_build_end_expr(irb, scope, node, else_expr_result, &peer_parent->peers[1].base);
6755 }6767 }
6756 IrBasicBlock *after_else_block = irb->current_basic_block;6768 IrBasicBlock *after_else_block = irb->current_basic_block;
6757 if (!instr_is_unreachable(else_expr_result))6769 if (!instr_is_unreachable(else_expr_result))
...@@ -6863,6 +6875,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *...@@ -6863,6 +6875,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
68636875
6864 ResultLocPeerParent *peer_parent = allocate<ResultLocPeerParent>(1);6876 ResultLocPeerParent *peer_parent = allocate<ResultLocPeerParent>(1);
6865 peer_parent->base.id = ResultLocIdPeerParent;6877 peer_parent->base.id = ResultLocIdPeerParent;
6878 peer_parent->is_comptime = is_comptime;
6866 peer_parent->parent = result_loc;6879 peer_parent->parent = result_loc;
6867 peer_parent->peers = allocate<ResultLocPeer>(prong_count);6880 peer_parent->peers = allocate<ResultLocPeer>(prong_count);
6868 peer_parent->peer_count = 0;6881 peer_parent->peer_count = 0;
...@@ -7311,7 +7324,8 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode...@@ -7311,7 +7324,8 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
7311 IrBasicBlock *end_block = ir_create_basic_block(irb, parent_scope, "UnwrapErrEnd");7324 IrBasicBlock *end_block = ir_create_basic_block(irb, parent_scope, "UnwrapErrEnd");
7312 IrInstruction *cond_br_inst = ir_build_cond_br(irb, parent_scope, node, is_err, err_block, ok_block, is_comptime);7325 IrInstruction *cond_br_inst = ir_build_cond_br(irb, parent_scope, node, is_err, err_block, ok_block, is_comptime);
73137326
7314 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, ok_block, end_block, result_loc);7327 ResultLocPeerParent *peer_parent = create_binary_result_peers(cond_br_inst, ok_block, end_block, result_loc,
7328 is_comptime);
73157329
7316 ir_set_cursor_at_end_and_append_block(irb, err_block);7330 ir_set_cursor_at_end_and_append_block(irb, err_block);
7317 Scope *err_scope;7331 Scope *err_scope;
...@@ -14880,8 +14894,10 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s...@@ -14880,8 +14894,10 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
14880 ResultLocPeer *result_peer = reinterpret_cast<ResultLocPeer *>(result_loc);14894 ResultLocPeer *result_peer = reinterpret_cast<ResultLocPeer *>(result_loc);
14881 ResultLocPeerParent *peer_parent = result_peer->parent;14895 ResultLocPeerParent *peer_parent = result_peer->parent;
1488214896
14883 if (ira->const_predecessor_bb)14897 bool is_comptime;
14884 return nullptr;14898 if (!ir_resolve_comptime(ira, peer_parent->is_comptime->child, &is_comptime))
14899 return ira->codegen->invalid_instruction;
14900 if (is_comptime) return nullptr;
1488514901
14886 if (peer_parent->resolved_type == nullptr) {14902 if (peer_parent->resolved_type == nullptr) {
14887 ResultLocPeer *last_peer = &peer_parent->peers[peer_parent->peer_count - 1];14903 ResultLocPeer *last_peer = &peer_parent->peers[peer_parent->peer_count - 1];
...@@ -16357,6 +16373,11 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh...@@ -16357,6 +16373,11 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
1635716373
16358 ResultLocPeerParent *peer_parent = phi_instruction->peer_parent;16374 ResultLocPeerParent *peer_parent = phi_instruction->peer_parent;
16359 if (peer_parent != nullptr && peer_parent->resolved_type == nullptr) {16375 if (peer_parent != nullptr && peer_parent->resolved_type == nullptr) {
16376 bool is_comptime;
16377 if (!ir_resolve_comptime(ira, peer_parent->is_comptime->child, &is_comptime))
16378 return ira->codegen->invalid_instruction;
16379 if (is_comptime) goto skip_peer_stuff;
16380
16360 // Suspend the phi first so that it gets resumed last16381 // Suspend the phi first so that it gets resumed last
16361 IrSuspendPosition suspend_pos;16382 IrSuspendPosition suspend_pos;
16362 ira_suspend(ira, &phi_instruction->base, nullptr, &suspend_pos);16383 ira_suspend(ira, &phi_instruction->base, nullptr, &suspend_pos);
...@@ -16393,6 +16414,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh...@@ -16393,6 +16414,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
1639316414
16394 return ira_resume(ira);16415 return ira_resume(ira);
16395 }16416 }
16417skip_peer_stuff:
1639616418
16397 ZigList<IrBasicBlock*> new_incoming_blocks = {0};16419 ZigList<IrBasicBlock*> new_incoming_blocks = {0};
16398 ZigList<IrInstruction*> new_incoming_values = {0};16420 ZigList<IrInstruction*> new_incoming_values = {0};
std/special/bootstrap.zig+4-4
...@@ -114,20 +114,20 @@ extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 {...@@ -114,20 +114,20 @@ extern fn main(c_argc: i32, c_argv: [*][*]u8, c_envp: [*]?[*]u8) i32 {
114// and we want fewer call frames in stack traces.114// and we want fewer call frames in stack traces.
115inline fn callMain() u8 {115inline fn callMain() u8 {
116 switch (@typeId(@typeOf(root.main).ReturnType)) {116 switch (@typeId(@typeOf(root.main).ReturnType)) {
117 builtin.TypeId.NoReturn => {117 .NoReturn => {
118 root.main();118 root.main();
119 },119 },
120 builtin.TypeId.Void => {120 .Void => {
121 root.main();121 root.main();
122 return 0;122 return 0;
123 },123 },
124 builtin.TypeId.Int => {124 .Int => {
125 if (@typeOf(root.main).ReturnType.bit_count != 8) {125 if (@typeOf(root.main).ReturnType.bit_count != 8) {
126 @compileError("expected return type of main to be 'u8', 'noreturn', 'void', or '!void'");126 @compileError("expected return type of main to be 'u8', 'noreturn', 'void', or '!void'");
127 }127 }
128 return root.main();128 return root.main();
129 },129 },
130 builtin.TypeId.ErrorUnion => {130 .ErrorUnion => {
131 root.main() catch |err| {131 root.main() catch |err| {
132 std.debug.warn("error: {}\n", @errorName(err));132 std.debug.warn("error: {}\n", @errorName(err));
133 if (builtin.os != builtin.Os.zen) {133 if (builtin.os != builtin.Os.zen) {
std/special/panic.zig+3-3
...@@ -7,8 +7,8 @@ const builtin = @import("builtin");...@@ -7,8 +7,8 @@ const builtin = @import("builtin");
7const std = @import("std");7const std = @import("std");
88
9pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {9pub fn panic(msg: []const u8, error_return_trace: ?*builtin.StackTrace) noreturn {
10 const stderr = std.io.getStdErr() catch std.process.abort();10 //const stderr = std.io.getStdErr() catch std.process.abort();
11 stderr.write("panic: ") catch std.process.abort();11 //stderr.write("panic: ") catch std.process.abort();
12 stderr.write(msg) catch std.process.abort();12 //stderr.write(msg) catch std.process.abort();
13 std.process.abort();13 std.process.abort();
14}14}