Skip to content

Commit 7b968f3

Browse files
cknittclaude
andcommitted
Keep callable instances out of a function arm
Why: A class can extend Function, so its instances have typeof "function". The instance cases without an arm were only checked before the typeof switch when it had an object case, so a callable instance falling to the default was taken by a function arm. The instance cases without an arm are now checked whenever the typeof switch has an object or a function case. Generated code changed (a callable class in the default): Example: @instanceof("Callable") type callable @unboxed type t = C(callable) | D(animal) | F(int => int) | S(string) | N(float) | B(bool) let f = v => switch v { | F(_) => "function" | S(_) => "string" | _ => "other" } Before (JS): switch (typeof v) { case "function" : return "function"; case "string" : return "string"; default: return "other"; } After (JS): if (v instanceof Animal || v instanceof Callable) { return "other"; } switch (typeof v) { case "function" : return "function"; case "string" : return "string"; default: return "other"; } 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 ac36340 commit 7b968f3

3 files changed

Lines changed: 74 additions & 29 deletions

File tree

‎compiler/core/lam_compile.ml‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -882,20 +882,23 @@ let compile output_prefix =
882882
let not_typeof_clauses, typeof_clauses =
883883
List.partition clause_is_not_typeof clauses
884884
in
885-
let has_object_typeof =
885+
let has_typeof kind =
886886
List.exists
887887
(function
888-
| Variant_runtime.Payload_shape ObjectType, _ -> true
888+
| Variant_runtime.Payload_shape block_type, _ -> block_type = kind
889889
| _ -> false)
890890
typeof_clauses
891891
in
892+
let has_object_typeof = has_typeof ObjectType in
892893
(* When there's an ObjectType typeof case, null, arrays and class
893894
instances can incorrectly match it (typeof null === typeof [] ===
894-
"object"). Guard against them when they should fall through to
895-
default: null, and the instance cases without an arm of their own. *)
895+
"object"), and class instances can match a FunctionType case, since
896+
a class can extend Function. Guard against them when they should fall
897+
through to default: null, and the instance cases without an arm of
898+
their own. *)
896899
let needs_null_guard = has_object_typeof && has_null_case in
897900
let unmatched_instances =
898-
if not has_object_typeof then []
901+
if not (has_object_typeof || has_typeof FunctionType) then []
899902
else
900903
Ext_list.filter_map block_cases (function
901904
| Variant_runtime.InstanceType instance

‎tests/tests/src/untagged_runtime_annotations_test.mjs‎

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ function handlerKind(handler) {
9494
}
9595
}
9696

