From b7b905d227c0124778214f7a307be04b7086ea6a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Tue, 6 Dec 2022 17:57:27 -0700 Subject: [PATCH] add behavior test for while(true) not needing else unreachable closes #707 --- lib/std/event/loop.zig | 2 +- lib/std/os.zig | 2 +- lib/std/zig/render.zig | 2 +- lib/std/zig/string_literal.zig | 2 +- src/Sema.zig | 14 +++++++------- src/print_air.zig | 2 +- src/translate_c.zig | 4 ++-- test/behavior/while.zig | 7 +++++++ 8 files changed, 21 insertions(+), 14 deletions(-) diff --git a/lib/std/event/loop.zig b/lib/std/event/loop.zig index 7350a06607c97bcd5694375ab586c06cb58d73d6..78ecb8c600fbd22de1b657aa32c4a4bfa2d3ea27 100644 --- a/lib/std/event/loop.zig +++ b/lib/std/event/loop.zig @@ -1469,7 +1469,7 @@ pub const Loop = struct { .Cancelled => continue, } if (overlapped) |o| break o; - } else unreachable; // TODO else unreachable should not be necessary + }; const resume_node = @fieldParentPtr(ResumeNode, "overlapped", overlapped); const handle = resume_node.handle; const resume_node_id = resume_node.id; diff --git a/lib/std/os.zig b/lib/std/os.zig index fc021be954e3e44279f9e678aa670464acf5512e..34a44beff33dff976e24e71863301a5972d25fa8 100644 --- a/lib/std/os.zig +++ b/lib/std/os.zig @@ -3621,7 +3621,7 @@ pub fn accept( else => |err| return unexpectedErrno(err), } } - } else unreachable; + }; if (!have_accept4) { try setSockFlags(accepted_sock, flags); diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 160ec13f0c1cc05accfba4342c08b484e29f03db..0c54b3c751361badedc8fbaddb2fbd5d8da4c39f 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -2211,7 +2211,7 @@ fn renderAsm( }, else => unreachable, } - } else unreachable; // TODO shouldn't need this on while(true) + } } fn renderCall( diff --git a/lib/std/zig/string_literal.zig b/lib/std/zig/string_literal.zig index 22b8f68fa7cf457be731d29485010848fd71ee00..4859c379a081f36ba87718f79799b4c6e180e9b1 100644 --- a/lib/std/zig/string_literal.zig +++ b/lib/std/zig/string_literal.zig @@ -266,7 +266,7 @@ pub fn parseWrite(writer: anytype, bytes: []const u8) error{OutOfMemory}!Result index += 1; }, } - } else unreachable; // TODO should not need else unreachable on while(true) + } } /// Higher level API. Does not return extra info about parse errors. diff --git a/src/Sema.zig b/src/Sema.zig index ecabd7b65edf3193e3e0c6d8ee4ee6427a9acb41..34732a18228ab421506eb88c86bdcd5565cd04f1 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -1359,7 +1359,7 @@ fn analyzeBodyInner( break check_block.runtime_index; } check_block = check_block.parent.?; - } else unreachable; + }; if (@enumToInt(target_runtime_index) < @enumToInt(block.runtime_index)) { const runtime_src = block.runtime_cond orelse block.runtime_loop.?; @@ -1733,7 +1733,7 @@ fn analyzeBodyInner( break always_noreturn; map.putAssumeCapacity(inst, air_inst); i += 1; - } else unreachable; + }; // balance out dbg_block_begins in case of early noreturn const noreturn_inst = block.instructions.popOrNull(); @@ -3478,7 +3478,7 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro .store => break candidate, else => break :ct, } - } else unreachable; // TODO shouldn't need this + }; while (true) { if (search_index == 0) break :ct; @@ -3704,7 +3704,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com .store => break candidate, else => break :ct, } - } else unreachable; // TODO shouldn't need this + }; const bitcast_inst = while (true) { if (search_index == 0) break :ct; @@ -3716,7 +3716,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com .bitcast => break candidate, else => break :ct, } - } else unreachable; // TODO shouldn't need this + }; const const_inst = while (true) { if (search_index == 0) break :ct; @@ -3728,7 +3728,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com .constant => break candidate, else => break :ct, } - } else unreachable; // TODO shouldn't need this + }; const store_op = air_datas[store_inst].bin_op; const store_val = (try sema.resolveMaybeUndefVal(store_op.rhs)) orelse break :ct; @@ -14964,7 +14964,7 @@ fn zirClosureGet( break tv; } scope = scope.parent.?; - } else unreachable; + }; if (tv.val.tag() == .unreachable_value and !block.is_typeof and sema.func == null) { const msg = msg: { diff --git a/src/print_air.zig b/src/print_air.zig index 86ba81e110f4e18235ddb87d56f87c38af08eed7..671f781e5ed72661f095178898e65eb0c69af878 100644 --- a/src/print_air.zig +++ b/src/print_air.zig @@ -848,7 +848,7 @@ const Writer = struct { if ((bits >> 31) != 0) break :blk false; extra_index += 1; tomb_op_index += 31; - } else unreachable; + } }; return w.writeInstRef(s, operand, dies); } diff --git a/src/translate_c.zig b/src/translate_c.zig index d4b2e0c709fc75c067c529865201c3fe3784513d..3ec5483b364caa6681def7ae1412dd6fdd442362 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -523,7 +523,7 @@ fn declVisitorNamesOnly(c: *Context, decl: *const clang.Decl) Error!void { child_ty = macroqualified_ty.getModifiedType().getTypePtr(); }, else => return, - } else unreachable; + }; const result = try c.unnamed_typedefs.getOrPut(c.gpa, addr); if (result.found_existing) { @@ -629,7 +629,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void { }, else => break fn_type, } - } else unreachable; + }; const fn_ty = @ptrCast(*const clang.FunctionType, fn_type); const return_qt = fn_ty.getReturnType(); diff --git a/test/behavior/while.zig b/test/behavior/while.zig index a54563bb8356262f332d1294913a97380d785b5c..2496bf0c38b6934ccdcc068fe0c2dfc9140b7005 100644 --- a/test/behavior/while.zig +++ b/test/behavior/while.zig @@ -364,3 +364,10 @@ test "try terminating an infinite loop" { _ = try Foo.bar(); } else unreachable); } + +test "while loop with comptime true condition needs no else block to return value with break" { + const x = while (true) { + break @as(u32, 69); + }; + try expect(x == 69); +} -- 2.54.0