Repository navigation
Commit ea15824
Match untagged variants with a literal switch and one typeof dispatch (#9015)
* Match untagged variants with a literal switch and one typeof dispatch
Why:
The payload dispatch of an untagged variant ran its instance checks
before the `typeof` checks, so strings and numbers reached `instanceof`:
a class missing at run time threw for them, and a `Symbol.hasInstance`
could claim them. Literals were tested by a size heuristic that chose
between two equivalent expressions, the object case by a guard per
instance case, and a subclass by negating it in its superclass's check.
The literals come first, compared with `===` by one `switch (x)` (an `if`
per body for at most two), whose default is the payload dispatch: one
`switch (typeof x)` over the primitive kinds, whose default is an `if` chain
over the instance cases (a subclass first), the function case, the tagged
constructors and the object case. In a complete match the last arm is not
checked. A literal without an arm skips the `switch` when no payload check
would accept it, and a `null` that only the object check would accept adds
`x !== null` to it. Checks leading to the default before the last checked
arm fold into its check, so class checks run only on objects.
`Dynamic_checks.is_a_literal_case` and the per-instance guards are gone.
JS modified (primitives before classes):
Example:
@unboxed
type t = Missing(missing) | Text(string) | Count(float)
let kind = value =>
switch value {
| Missing(_) => "missing"
| Text(text) => text
| Count(_) => "count"
}
Before (js):
if (value instanceof UntaggedMissingClass) {
return "missing";
}
switch (typeof value) {
case "string" :
return value;
case "number" :
return "count";
}
After (js):
switch (typeof value) {
case "string" :
return value;
case "number" :
return "count";
default:
return "missing";
}
JS modified (instances without an arm):
Example:
@unboxed
type t = D(Date.t) | R(RegExp.t) | O({x: int}) | S(string) | I(int) | F(int => int)
let kind = value =>
switch value {
| O(_) => "O"
| S(_) => "S"
| _ => "other"
}
Before (js):
if (value instanceof RegExp || value instanceof Date) {
return "other";
}
switch (typeof value) {
case "object" :
return "O";
case "string" :
return "S";
default:
return "other";
}
After (js):
if (typeof value === "string") {
return "S";
} else if (typeof value === "object" && !(value instanceof RegExp) && !(value instanceof Date)) {
return "O";
} else {
return "other";
}
JS modified (a subclass first):
Example:
@unboxed
type upload = Blob(Blob.t) | File(File.t)
let kind = upload =>
switch upload {
| Blob(_) => "blob"
| File(_) => "file"
}
Before (js):
if (upload instanceof Blob && !(upload instanceof File)) {
return "blob";
} else {
return "file";
}
After (js):
if (upload instanceof File) {
return "file";
} else {
return "blob";
}
Review: Does `break` in an arm leave the generated `switch` instead of the
loop? No: every arm placed in a generated `switch` is compiled in a switch
context, where `break` and `continue` are labeled;
`untagged_loop_control_test.res` checks arms of every part.
Review: What changes for a value outside every case? It lands in the last
arm of the chain: with `PPromise | PFloat` the float case is now checked, so
a thenable lands in `PPromise` (rule 10).
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>
* Drop literal arms that a complete payload dispatch runs anyway
A complete payload dispatch with one body runs it unchecked, so a literal arm
with the same body needs no case of its own.
JS modified (literal arm equal to the payload arm):
Example:
@unboxed
type rec t = | @as(null) Null | @as(undefined) Undefined | WhyNotAnotherOne | Point({x: int, y: int})
let plus = (x, y) =>
switch y {
| Null | Undefined => x
| WhyNotAnotherOne => WhyNotAnotherOne
| Point(_) => WhyNotAnotherOne
}
Before (js):
switch (y) {
case null :
case undefined :
return x;
case "WhyNotAnotherOne" :
return "WhyNotAnotherOne";
default:
return "WhyNotAnotherOne";
}
After (js):
if (y == null) {
return x;
} else {
return "WhyNotAnotherOne";
}
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>
* Update the gentype example for the untagged dispatch
The gentype example matches an untagged variant too; its output follows the
new dispatch.
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>
* Say precisely when literals use an if and what folds
Rule 7 counts the literals that get a case, as the code does, and rule 11
names the values constant folding sees: literals and blocks of constants.
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>
* Emit untagged checks directly instead of through Dynamic_checks
The check AST in Ast_untagged_variants only encoded the int-tag test of
tagged variants and the instance checks. Js_exp_make now builds both
directly (is_int_tag from the literal list, is_instance_of), and
matching_facts no longer carries has_null, has_undefined and
has_other_literal, which that test derived. A class is never a switch key,
so block_type_name no longer pretends to emit one. No output changes.
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>
* Share which literal passes which payload check
Code generation and constant folding each had a table of the literals a
payload case accepts, and they disagreed on null and an object case.
Variant_runtime.literal_passes is now the one table; folding follows the
generated code, where `typeof null` is "object", so null without a null
case folds into the object case as it is matched at run time.
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>
* Keep the checks an empty arm or a tagged block needs
Why:
The payload dispatch decided that a match had no arm besides its
primitive ones from the compiled chain of the others, which is empty when
their bodies do nothing, and then ran the last primitive arm unchecked.
A literal arm sharing the body of the payload dispatch was dropped,
although two tagged blocks are told apart by reading their tag, which a
literal such as `null` does not have.
Whether there are other arms comes from the dispatch entries. A literal arm
joins the payload dispatch only when at most one tagged block is matched, and
the last tag of a complete match is not checked, as the last arm of every
other complete match.
JS modified (an arm that does nothing):
Example:
@unboxed type t = S(string) | O({x: int})
let f = v => {
switch v {
| S(_) => Console.log("s")
| O(_) => ()
}
Console.log("after")
}
Before (js):
switch (typeof v) {
default:
console.log("s");
}
console.log("after");
After (js):
if (typeof v === "string") {
console.log("s");
}
console.log("after");
JS modified (a literal sharing the body of tagged blocks):
Example:
type t = | @unboxed MS(string) | MB({x: int}) | MC(int) | @as(null) MNull | @as(1) MOne
let f = v =>
switch v {
| MB(_) => "other"
| MC(_) => "other"
| MOne => "one"
| MNull | MS(_) => "other"
}
Before (js):
if (v === 1) {
return "one";
}
if (typeof v === "string") {
return "other";
}
switch (v.TAG) {
case "MB" :
case "MC" :
return "other";
}
After (js):
if (v === null) {
return "other";
}
if (v === 1) {
return "one";
}
if (typeof v === "string") {
return "other";
}
switch (v.TAG) {
case "MB" :
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>
* Drop the tagged blocks of mixed variants from the dispatch
Since #9040 a variant is untagged as a whole or not at all, so the payload
dispatch of an untagged variant never meets a tagged block. The `switch` on
the tag inside its `if` chain, the merging of tagged clauses, the count of
tagged blocks before dropping literal arms and the tag-name parameter are
gone, and rule 8 no longer lists tagged constructors.
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>
* Give a literal a case where skipping would add a check
Why:
A literal without an arm skipped the literal switch whenever the payload
dispatch led it to the default, even where that made a complete
dispatch check its last arm or added `x !== null`, although the switch
existed for other literals anyway.
When other literals get a case, a literal whose skipping would add such a
check gets a case too. Literals that reach the default without a new check
still skip. The fallback for tagged blocks is gone: an untagged variant has
none since #9040. Rule 10 now says where a value outside every case lands
when the match shares a default.
Generated code changed (a bool literal beside string literals):
Example:
@unboxed
type t = | @as("a") A | @as("b") B | @as(true) T | S(string) | O({x: int})
let f = (x: t) => switch x { | S(_) => 1 | O(_) => 2 | _ => 3 }
Before (JS):
switch (x) {
case "a" :
case "b" :
return 3;
default:
if (typeof x === "string") {
return 1;
} else if (typeof x === "object") {
return 2;
} else {
return 3;
}
}
After (JS):
switch (x) {
case "a" :
case "b" :
case true :
return 3;
default:
if (typeof x === "string") {
return 1;
} else {
return 2;
}
}
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>
* Let literals skip the switch whatever other literals do
Why:
Giving a literal a case where skipping would add a check saves one
typeof or null check in a narrow situation, at the price of two more
predicates in the most intricate part of the dispatch.
This undoes that rule, keeping the removal of the tagged-block fallback and
the rule 10 clause from the same commit.
Generated code changed (a bool literal beside string literals):
Example:
@unboxed
type t = | @as("a") A | @as("b") B | @as(true) T | S(string) | O({x: int})
let f = (x: t) => switch x { | S(_) => 1 | O(_) => 2 | _ => 3 }
Before (JS):
switch (x) {
case "a" :
case "b" :
case true :
return 3;
default:
if (typeof x === "string") {
return 1;
} else {
return 2;
}
}
After (JS):
switch (x) {
case "a" :
case "b" :
return 3;
default:
if (typeof x === "string") {
return 1;
} else if (typeof x === "object") {
return 2;
} else {
return 3;
}
}
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>
* Regenerate the callable-instance test for the typeof dispatch
Why:
The test added to the bottom of the stack is compiled by this pull
request's dispatch, which checks the function arm against the classes.
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>
---------
Signed-off-by: Christoph Knittel <ck@cca.io>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>1 parent 6217c4e commit ea15824
28 files changed
Lines changed: 1422 additions & 907 deletions
File tree
- changes
- compiler
- core
- ml
- tests
- gentype_tests/typescript-react-example/src
- ounit_tests
- tests/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1409 | 1409 | | |
1410 | 1410 | | |
1411 | 1411 | | |
1412 | | - | |
1413 | | - | |
1414 | | - | |
1415 | | - | |
1416 | | - | |
1417 | | - | |
1418 | | - | |
1419 | | - | |
1420 | | - | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
| 1415 | + | |
| 1416 | + | |
| 1417 | + | |
| 1418 | + | |
1421 | 1419 | | |
1422 | 1420 | | |
1423 | 1421 | | |
1424 | 1422 | | |
1425 | 1423 | | |
1426 | | - | |
1427 | | - | |
1428 | | - | |
1429 | | - | |
1430 | | - | |
1431 | | - | |
1432 | | - | |
1433 | | - | |
1434 | | - | |
1435 | | - | |
1436 | | - | |
1437 | | - | |
1438 | | - | |
1439 | | - | |
1440 | | - | |
1441 | | - | |
1442 | | - | |
1443 | | - | |
1444 | | - | |
1445 | | - | |
1446 | | - | |
1447 | | - | |
1448 | | - | |
1449 | | - | |
1450 | | - | |
1451 | | - | |
1452 | | - | |
1453 | | - | |
1454 | | - | |
1455 | | - | |
| 1424 | + | |
| 1425 | + | |
| 1426 | + | |
| 1427 | + | |
| 1428 | + | |
| 1429 | + | |
| 1430 | + | |
| 1431 | + | |
| 1432 | + | |
| 1433 | + | |
| 1434 | + | |
| 1435 | + | |
| 1436 | + | |
| 1437 | + | |
| 1438 | + | |
| 1439 | + | |
1456 | 1440 | | |
1457 | | - | |
| 1441 | + | |
| 1442 | + | |
| 1443 | + | |
| 1444 | + | |
| 1445 | + | |
| 1446 | + | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
1458 | 1450 | | |
1459 | 1451 | | |
1460 | 1452 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
164 | 164 | | |
165 | 165 | | |
166 | 166 | | |
167 | | - | |
| 167 | + | |
| 168 | + | |
168 | 169 | | |
169 | 170 | | |
170 | 171 | | |
| |||
179 | 180 | | |
180 | 181 | | |
181 | 182 | | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
| 183 | + | |
189 | 184 | | |
190 | 185 | | |
191 | 186 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
79 | 82 | | |
80 | 83 | | |
81 | 84 | | |
| |||
0 commit comments