97+
function wideHandlerKind(handler) {
98+
if (handler instanceof UntaggedAnimal || handler instanceof UntaggedCallable) {
99+
return "other";
100+
}
101+
switch (typeof handler) {
102+
case "function" :
103+
return "function";
104+
case "string" :
105+
return "name";
106+
default:
107+
return "other";
108+
}
109+
}
110+
97111
function objectOrNameKind(value) {
98112
if (value instanceof UntaggedDog) {
99113
return "dog";
@@ -104,54 +118,59 @@ function objectOrNameKind(value) {
104118

105119
Mocha.describe("Untagged_runtime_annotations_test", () => {
106120
Mocha.test("a subclass case is not taken for its superclass", () => {
107-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 148, characters 7-14", "animal", describePet(new UntaggedAnimal()));
108-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 149, characters 7-14", "dog", describePet(new UntaggedDog()));
109-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 150, characters 7-14", "rex", "rex");
121+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 165, characters 7-14", "animal", describePet(new UntaggedAnimal()));
122+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 166, characters 7-14", "dog", describePet(new UntaggedDog()));
123+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 167, characters 7-14", "rex", "rex");
110124
});
111125
Mocha.test("a superclass case alone excludes the subclass", () => {
112-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 154, characters 7-14", "animal", onlyAnimal(new UntaggedAnimal()));
113-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 155, characters 7-14", "other", onlyAnimal(new UntaggedDog()));
126+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 171, characters 7-14", "animal", onlyAnimal(new UntaggedAnimal()));
127+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 172, characters 7-14", "other", onlyAnimal(new UntaggedDog()));
114128
});
115129
Mocha.test("declaration order does not matter", () => {
116-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 159, characters 7-14", "dog", describeReversed(new UntaggedDog()));
117-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 160, characters 7-14", "animal", describeReversed(new UntaggedAnimal()));
130+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 176, characters 7-14", "dog", describeReversed(new UntaggedDog()));
131+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 177, characters 7-14", "animal", describeReversed(new UntaggedAnimal()));
118132
});
119133
Mocha.test("a subclass upcasts to its superclass", () => {
120134
let animal = new UntaggedDog();
121-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 165, characters 7-14", "dog", describePet(animal));
135+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 182, characters 7-14", "dog", describePet(animal));
122136
});
123137
Mocha.test("classes reached through a module alias", () => {
124-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 169, characters 7-14", "animal", describeAliased(new UntaggedAnimal()));
125-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 170, characters 7-14", "dog", describeAliased(new UntaggedDog()));
138+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 186, characters 7-14", "animal", describeAliased(new UntaggedAnimal()));
139+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 187, characters 7-14", "dog", describeAliased(new UntaggedDog()));
126140
});
127141
Mocha.test("typeOf symbol", () => {
128-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 174, characters 7-14", "symbol", keyKind(Symbol.for("key")));
129-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 175, characters 7-14", "text", "text");
142+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 191, characters 7-14", "symbol", keyKind(Symbol.for("key")));
143+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 192, characters 7-14", "text", "text");
130144
});
131145
Mocha.test("typeOf string", () => {
132-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 179, characters 7-14", "id", "id");
133-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 180, characters 7-14", "index", "index");
146+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 196, characters 7-14", "id", "id");
147+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 197, characters 7-14", "index", "index");
134148
});
135149
Mocha.test("a class at a dotted path", () => {
136-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 184, characters 7-14", "locale", localeKind(new Intl.Locale("en")));
137-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 185, characters 7-14", "de", "de");
150+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 201, characters 7-14", "locale", localeKind(new Intl.Locale("en")));
151+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 202, characters 7-14", "de", "de");
138152
});
139153
Mocha.test("an annotated type is not wrapped in an option", () => {
140154
let animal = new UntaggedAnimal();
141-
Test_utils.ok("File \"untagged_runtime_annotations_test.res\", line 190, characters 7-14", animal === animal);
142-
Test_utils.ok("File \"untagged_runtime_annotations_test.res\", line 191, characters 7-14", animal === animal);
155+
Test_utils.ok("File \"untagged_runtime_annotations_test.res\", line 207, characters 7-14", animal === animal);
156+
Test_utils.ok("File \"untagged_runtime_annotations_test.res\", line 208, characters 7-14", animal === animal);
143157
});
144158
Mocha.test("a phantom type hierarchy", () => {
145-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 195, characters 7-14", "animal", describePhantom(new UntaggedAnimal()));
146-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 196, characters 7-14", "dog", describePhantom(new UntaggedDog()));
159+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 212, characters 7-14", "animal", describePhantom(new UntaggedAnimal()));
160+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 213, characters 7-14", "dog", describePhantom(new UntaggedDog()));
147161
});
148162
Mocha.test("a function case excludes class instances", () => {
149-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 200, characters 7-14", "callable", handlerKind(new UntaggedCallable()));
150-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 201, characters 7-14", "function", handlerKind(x => x));
163+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 217, characters 7-14", "callable", handlerKind(new UntaggedCallable()));
164+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 218, characters 7-14", "function", handlerKind(x => x));
165+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 219, characters 7-14", "other", wideHandlerKind(new UntaggedCallable()));
166+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 220, characters 7-14", "other", wideHandlerKind(new UntaggedAnimal()));
167+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 221, characters 7-14", "function", wideHandlerKind(x => x));
168+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 222, characters 7-14", "name", "name");
169+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 223, characters 7-14", "other", "other");
151170
});
152171
Mocha.test("a class declared over an object type", () => {
153-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 205, characters 7-14", "dog", objectOrNameKind(new UntaggedDog()));
154-
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 206, characters 7-14", "rex", "rex");
172+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 227, characters 7-14", "dog", objectOrNameKind(new UntaggedDog()));
173+
Test_utils.eq("File \"untagged_runtime_annotations_test.res\", line 228, characters 7-14", "rex", "rex");
155174
});
156175
});
157176

@@ -169,6 +188,7 @@ export {
169188
localeKind,
170189
describePhantom,
171190
handlerKind,
191+
wideHandlerKind,
172192
objectOrNameKind,
173193
}
174194
/* Not a pure module */

‎tests/tests/src/untagged_runtime_annotations_test.res‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,23 @@ let handlerKind = handler =>
126126
| Function(_) => "function"
127127
}
128128

129+
// A callable instance without an arm reaches the default, not the function arm
130+
@unboxed
131+
type wideHandler =
132+
| WideCallable(callable)
133+
| WideAnimal(animal)
134+
| WideFunction(int => int)
135+
| WideName(string)
136+
| WideCount(float)
137+
| WideFlag(bool)
138+
139+
let wideHandlerKind = handler =>
140+
switch handler {
141+
| WideFunction(_) => "function"
142+
| WideName(_) => "name"
143+
| _ => "other"
144+
}
145+
129146
@unboxed
130147
type boxedAnimal = BoxedAnimal(animal)
131148

@@ -199,6 +216,11 @@ describe(__MODULE__, () => {
199216
test("a function case excludes class instances", () => {
200217
eq(__LOC__, "callable", handlerKind(Callable(makeCallable())))
201218
eq(__LOC__, "function", handlerKind(Function(x => x)))
219+
eq(__LOC__, "other", wideHandlerKind(WideCallable(makeCallable())))
220+
eq(__LOC__, "other", wideHandlerKind(WideAnimal(makeAnimal())))
221+
eq(__LOC__, "function", wideHandlerKind(WideFunction(x => x)))
222+
eq(__LOC__, "name", wideHandlerKind(WideName("a")))
223+
eq(__LOC__, "other", wideHandlerKind(WideCount(1.)))
202224
})
203225

204226
test("a class declared over an object type", () => {

0 commit comments

Comments
 (0)