authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-20 00:08:26-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-20 00:20:29-07:00
log7d9e3840bb668f2040b30480deadf8c1a4cad4b0
tree7d6ecc5f0207eefe0850ac7ee5047519f9fc5980
parent01638c250fa270c8cb3b3416ea55e22988cd32a2

Sema: fix inline break from a non-comptime scope to outer one

Prior to this, the compiler would hit an assertion because the break_inline would not successfully move the compile-time control flow.

3 files changed, 23 insertions(+), 6 deletions(-)

src/Sema.zig+22-3
......@@ -964,7 +964,14 @@ fn analyzeBodyInner(
964964 break sema.zirBreak(block, inst);
965965 }
966966 },
967 .break_inline => break inst,
967 .break_inline => {
968 if (block.is_comptime) {
969 break inst;
970 } else {
971 sema.comptime_break_inst = inst;
972 return error.ComptimeBreak;
973 }
974 },
968975 .repeat => {
969976 if (block.is_comptime) {
970977 // Send comptime control flow back to the beginning of this block.
......@@ -3572,8 +3579,20 @@ fn resolveBlockBody(
35723579 if (child_block.is_comptime) {
35733580 return sema.resolveBody(child_block, body, body_inst);
35743581 } else {
3575 _ = try sema.analyzeBody(child_block, body);
3576 return sema.analyzeBlockBody(parent_block, src, child_block, merges);
3582 if (sema.analyzeBodyInner(child_block, body)) |_| {
3583 return sema.analyzeBlockBody(parent_block, src, child_block, merges);
3584 } else |err| switch (err) {
3585 error.ComptimeBreak => {
3586 const break_inst = sema.comptime_break_inst;
3587 const break_data = sema.code.instructions.items(.data)[break_inst].@"break";
3588 if (break_data.block_inst == body_inst) {
3589 return sema.resolveInst(break_data.operand);
3590 } else {
3591 return error.ComptimeBreak;
3592 }
3593 },
3594 else => |e| return e,
3595 }
35773596 }
35783597}
35793598
src/type.zig+1-1
......@@ -2986,7 +2986,7 @@ pub const Type = extern union {
29862986
29872987 pub fn containerLayout(ty: Type) std.builtin.TypeInfo.ContainerLayout {
29882988 return switch (ty.tag()) {
2989 .tuple => .Auto,
2989 .tuple, .empty_struct_literal => .Auto,
29902990 .@"struct" => ty.castTag(.@"struct").?.data.layout,
29912991 .@"union" => ty.castTag(.@"union").?.data.layout,
29922992 .union_tagged => ty.castTag(.union_tagged).?.data.layout,
test/behavior/eval.zig-2
......@@ -763,8 +763,6 @@ fn scalar(x: u32) u32 {
763763}
764764
765765test "comptime assign int to optional int" {
766 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
767
768766 comptime {
769767 var x: ?i32 = null;
770768 x = 2;