authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-06 17:57:27-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-12-06 17:57:27-07:00
logb7b905d227c0124778214f7a307be04b7086ea6a
tree315e50ccc6de51722770042967ba86e655e9fc71
parente7d28344fa3ee81d6ad7ca5ce1f83d50d8502118

add behavior test for while(true) not needing else unreachable

closes #707

8 files changed, 21 insertions(+), 14 deletions(-)

lib/std/event/loop.zig+1-1
......@@ -1469,7 +1469,7 @@ pub const Loop = struct {
14691469 .Cancelled => continue,
14701470 }
14711471 if (overlapped) |o| break o;
1472 } else unreachable; // TODO else unreachable should not be necessary
1472 };
14731473 const resume_node = @fieldParentPtr(ResumeNode, "overlapped", overlapped);
14741474 const handle = resume_node.handle;
14751475 const resume_node_id = resume_node.id;
lib/std/os.zig+1-1
......@@ -3621,7 +3621,7 @@ pub fn accept(
36213621 else => |err| return unexpectedErrno(err),
36223622 }
36233623 }
3624 } else unreachable;
3624 };
36253625
36263626 if (!have_accept4) {
36273627 try setSockFlags(accepted_sock, flags);
lib/std/zig/render.zig+1-1
......@@ -2211,7 +2211,7 @@ fn renderAsm(
22112211 },
22122212 else => unreachable,
22132213 }
2214 } else unreachable; // TODO shouldn't need this on while(true)
2214 }
22152215}
22162216
22172217fn renderCall(
lib/std/zig/string_literal.zig+1-1
......@@ -266,7 +266,7 @@ pub fn parseWrite(writer: anytype, bytes: []const u8) error{OutOfMemory}!Result
266266 index += 1;
267267 },
268268 }
269 } else unreachable; // TODO should not need else unreachable on while(true)
269 }
270270}
271271
272272/// Higher level API. Does not return extra info about parse errors.
src/Sema.zig+7-7
......@@ -1359,7 +1359,7 @@ fn analyzeBodyInner(
13591359 break check_block.runtime_index;
13601360 }
13611361 check_block = check_block.parent.?;
1362 } else unreachable;
1362 };
13631363
13641364 if (@enumToInt(target_runtime_index) < @enumToInt(block.runtime_index)) {
13651365 const runtime_src = block.runtime_cond orelse block.runtime_loop.?;
......@@ -1733,7 +1733,7 @@ fn analyzeBodyInner(
17331733 break always_noreturn;
17341734 map.putAssumeCapacity(inst, air_inst);
17351735 i += 1;
1736 } else unreachable;
1736 };
17371737
17381738 // balance out dbg_block_begins in case of early noreturn
17391739 const noreturn_inst = block.instructions.popOrNull();
......@@ -3478,7 +3478,7 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro
34783478 .store => break candidate,
34793479 else => break :ct,
34803480 }
3481 } else unreachable; // TODO shouldn't need this
3481 };
34823482
34833483 while (true) {
34843484 if (search_index == 0) break :ct;
......@@ -3704,7 +3704,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
37043704 .store => break candidate,
37053705 else => break :ct,
37063706 }
3707 } else unreachable; // TODO shouldn't need this
3707 };
37083708
37093709 const bitcast_inst = while (true) {
37103710 if (search_index == 0) break :ct;
......@@ -3716,7 +3716,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
37163716 .bitcast => break candidate,
37173717 else => break :ct,
37183718 }
3719 } else unreachable; // TODO shouldn't need this
3719 };
37203720
37213721 const const_inst = while (true) {
37223722 if (search_index == 0) break :ct;
......@@ -3728,7 +3728,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com
37283728 .constant => break candidate,
37293729 else => break :ct,
37303730 }
3731 } else unreachable; // TODO shouldn't need this
3731 };
37323732
37333733 const store_op = air_datas[store_inst].bin_op;
37343734 const store_val = (try sema.resolveMaybeUndefVal(store_op.rhs)) orelse break :ct;
......@@ -14964,7 +14964,7 @@ fn zirClosureGet(
1496414964 break tv;
1496514965 }
1496614966 scope = scope.parent.?;
14967 } else unreachable;
14967 };
1496814968
1496914969 if (tv.val.tag() == .unreachable_value and !block.is_typeof and sema.func == null) {
1497014970 const msg = msg: {
src/print_air.zig+1-1
......@@ -848,7 +848,7 @@ const Writer = struct {
848848 if ((bits >> 31) != 0) break :blk false;
849849 extra_index += 1;
850850 tomb_op_index += 31;
851 } else unreachable;
851 }
852852 };
853853 return w.writeInstRef(s, operand, dies);
854854 }
src/translate_c.zig+2-2
......@@ -523,7 +523,7 @@ fn declVisitorNamesOnly(c: *Context, decl: *const clang.Decl) Error!void {
523523 child_ty = macroqualified_ty.getModifiedType().getTypePtr();
524524 },
525525 else => return,
526 } else unreachable;
526 };
527527
528528 const result = try c.unnamed_typedefs.getOrPut(c.gpa, addr);
529529 if (result.found_existing) {
......@@ -629,7 +629,7 @@ fn visitFnDecl(c: *Context, fn_decl: *const clang.FunctionDecl) Error!void {
629629 },
630630 else => break fn_type,
631631 }
632 } else unreachable;
632 };
633633 const fn_ty = @ptrCast(*const clang.FunctionType, fn_type);
634634 const return_qt = fn_ty.getReturnType();
635635
test/behavior/while.zig+7
......@@ -364,3 +364,10 @@ test "try terminating an infinite loop" {
364364 _ = try Foo.bar();
365365 } else unreachable);
366366}
367
368test "while loop with comptime true condition needs no else block to return value with break" {
369 const x = while (true) {
370 break @as(u32, 69);
371 };
372 try expect(x == 69);
373}