Sema: fix analyzeBlockBody logic

Previously, when a coercion needed to be inserted into a break
instruction, the `br` AIR instruction would be rewritten so that the
block operand was a sub-block that did the coercion. The problem is that
the sub-block itself was never added to the parent block, resulting in
the `br` instruction operand being a bad reference.

Now, the `br` AIR instruction that needs to have coercion instructions
added is replaced with the sub-block itself with type `noreturn`, and
then the sub-block has the coercion instructions and a new `br`
instruction that breaks from the original block.

LLVM backend needed to be fixed to lower `noreturn` blocks without
emitting an unused LLVM basic block.
This commit is contained in:
Andrew Kelley
2021-11-26 23:17:01 -07:00
parent d43ebf562d
commit f0deef1d79
5 changed files with 51 additions and 47 deletions

View File

@@ -136,3 +136,26 @@ test "unwrap function call with optional pointer return value" {
try S.entry();
comptime try S.entry();
}
test "nested orelse" {
const S = struct {
fn entry() !void {
try expect(func() == null);
}
fn maybe() ?Foo {
return null;
}
fn func() ?Foo {
const x = maybe() orelse
maybe() orelse
return null;
_ = x;
unreachable;
}
const Foo = struct {
field: i32,
};
};
try S.entry();
comptime try S.entry();
}