You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 2d98edf
Browse filesBrowse the repository at this point in the historyBrowse files
Keep class checks away from primitives and default-arm cases
Why:
Two forms still ran class checks on primitives: the two-arm `if`, with
the class arm declared first, and the literal test, in its form of
negated payload checks. And a `switch` with a default arm let the cases
it does not name reach the arms it names: a `Date` landed in the object
arm, and an `@as("lit")` literal in the string arm (#8995).
Of a class arm and an arm of another kind, the two-arm form checks the
other one. A variant with class cases tests its literals by comparison. The
`switch (typeof x)` form is preceded by a guard sending instances of cases
without an arm to the default, as it already did for arrays; when that guard
has class checks, the primitive arms are tested first. The literal test is
skipped only when no literal would pass the check of a payload arm. The
spec's rule 10 example of an unchecked branch changes accordingly: with
`PPromise(promise<int>) | PFloat(float)`, the float case is checked, so a
thenable that is not a `Promise` lands in `PPromise`.
JS modified (an instance handled by the default):
Example:
@unboxed type t = D(Date.t) | R(RegExp.t) | O({x: int}) | S(string) | I(int) | F(int => int)
let f = v => switch v { | O(_) => "O" | S(_) => "S" | _ => "other" }
Before (js):
switch (typeof v) {
case "object" :
return "O";
case "string" :
return "S";
default:
return "other";
}
After (js):
switch (typeof v) {
case "string" :
return "S";
default:
if (v instanceof RegExp || v instanceof Date) {
return "other";
}
switch (typeof v) {
case "object" :
return "O";
default:
return "other";
}
}
JS modified (a literal handled by the default):
Example:
@unboxed type t = | @as("lit") Lit | S(string) | I(int) | B(bool) | O({x: int}) | F(int => int)
let f = v => switch v { | S(_) => "S" | O(_) => "O" | _ => "other" }
Before (js):
switch (typeof v) {
case "string" :
return "S";
After (js):
if (v === "lit") {
return "other";
}
switch (typeof v) {
case "string" :
return "S";
JS modified (two arms):
Example:
@unboxed type t = W(weird) | S(string)
Before (js): if (v instanceof Weird) { return "W"; } else { return "S"; }
After (js): if (typeof v === "string") { return "S"; } else { return "W"; }
Fixes: #8995
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>
Mocha.test("an instance handled by the default is not an object case",()=>{
78
+
Test_utils.eq("File \"untagged_default_arm_test.res\", line 71, characters 7-14","other",objectOrString(id(newDate())));
79
+
Test_utils.eq("File \"untagged_default_arm_test.res\", line 72, characters 7-14","other",objectOrString(id(/a/)));
80
+
Test_utils.eq("File \"untagged_default_arm_test.res\", line 73, characters 7-14","O",objectOrString(id({
81
+
x: 1
82
+
})));
83
+
Test_utils.eq("File \"untagged_default_arm_test.res\", line 74, characters 7-14","S",objectOrString(id("s")));
84
+
Test_utils.eq("File \"untagged_default_arm_test.res\", line 75, characters 7-14","other",objectOrString(id(1)));
85
+
});
86
+
Mocha.test("a literal handled by the default is not a string case",()=>{
87
+
Test_utils.eq("File \"untagged_default_arm_test.res\", line 79, characters 7-14","other",textOrRecord(id("lit")));
88
+
Test_utils.eq("File \"untagged_default_arm_test.res\", line 80, characters 7-14","text",textOrRecord(id("lit2")));
89
+
Test_utils.eq("File \"untagged_default_arm_test.res\", line 81, characters 7-14","record",textOrRecord(id({
90
+
x: 1
91
+
})));
92
+
Test_utils.eq("File \"untagged_default_arm_test.res\", line 82, characters 7-14","other",textOrRecord(id(1)));
93
+
});
94
+
Mocha.test("primitives skip class checks in the two-arm form",()=>Test_utils.eq("File \"untagged_default_arm_test.res\", line 86, characters 7-14","name",twoArmsKind(id("a"))));
95
+
Mocha.test("literals skip class checks",()=>{
96
+
Test_utils.eq("File \"untagged_default_arm_test.res\", line 90, characters 7-14","one",withLiteralsKind(id(1)));
97
+
Test_utils.eq("File \"untagged_default_arm_test.res\", line 91, characters 7-14","name",withLiteralsKind(id("a")));
0 commit comments