Skip to content

Commit d7ac881

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 155630c commit d7ac881

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
@@ -908,11 +908,13 @@ let switch lam (lam_switch : lambda_switch) : t =
908908
match untagged_action facts cst with
909909
| `Case action -> action_or_switch action
910910
| `Undecided -> Lswitch (lam, lam_switch))
911-
| Lconst (Const_constructor cstr_name), _ ->
911+
| Lconst (Const_constructor constant), _ ->
912+
(* By runtime value: a coercion accepts [@as(1.)] for [@as(1.0)] *)
912913
let action =
913914
Ext_list.find_opt lam_switch.sw_consts (fun (key, action) ->
914915
match key with
915-
| Switch_constructor (Constant tag) when cstr_name = tag ->
916+
| Switch_constructor (Constant tag)
917+
when literal_denotes_same constant.literal tag.literal ->
916918
Some action
917919
| Switch_int _ | Switch_constructor _ -> None)
918920
in
@@ -940,13 +942,21 @@ let switch lam (lam_switch : lambda_switch) : t =
940942
| Blk_module _ | Blk_module_export _ | Blk_extension ->
941943
None
942944
in
945+
let same_runtime (block : Variant_runtime.block)
946+
(case : Variant_runtime.block) =
947+
match (block, case) with
948+
| Tagged block, Tagged case ->
949+
block.tag_name = case.tag_name
950+
&& literal_denotes_same block.tag.literal case.tag.literal
951+
| _ -> block = case
952+
in
943953
let action =
944954
Ext_list.find_opt lam_switch.sw_blocks (fun (key, action) ->
945-
match key with
946-
| Switch_constructor (Block case_runtime)
947-
when runtime = Some case_runtime ->
955+
match (key, runtime) with
956+
| Switch_constructor (Block case_runtime), Some runtime
957+
when same_runtime runtime case_runtime ->
948958
Some action
949-
| Switch_int _ | Switch_constructor _ -> None)
959+
| (Switch_int _ | Switch_constructor _), _ -> None)
950960
in
951961
action_or_switch action
952962
| _ -> 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)