Skip to content

Commit d42a132

Browse files
cknittclaude
andcommitted
Fold matches of constructors by their runtime literals
Why: A coercion accepts constructors whose literals are spelled differently but are the same value, such as `@as(1.)` for `@as(1.0)`, while a match on a constant constructor was folded by comparing tags structurally, so the coerced constant missed its arm. Constant constructors and tagged blocks are now found by the runtime value of their literal, as the untagged dispatch already does. JS modified (folded match of a coerced constant): Example: type source = | @as(1.) One type target = | @as(1.0) One | Two | Three | Four let isOne = (x: target) => switch x { | One => true | _ => false } let r = isOne((One: source :> target)) Before (js): let r = false; After (js): let r = true; Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 7fb8da3 commit d42a132

3 files changed

Lines changed: 56 additions & 8 deletions

File tree

‎compiler/ml/lambda.ml‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -909,11 +909,13 @@ let switch lam (lam_switch : lambda_switch) : t =
909909
match untagged_action facts cst with
910910
| `Case action -> action_or_switch action
911911
| `Undecided -> Lswitch (lam, lam_switch))
912-
| Lconst (Const_constructor cstr_name), _ ->
912+
| Lconst (Const_constructor constant), _ ->
913+
(* By runtime value: a coercion accepts [@as(1.)] for [@as(1.0)] *)
913914
let action =
914915
Ext_list.find_opt lam_switch.sw_consts (fun (key, action) ->
915916
match key with
916-
| Switch_constructor (Constant tag) when cstr_name = tag ->
917+
| Switch_constructor (Constant tag)
918+
when literal_denotes_same constant.literal tag.literal ->
917919
Some action
918920
| Switch_int _ | Switch_constructor _ -> None)
919921
in
@@ -941,13 +943,21 @@ let switch lam (lam_switch : lambda_switch) : t =
941943
| Blk_module _ | Blk_module_export _ | Blk_extension ->
942944
None
943945
in
946+
let same_runtime (block : Variant_runtime.block)
947+
(case : Variant_runtime.block) =
948+
match (block, case) with
949+
| Tagged block, Tagged case ->
950+
block.tag_name = case.tag_name
951+
&& literal_denotes_same block.tag.literal case.tag.literal
952+
| _ -> block = case
953+
in
944954
let action =
945955
Ext_list.find_opt lam_switch.sw_blocks (fun (key, action) ->
946-
match key with
947-
| Switch_constructor (Block case_runtime)
948-
when runtime = Some case_runtime ->
956+
match (key, runtime) with
957+
| Switch_constructor (Block case_runtime), Some runtime
958+
when same_runtime runtime case_runtime ->
949959
Some action
950-
| Switch_int _ | Switch_constructor _ -> None)
960+
| (Switch_int _ | Switch_constructor _), _ -> None)
951961
in
952962
action_or_switch action
953963
| _ -> Lswitch (lam, lam_switch)

‎tests/tests/src/as_constructor_name_test.mjs‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,29 @@ let Stated = {
77
a: "A"
88
};
99

10+
function isOne(x) {
11+
return x === 1.0;
12+
}
13+
14+
function isOneOf(x) {
15+
return x.TAG === 1.0;
16+
}
17+
1018
Mocha.describe("As_constructor_name_test", () => {
11-
Mocha.test("coercion", () => Test_utils.eq("File \"as_constructor_name_test.res\", line 22, characters 7-14", "A", "A"));
12-
Mocha.test("interface", () => Test_utils.eq("File \"as_constructor_name_test.res\", line 26, characters 7-14", "A", "A"));
19+
Mocha.test("coercion", () => Test_utils.eq("File \"as_constructor_name_test.res\", line 41, characters 7-14", "A", "A"));
20+
Mocha.test("folded matches of coerced constants", () => {
21+
Test_utils.eq("File \"as_constructor_name_test.res\", line 45, characters 7-14", true, true);
22+
Test_utils.eq("File \"as_constructor_name_test.res\", line 46, characters 7-14", true, true);
23+
});
24+
Mocha.test("interface", () => Test_utils.eq("File \"as_constructor_name_test.res\", line 50, characters 7-14", "A", "A"));
1325
});
1426

1527
let declaredA = "A";
1628

1729
export {
1830
declaredA,
1931
Stated,
32+
isOne,
33+
isOneOf,
2034
}
2135
/* Not a pure module */

‎tests/tests/src/as_constructor_name_test.res‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,35 @@ module Stated: {
1717
let a = A
1818
}
1919

20+
// Literals are compared by runtime value, also when a match is folded
21+
type sourceNumber = | @as(1.) One
22+
type targetNumber = | @as(1.0) One | Two | Three | Four
23+
24+
let isOne = (x: targetNumber) =>
25+
switch x {
26+
| One => true
27+
| _ => false
28+
}
29+
30+
type sourceBlock = | @as(1.) OneOf(int)
31+
type targetBlock = | @as(1.0) OneOf(int) | TwoOf(int)
32+
33+
let isOneOf = (x: targetBlock) =>
34+
switch x {
35+
| OneOf(_) => true
36+
| _ => false
37+
}
38+
2039
describe(__MODULE__, () => {
2140
test("coercion", () => {
2241
eq(__LOC__, declaredA, A)
2342
})
2443

44+
test("folded matches of coerced constants", () => {
45+
eq(__LOC__, isOne((One: sourceNumber) :> targetNumber), true)
46+
eq(__LOC__, isOneOf((OneOf(1): sourceBlock) :> targetBlock), true)
47+
})
48+
2549
test("interface", () => {
2650
eq(__LOC__, Stated.a, Stated.A)
2751
})

0 commit comments

Comments
 (0)