fix(issue-0057): all-diverging match arms no longer fail LLVM verification
A match (`if subject == { case ... }`) whose arms all diverge (each
`return`s / `raise`s) failed LLVM verification with a `void` phi plus
"Terminator found in the middle of a basic block". Two causes in lowerMatch:
- The value-arm path did `lowerBlockValue(arm.body) orelse constInt(0, …)`,
emitting the fallback `const` into a block the body had ALREADY terminated
(a diverging arm), so `currentBlockHasTerminator()` then saw the const (not
the `ret`) and emitted a `br merge` after the terminator. Fix: materialize
the fallback value + branch only when the block hasn't terminated.
- A fully-diverging match infers `result_type == .noreturn` yet still built a
value-merge phi. Fix: `has_value_merge` excludes `.noreturn`, so such a
match builds no phi; its arms terminate and the merge block is unreachable.
Also: inferMatchResultType now skips `.noreturn` arms (a diverging arm doesn't
decide the result type) and reports `.noreturn` only when EVERY arm diverges —
so a mixed match (some arms yield values, some diverge) infers the value type.
This unblocks ERR E1.5's `catch` match-body form (`x catch e == { case .A:
return …; else: raise e; }`), which desugars to an all-diverging match.
Regression: examples/225-match-diverging-arms.sx (all-diverging + mixed,
exit 134). Gates: zig build, zig build test, 263/263 examples.
This commit is contained in:
40
examples/225-match-diverging-arms.sx
Normal file
40
examples/225-match-diverging-arms.sx
Normal file
@@ -0,0 +1,40 @@
|
||||
// Regression for issue 0057: a match (`if subject == { case ... }`) whose arms
|
||||
// ALL diverge (each `return`s) used to fail LLVM verification (a `void` phi +
|
||||
// "terminator in the middle of a basic block") — lowerMatch emitted a
|
||||
// value-merge phi and a fallback `const` into arm blocks that had already
|
||||
// terminated. Now a fully-diverging match produces no merge phi, and a mixed
|
||||
// match (some arms diverge, some yield values) materializes the merge only
|
||||
// from the value-producing arms.
|
||||
|
||||
#import "modules/std.sx";
|
||||
|
||||
// All arms diverge — the match is `noreturn`, no merge phi.
|
||||
classify :: (n: s32) -> s32 {
|
||||
if n == {
|
||||
case 0: return 10;
|
||||
case 1: return 20;
|
||||
else: return 90;
|
||||
}
|
||||
return 0; // unreachable
|
||||
}
|
||||
|
||||
// Mixed: value arms + a diverging arm.
|
||||
pick :: (n: s32) -> s32 {
|
||||
v := if n == {
|
||||
case 0: 1;
|
||||
case 1: return 100; // diverging arm — no fallback const after its `ret`
|
||||
else: 3;
|
||||
};
|
||||
return v + 5;
|
||||
}
|
||||
|
||||
main :: () -> s32 {
|
||||
r : s32 = 0;
|
||||
r = r + classify(0); // 10
|
||||
r = r + classify(1); // 20
|
||||
r = r + classify(7); // 90
|
||||
r = r + pick(0); // 1 + 5 = 6
|
||||
r = r + pick(9); // 3 + 5 = 8 (else arm)
|
||||
print("match result: {}\n", r); // 10+20+90+6+8 = 134
|
||||
return r;
|
||||
}
|
||||
Reference in New Issue
Block a user