issues/0066: RESOLVED — match-value arms lowered against result type
A value-position match's arms are now lowered with `target_type` set to
the merge's `result_type`, so positive and negated integer literals pick
the same width. Fixes the `PHI node operands are not the same type as the
result` failure for `if n == { case 0: 100; else: -1; }`-style returns.
Regression: examples/0043-basic-match-value-mixed-width.sx.
Gates: zig build, zig build test, run_examples.sh -> 345 passed.
This commit is contained in:
31
examples/0043-basic-match-value-mixed-width.sx
Normal file
31
examples/0043-basic-match-value-mixed-width.sx
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
// A value-position match (`if subject == { case … }`) returning a small
|
||||||
|
// integer type works when arms mix positive and negated literals: every arm
|
||||||
|
// value is lowered against the merge's result type, so the phi operands all
|
||||||
|
// share one width.
|
||||||
|
//
|
||||||
|
// Regression (issue 0066): a negated-literal arm (`else: -1`) previously
|
||||||
|
// lowered at a narrower width than the positive arms, tripping LLVM's
|
||||||
|
// "PHI node operands are not the same type as the result".
|
||||||
|
|
||||||
|
#import "modules/std.sx";
|
||||||
|
|
||||||
|
sign :: (n: s32) -> s32 {
|
||||||
|
if n == {
|
||||||
|
case 0: 0;
|
||||||
|
else: if n > 0 then 1 else -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
classify :: (n: s32) -> s32 {
|
||||||
|
if n == {
|
||||||
|
case 0: 100;
|
||||||
|
case 1: 10;
|
||||||
|
else: -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main :: () -> s32 {
|
||||||
|
print("sign: {} {} {}\n", sign(-9), sign(0), sign(9)); // -1 0 1
|
||||||
|
print("classify: {} {} {}\n", classify(0), classify(1), classify(5)); // 100 10 -1
|
||||||
|
0
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
0
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
sign: -1 0 1
|
||||||
|
classify: 100 10 -1
|
||||||
@@ -1,5 +1,11 @@
|
|||||||
# 0066 — match-as-value with a negated-literal arm builds a mismatched phi
|
# 0066 — match-as-value with a negated-literal arm builds a mismatched phi
|
||||||
|
|
||||||
|
> **RESOLVED.** `lowerMatch`'s value path (`has_value_merge`) now lowers each
|
||||||
|
> arm body with `target_type = result_type`, so literals and negated literals in
|
||||||
|
> the arms pick the merge's width instead of leaking a narrower one. The phi
|
||||||
|
> operands are uniform; `coerceToType` still runs afterward as a backstop.
|
||||||
|
> Regression: `examples/0043-basic-match-value-mixed-width.sx`.
|
||||||
|
|
||||||
## Symptom
|
## Symptom
|
||||||
|
|
||||||
A value-position `match` (the `if subject == { case ... }` sugar) returning a
|
A value-position `match` (the `if subject == { case ... }` sugar) returning a
|
||||||
|
|||||||
@@ -4497,7 +4497,13 @@ pub const Lowering = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (has_value_merge) {
|
if (has_value_merge) {
|
||||||
|
// Lower the arm body against the merge's result type so literals
|
||||||
|
// (and negated literals) in the arm pick the right width — the
|
||||||
|
// phi operands must all match `result_type` (issue 0066).
|
||||||
|
const saved_arm_target = self.target_type;
|
||||||
|
self.target_type = result_type;
|
||||||
const maybe_v = self.lowerBlockValue(arm.body);
|
const maybe_v = self.lowerBlockValue(arm.body);
|
||||||
|
self.target_type = saved_arm_target;
|
||||||
self.current_match_tags = saved_match_tags;
|
self.current_match_tags = saved_match_tags;
|
||||||
self.scope = old_scope;
|
self.scope = old_scope;
|
||||||
arm_scope.deinit();
|
arm_scope.deinit();
|
||||||
|
|||||||
Reference in New Issue
Block a user