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
44restore test_runner.zig to master branch
55 - also the default panic function and unexpected_error_tracing. see the commit
66 that adds this text to BRANCH_TODO file.
7 - and std/specia/bootstrap.zig
78
89get an empty file compiling successfully (with no panic fn override)
910
src/all_types.hpp+1
......@@ -3656,6 +3656,7 @@ struct ResultLocPeerParent {
36563656 ResultLocPeer *peers;
36573657 size_t peer_count;
36583658 ZigType *resolved_type;
3659 IrInstruction *is_comptime;
36593660};
36603661
36613662struct IrSuspendPosition {
src/ir.cpp+34-12
......@@ -3885,11 +3885,12 @@ static IrInstruction *ir_gen_bool_and(IrBuilder *irb, Scope *scope, AstNode *nod
38853885}
38863886
38873887static 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)
38893889{
38903890 ResultLocPeerParent *peer_parent = allocate<ResultLocPeerParent>(1);
38913891 peer_parent->base.id = ResultLocIdPeerParent;
38923892 peer_parent->base.source_instruction = cond_br_inst;
3893 peer_parent->is_comptime = is_comptime;
38933894 peer_parent->parent = parent;
38943895 peer_parent->peer_count = 2;
38953896 peer_parent->peers = allocate<ResultLocPeer>(2);
......@@ -3931,7 +3932,8 @@ static IrInstruction *ir_gen_orelse(IrBuilder *irb, Scope *parent_scope, AstNode
39313932 IrBasicBlock *end_block = ir_create_basic_block(irb, parent_scope, "OptionalEnd");
39323933 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
39363938 ir_set_cursor_at_end_and_append_block(irb, null_block);
39373939 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
54385440 IrInstruction *cond_br_inst = ir_build_cond_br(irb, scope, condition->source_node, condition,
54395441 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
54435446 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
54595462 return irb->codegen->invalid_instruction;
54605463 } else {
54615464 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);
54625466 }
54635467 IrBasicBlock *after_else_block = irb->current_basic_block;
54645468 if (!instr_is_unreachable(else_expr_result))
......@@ -5930,7 +5934,8 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
59305934 cond_br_inst = is_err; // for the purposes of the source instruction to create_binary_result_peers
59315935 }
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
59355940 ir_set_cursor_at_end_and_append_block(irb, body_block);
59365941 if (var_symbol) {
......@@ -6031,7 +6036,8 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, Scope *scope, AstNode *n
60316036 cond_br_inst = is_non_null; // for the purposes of source instruction for create_binary_result_peers
60326037 }
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
60366042 ir_set_cursor_at_end_and_append_block(irb, body_block);
60376043 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
61136119 cond_br_inst = cond_val; // for the source instruction arg to create_binary_result_peers
61146120 }
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);
61176124 ir_set_cursor_at_end_and_append_block(irb, body_block);
61186125
61196126 ZigList<IrInstruction *> incoming_values = {0};
......@@ -6244,7 +6251,8 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, Scope *parent_scope, AstNo
62446251 IrInstruction *cond_br_inst = ir_mark_gen(ir_build_cond_br(irb, parent_scope, node, cond,
62456252 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
62496257 ir_set_cursor_at_end_and_append_block(irb, body_block);
62506258 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
66226630 IrInstruction *cond_br_inst = ir_build_cond_br(irb, scope, node, is_non_null,
66236631 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
66276636 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
66576666 return else_expr_result;
66586667 } else {
66596668 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);
66606670 }
66616671 IrBasicBlock *after_else_block = irb->current_basic_block;
66626672 if (!instr_is_unreachable(else_expr_result))
......@@ -6702,7 +6712,8 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
67026712 IrInstruction *is_comptime = force_comptime ? ir_build_const_bool(irb, scope, node, true) : ir_build_test_comptime(irb, scope, node, is_err);
67036713 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
67076718 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 *
67526763 return else_expr_result;
67536764 } else {
67546765 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);
67556767 }
67566768 IrBasicBlock *after_else_block = irb->current_basic_block;
67576769 if (!instr_is_unreachable(else_expr_result))
......@@ -6863,6 +6875,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
68636875
68646876 ResultLocPeerParent *peer_parent = allocate<ResultLocPeerParent>(1);
68656877 peer_parent->base.id = ResultLocIdPeerParent;
6878 peer_parent->is_comptime = is_comptime;
68666879 peer_parent->parent = result_loc;
68676880 peer_parent->peers = allocate<ResultLocPeer>(prong_count);
68686881 peer_parent->peer_count = 0;
......@@ -7311,7 +7324,8 @@ static IrInstruction *ir_gen_catch(IrBuilder *irb, Scope *parent_scope, AstNode
73117324 IrBasicBlock *end_block = ir_create_basic_block(irb, parent_scope, "UnwrapErrEnd");
73127325 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
73167330 ir_set_cursor_at_end_and_append_block(irb, err_block);
73177331 Scope *err_scope;
......@@ -14880,8 +14894,10 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
1488014894 ResultLocPeer *result_peer = reinterpret_cast<ResultLocPeer *>(result_loc);
1488114895 ResultLocPeerParent *peer_parent = result_peer->parent;
1488214896
14883 if (ira->const_predecessor_bb)
14884 return nullptr;
14897 bool is_comptime;
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
1488614902 if (peer_parent->resolved_type == nullptr) {
1488714903 ResultLocPeer *last_peer = &peer_parent->peers[peer_parent->peer_count - 1];
......@@ -16357,6 +16373,11 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
1635716373
1635816374 ResultLocPeerParent *peer_parent = phi_instruction->peer_parent;
1635916375 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
1636016381 // Suspend the phi first so that it gets resumed last
1636116382 IrSuspendPosition suspend_pos;
1636216383 ira_suspend(ira, &phi_instruction->base, nullptr, &suspend_pos);
......@@ -16393,6 +16414,7 @@ static IrInstruction *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionPh
1639316414
1639416415 return ira_resume(ira);
1639516416 }
16417skip_peer_stuff:
1639616418
1639716419 ZigList<IrBasicBlock*> new_incoming_blocks = {0};
1639816420 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 {
114114// and we want fewer call frames in stack traces.
115115inline fn callMain() u8 {
116116 switch (@typeId(@typeOf(root.main).ReturnType)) {
117 builtin.TypeId.NoReturn => {
117 .NoReturn => {
118118 root.main();
119119 },
120 builtin.TypeId.Void => {
120 .Void => {
121121 root.main();
122122 return 0;
123123 },
124 builtin.TypeId.Int => {
124 .Int => {
125125 if (@typeOf(root.main).ReturnType.bit_count != 8) {
126126 @compileError("expected return type of main to be 'u8', 'noreturn', 'void', or '!void'");
127127 }
128128 return root.main();
129129 },
130 builtin.TypeId.ErrorUnion => {
130 .ErrorUnion => {
131131 root.main() catch |err| {
132132 std.debug.warn("error: {}\n", @errorName(err));
133133 if (builtin.os != builtin.Os.zen) {
std/special/panic.zig+3-3
......@@ -7,8 +7,8 @@ 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();
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();
1313 std.process.abort();
1414}