From c4df9bf56f204f63f4e87255933ba453d69d0182 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 2 Oct 2021 20:15:03 -0700 Subject: AstGen: fix `while` and `for` with unreachable bodies Companion commit to 61a53a587558ff1fe1b0ec98bb424022885edccf. This commit also moves over a bunch of behavior test cases to the passing-for-stage2 section. --- src/codegen/llvm.zig | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/codegen') diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 761dd2a8bc..500aa482fa 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -1605,7 +1605,15 @@ pub const FuncGen = struct { self.builder.positionBuilderAtEnd(loop_block); try self.genBody(body); - _ = self.builder.buildBr(loop_block); + // TODO instead of this logic, change AIR to have the property that + // every block is guaranteed to end with a noreturn instruction. + // Then we can simply rely on the fact that a repeat or break instruction + // would have been emitted already. Also the main loop in genBody can + // be while(true) instead of for(body), which will eliminate 1 branch on + // a hot path. + if (body.len == 0 or !self.air.typeOfIndex(body[body.len - 1]).isNoReturn()) { + _ = self.builder.buildBr(loop_block); + } return null; } -- cgit v1.2.3