Skip to content

Commit 6217c4e

Browse files
cknittclaude
andauthored
Declare the runtime class or typeof of types, and annotate the stdlib (#8950)
* Declare the runtime class or typeof of abstract types Why: Untagged variants recognize payloads of abstract types only from a hard-coded list of stdlib type names, which knows no subclasses and is closed to bindings such as WebAPI's (#7147). `@instanceOf("Class")` and `@typeOf("…")` on an abstract type or a type abbreviation state how its values are recognized at run time. The classifier reads them along the type's expansion chain: the outermost one decides, and the classes annotated further down a chain of private abbreviations are its superclasses. A class check excludes the subclasses that are cases too. Both imply that a value is never undefined. The name list stays as fallback for types without annotations. Editor completion offers both, and `@notUndefined`, which was listed with the file-level `@@` decorators, now completes after a single `@`. Representation added (untagged variant payload): Example: @instanceof("Animal") type animal @instanceof("Dog") type dog = private animal @typeof("symbol") type sym @unboxed type t = Animal(animal) | Dog(dog) | Sym(sym) After (representation): Animal(a) ↦ a, recognized by a instanceof Animal && !(a instanceof Dog) Dog(d) ↦ d, recognized by d instanceof Dog Sym(s) ↦ s, recognized by typeof s === "symbol" Diagnostic added (invalid annotation): Example: @typeof("undefined") type t After (diagnostic): @typeof expects one of "string", "number", "bigint", "boolean", "symbol", "function" or "object", not "undefined". Review: Why `@instanceOf`/`@typeOf` rather than one `@runtime(...)` attribute? They read like the JavaScript checks they produce, take a single string like `@as` and `@tag`, and sit next to the existing `@notUndefined`. Review: Why also on public abbreviations? The typed array types are public aliases of `TypedArray.t<'a>` that users pass to `TypedArray` functions, so they can only be told apart by the name as written, as the name list does. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io> * Check runtime annotations against the types they describe Why: An annotation on an abbreviation was trusted whatever the abbreviated type was. `@typeOf("string") type s = option<string>` made `Some(None)` of `s` the same value as `None`, and two spellings of one type were recognized differently. Paths such as `Object`, `BigInt` or `globalThis.Date` were accepted and misrecognized every value or duplicated another class. An annotation on an abbreviation must now refine an abstract type that is not builtin, repeat the abbreviated type's annotation, or declare a subclass of the class annotated there. A subclass needs a private abbreviation, or a public one that is a strict instance of the superclass type (a phantom type hierarchy), and must not repeat a class of its chain. Paths that name no class to recognize values by are rejected. Payloads of `@unboxed` wrappers of annotated types are never undefined either, as with `@notUndefined`, and the function check excludes class cases as the object check does, so the two-arm form agrees with the `switch` form. Diagnostic added (annotation on a type that can be undefined): Example: @typeof("string") type s = option<string> After (diagnostic): @typeof on a type abbreviation must describe an abstract type, or a type with the same annotation. The abbreviated type already has a runtime representation the compiler knows, or can be undefined. Diagnostic added (subclass on a public abbreviation): Example: @instanceof("Animal") type animal @instanceof("Dog") type dog = animal After (diagnostic): A subclass must be a private abbreviation: write `= private ...` so that instances of Animal cannot be used as instances of Dog without a coercion. Diagnostic added (class path): Example: @instanceof("BigInt") type t After (diagnostic): Values of type bigint are primitives, not instances of BigInt. Use @typeof("bigint") instead. JS modified (option of an unboxed wrapper): Example: @unboxed type boxedAnimal = BoxedAnimal(animal) let x: option<boxedAnimal> = Some(BoxedAnimal(a)) Before (js): let x = Primitive_option.some(a); After (js): let x = a; JS modified (function case beside a class case): Example: @unboxed type handler = Function(int => int) | Callable(callable) Before (js): if (typeof handler === "function") { return "function"; } After (js): if (typeof handler === "function" && !(handler instanceof UntaggedCallable)) { return "function"; } Review: Why allow public subclasses that are strict instances? `Dom.res` and WebAPI model class hierarchies with phantom types, such as `type element<'a> = node<_element<'a>>`, whose subclass types unify with the superclass type only at the phantom argument. `type dog = animal` is the same type as `animal` and is rejected. Review: Why reject `globalThis.C` instead of normalizing it? The compiler already reads a shadowed class through `globalThis`, so the prefix adds nothing, and one spelling per class keeps the duplicate and subclass checks, which compare paths, exact. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io> * Compare runtime annotations and untagged payloads across signatures Why: Interface inclusion ignored `@instanceOf`/`@typeOf`. An interface could claim a class its implementation does not have, or hide a private superclass manifest; the implementation and its clients then compiled different checks for the same values, and #8938 came back in the clients. Functor arguments, `with type` constraints and module type ascriptions had the same hole. When an interface states a class or `typeof` for a type, through its own annotation or the type it abbreviates, the implementation must state the same one, superclasses included. An interface that states none hides it. The payload kind of every untagged case is compared too, so a variant declared in both is recognized the same way on both sides. Diagnostic added (hidden superclass): Example: // Classes.res @instanceof("Animal") type animal @instanceof("Dog") type dog = private animal // Classes.resi @instanceof("Animal") type animal @instanceof("Dog") type dog After (diagnostic): Type declarations do not match: type dog = animal is not included in type dog Their runtime representations differ: the first declaration states instances of Dog, a subclass of Animal, the second declaration states instances of Dog. Diagnostic added (claim the implementation does not make): Example: // Claim.res type t = option<string> // Claim.resi @typeof("string") type t After (diagnostic): Their runtime representations differ: the first declaration states no runtime class or typeof, the second declaration states values whose typeof is "string". Review: Why compare payload kinds when the payload types are already compared? Equal payload types are classified alike only when both sides see the same annotations; an interface that hides one (stating none, which is allowed) gives its variant another layout. Review: Why not compare `@notUndefined` too? It has the same hole on master (#8964), fixed separately in #8966; the implied never-undefined of the new annotations is covered here, because the classification includes it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io> * Document runtime annotations in the untagged variants spec Why: The spec states the rules the compiler implements, and its open questions asked what a mechanism for declaring classes and kinds on types has to decide. Rule 5 now describes `@instanceOf` and `@typeOf`: where they can appear, how they are read along a chain of abbreviations, which abbreviations and class paths are rejected, and the name list that still covers types without annotations. Rules 4, 6, 8, 10, 12, 13 and 14 gain what the annotations change: declared kinds, one case per class and per symbol, subclass and function exclusions, missing classes, implied never-undefined, and inclusion checks. The open questions become the decisions taken, with what is still open. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io> * Annotate the stdlib's runtime classes Why: Untagged variants recognized stdlib classes only by type name, with no relation between them, so a `Blob` case declared before a `File` case also caught files (#8938). Types whose values are instances of a global class now say so with `@instanceOf` instead of `@notUndefined`, `Symbol.t` with `@typeOf("symbol")`, and `File.t` is a private abbreviation of `Blob.t`. `JsError.t`, `Symbol.t` and the `Intl` classes can be untagged variant cases too. Fixes #8938. JS modified (Blob and File cases): 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) { return "blob"; } else { return "file"; } After (js): if (upload instanceof Blob && !(upload instanceof File)) { return "blob"; } else { return "file"; } Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io> * Document the stdlib's runtime annotations in the untagged variants spec Why: With the stdlib annotated, `Blob.t` and `File.t` are never undefined and #8938 is fixed, which the spec and its test still stated the other way round. Rule 5 lists the stdlib's annotations, rule 12 unboxes `option` of a variant with a `Blob.t` payload, and the known bug of overlapping classes is gone. The typed-array bug stays, filed as #8955. JS modified (option of a variant with a Blob payload): Example: @unboxed type withBlob = BBlob(Blob.t) | BString(string) let someWithBlob = (v: withBlob): option<withBlob> => Some(v) Before (js): return Primitive_option.some(v); After (js): return v; Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io> * Recognize untagged payloads only by annotations and builtin types Why: With the stdlib annotated, the classifier's list of stdlib type names only matched types that happen to be printed like a stdlib type, and its name-based dict check took any type named `dict` for an object. The list, matched before and after expansion, is gone, and so is the dict check: a payload is recognized by its `@instanceOf` or `@typeOf`, or by the builtin type it expands to. Generated code is unchanged for the stdlib's types. Diagnostic added (a local type named dict): Example: type dict<'a> @unboxed type t = D(dict<int>) | S(string) Before (js): if (typeof v === "object") { return "D"; } else { return "S"; } After (diagnostic): This untagged variant definition is invalid: Case D has a payload that is not of one of the recognized shapes (object, array, etc). Then it must be the only case with payloads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io> * Test that payloads are not recognized by type name Why: The classifier used to recognize payloads by the printed path of their type; nothing pinned that it no longer does. An unannotated local `Stdlib_Date.t`, which the removed list would have taken for a date, is an unknown payload, and aliases of builtin types are recognized through their expansion. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io> * Drop the name list from the untagged variants spec Why: Payloads are now recognized only by annotations and builtin types, so the spec's list of stdlib type names and the fallback to it are gone. Rule 4 lists `promise<_>` as the one builtin class and says that a type is never recognized by its name; rule 5 keeps the stdlib's annotations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io> * Store the resolved runtime shape on type declarations Why: Every reader re-derived what `@instanceOf`, `@typeOf` and `@notUndefined` state by walking a type's expansion chain in the reader's environment. Interface inclusion therefore evaluated an interface's `@instanceOf("Dog") type dog = private animal` against the implementation's annotated `animal`, while clients saw an unannotated `animal`: a superclass hidden that way was lost in clients. A declaration now carries `type_shape`: `Unstated`, `Never_undefined`, or `Recognized_as` a class with its superclasses or a `typeof` kind, resolved when the type is declared, in the environment it is declared in. The classifier, `option` unboxing and inclusion read it, walking only to the first declaration that states a shape; no attribute is parsed after declaration, and the deduplication of annotations repeated through module aliases is gone. Diagnostic added (a superclass hidden by its interface): Example: // Classes.res @instanceof("Animal") type animal @instanceof("Dog") type dog = private animal // Classes.resi type animal @instanceof("Dog") type dog = private animal After (diagnostic): Their runtime representations differ: the first declaration states instances of Dog, a subclass of Animal, the second declaration states instances of Dog. Review: Why a field rather than a normalized attribute? The facts are typed data, not text, and a field cannot be printed or forwarded by tools that handle attributes. 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> # Conflicts: # compiler/ext/config.ml * Close the gaps the re-review of runtime annotations found Why: An annotation could refine a functor parameter's or a locally abstract type, which stands for any type: `@instanceOf("Date") type d = X.t` in a functor applied to `option<int>` made `Some(None)` collapse to `None`. An abbreviation through an alias passed as a phantom subclass, an interface hiding a payload's class was rejected although master accepts it, and two `@instanceOf` attributes were reported as a conflict between the two kinds. Annotations on the type of a functor parameter, of a recursive module being defined, or on a locally abstract type are rejected. The phantom-hierarchy exception reads the abbreviation's arguments after expansion. A class can be declared over a `@typeOf("object")` type. An interface case with an unknown payload agrees with any payload of the implementation, since it is the only payload case and is never checked. Diagnostic added (a type that stands for another one): Example: module Make = (X: {type t}) => { @instanceof("Date") type date = X.t } After (diagnostic): @instanceof cannot describe the type of a functor parameter, of a recursive module being defined, or a locally abstract type: such a type stands for another one, whose values can be anything. Diagnostic added (public subclass through an alias): Example: @instanceof("Animal") type animal type phantom<'a> = animal @instanceof("Dog") type dog = phantom<int> After (diagnostic): A subclass must be a private abbreviation: write `= private ...` so that instances of Animal cannot be used as instances of Dog without a coercion. Diagnostic removed (an interface hiding a payload's class): Example: // A.resi type t @unboxed type v = | @as("n") N | P(t) // A.res type t = Date.t @unboxed type v = | @as("n") N | P(t) Before (diagnostic): The internal representations for case P are not equal 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> * Simplify the payload kinds and untagged checks Why: The architecture review found the same knowledge in several places: three tables of `typeof` names, an `IntType`/`FloatType` distinction that no consumer made, two functions for `@unboxed`, two checks of the arity of untagged constructors, and the opaque-abstract-type test written inline. `Variant_runtime` lists the kinds `typeof` recognizes once, and the names, the reverse lookup and the generated `typeof` strings come from it. `int` and `float` payloads have one kind, `NumberType`. `has_untagged` replaces its twin, the layout alone reports a constructor with more than one argument, now at the constructor, and `Variant_layout.abstractness` says whether an abstract type can be refined. `resolve_shapes` stops when a round changes nothing, `type_cannot_contain_undefined` no longer re-reads a shape it has read, and the doc of `type_shape` and rule 14 say what the shape holds and how classes compare. Diagnostic modified (more than one argument): Example: @unboxed type t = A(int, int) | B(string) Before (diagnostic): UntaggedConstructorMoreThanOneArg.res:2:1-32 After (diagnostic): UntaggedConstructorMoreThanOneArg.res:2:10-20 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> * Check @notundefined of an interface against the implementation Why: Interface inclusion ignored `@notUndefined`. An interface could declare `@notUndefined type t` over `type t = option<int>`, and clients then unboxed `option<t>`, so `Some(M.none)` was `None` (#8964). When the second declaration of an inclusion is `@notUndefined`, the first must be a type that cannot hold `undefined`, by the rule `option` unboxing uses, or an abstract type, which has no representation to contradict it. This covers `.resi` files, functor arguments and `with type` constraints. The check runs in `Includemod`, since `Typeopt` depends on `Includecore` through `Typedecl`. Diagnostic added: Example: // Hidden.resi @notundefined type t // Hidden.res type t = option<int> After (diagnostic): Type declarations do not match: type t = option<int> is not included in type t Their runtime representations differ: the second declaration is @notundefined, but the first declaration can hold undefined. Review: Why accept an abstract implementation without the attribute? The stdlib's `Lazy.res` declares `type t<+'a>` under `@notUndefined` in its interface, and so may bindings; such a type has no representation that could hold `undefined`, so requiring the attribute twice would only break them. Fixes: #8964 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io> * Accept @notundefined over implementations the compiler cannot see into Why: The check rejected bindings that are fine: an interface's `@notUndefined type t` over `type t = Dom.element`, an abbreviation of an abstract type, or over a JavaScript object type, which is never undefined. An implementation whose representation the compiler does not see has none that contradicts the interface: an abstract type, also reached through an abbreviation, or a JavaScript object type. A functor parameter's or a locally abstract type stands for any type and does not count as abstract. Diagnostic removed (an abbreviation of an abstract type): Example: // A.resi @notundefined type t // A.res type t = Dom.element Before (diagnostic): Their runtime representations differ: the second declaration is @notundefined, but the first declaration can hold undefined. 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> * Require a private abbreviation for every subclass Why: A public abbreviation could declare a subclass when its arguments were not all type variables, meant for phantom type hierarchies. The test was syntactic, so `@instanceOf("Dog") type dog = animal<int>` passed, and every `animal<int>` was then a dog without a coercion. A subclass is always a private abbreviation, phantom hierarchies included; upcasts use `:>`. Diagnostic added (public subclass of an instantiated class): Example: @instanceof("Animal") type animal<'a> @instanceof("Dog") type dog = animal<int> Before (diagnostic): none After (diagnostic): A subclass must be a private abbreviation: write `= private ...` so that instances of Animal cannot be used as instances of Dog without a coercion. 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 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> --------- Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7593903 commit 6217c4e

159 files changed

Lines changed: 2281 additions & 376 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎analysis/src/completion_decorators.ml‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,46 @@ You will need this decorator whenever you want to use a JSX component in ReScrip
298298
[
299299
{|The `@editor.completeFrom` decorator instructs the editor where it can draw additional completions from for this type.|};
300300
] );
301+
( "instanceOf",
302+
Some "instanceOf(\"$0\")",
303+
[
304+
{|The `@instanceOf` decorator states that the values of an abstract type are instances of a global JavaScript class. Such a type can be the payload of an untagged variant case, which is then recognized with `instanceof`, and is never `undefined`, like with `@notUndefined`.
305+
306+
A private abbreviation of another annotated type declares a subclass. A case of the superclass then excludes the instances of a subclass that is a case too, whatever the order of the cases.
307+
308+
Example usage:
309+
```rescript
310+
@instanceOf("Blob")
311+
type blob
312+
313+
@instanceOf("File")
314+
type file = private blob
315+
```|};
316+
] );
317+
( "typeOf",
318+
Some "typeOf(\"$0\")",
319+
[
320+
{|The `@typeOf` decorator states the JavaScript `typeof` of the values of an abstract type: one of `"string"`, `"number"`, `"bigint"`, `"boolean"`, `"symbol"`, `"function"` or `"object"`. Such a type can be the payload of an untagged variant case, and is never `undefined`, like with `@notUndefined`.
321+
322+
Example usage:
323+
```rescript
324+
@typeOf("symbol")
325+
type key
326+
```|};
327+
] );
328+
( "notUndefined",
329+
None,
330+
[
331+
{|The `@notUndefined` decorator marks an abstract type as one that can never be `undefined` in JavaScript. This allows the compiler to generate more efficient code when the type is used inside an `option`.
332+
333+
Example usage:
334+
```rescript
335+
@notUndefined
336+
type t
337+
```
338+
339+
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#notundefined-decorator).|};
340+
] );
301341
]
302342

303343
let toplevel =
@@ -331,17 +371,4 @@ let toplevel =
331371

332372
[Read more and see examples in the documentation](https://rescript-lang.org/docs/manual/latest/jsx#file-level-configuration).|};
333373
] );
334-
( "notUndefined",
335-
None,
336-
[
337-
{|The `@notUndefined` decorator marks an abstract type as one that can never be `undefined` in JavaScript. This allows the compiler to generate more efficient code when the type is used inside an `option`.
338-
339-
Example usage:
340-
```rescript
341-
@notUndefined
342-
type t
343-
```
344-
345-
[Read more and see examples in the documentation](https://rescript-lang.org/syntax-lookup#notundefined-decorator).|};
346-
] );
347374
]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Add `@instanceOf("Class")` and `@typeOf("…")` for abstract types and type abbreviations, so that values of bound JavaScript classes and opaque primitive types can be untagged variant cases. A private abbreviation of another annotated type declares a subclass.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Reject an implementation that can hold `undefined` for a type its interface declares `@notUndefined`, which made `Some(None)` of the type `None` in clients.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- An untagged variant with both `Blob` and `File` cases matches files as `File`, whatever the order of its cases. `File.t` is now a private abbreviation of `Blob.t`, and `JsError.t`, `Symbol.t` and the `Intl` classes can be untagged variant cases.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Untagged variants recognize stdlib classes only through their `@instanceOf` annotations; the compiler's list of stdlib type names is gone.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Require `= private` for every subclass declared with `@instanceOf`, also in phantom type hierarchies, and fold `null` into an untagged variant's object case as it is matched at run time.

‎compiler/core/js_exp_make.ml‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,18 +1399,25 @@ let literal_tag = function
13991399
| Null -> nil
14001400
| Undefined -> undefined
14011401

1402+
(* The constructor of a class at a global path such as "Intl.Locale": a
1403+
global with properties, so that a binding that shadows the global's name
1404+
is noticed. *)
1405+
let class_constructor (instance : Variant_runtime.Instance.t) =
1406+
match
1407+
String.split_on_char '.' (Variant_runtime.Instance.to_string instance)
1408+
with
1409+
| [] -> assert false
1410+
| global :: properties -> List.fold_left dot (js_global global) properties
1411+
14021412
(* The [typeof] string an untagged payload answers to. *)
14031413
let block_type_name = function
1404-
| Variant_runtime.IntType | FloatType -> str "number"
1405-
| BigintType -> str "bigint"
1406-
| BooleanType -> str "boolean"
1407-
| FunctionType -> str "function"
1408-
| StringType -> str "string"
1409-
| InstanceType i -> js_global (Variant_runtime.Instance.to_string i)
1410-
| ObjectType -> str "object"
1411-
| UnknownType ->
1412-
(* TODO: this should not happen *)
1413-
assert false
1414+
| Variant_runtime.InstanceType i -> class_constructor i
1415+
| block_type -> (
1416+
match Variant_runtime.typeof_name block_type with
1417+
| Some name -> str name
1418+
| None ->
1419+
(* An unknown payload has no check: it is the only payload case *)
1420+
assert false)
14141421

14151422
let tag_type = function
14161423
| Variant_runtime.Literal d -> literal_tag d
@@ -1431,8 +1438,7 @@ let rec emit_check (check : t Ast_untagged_variants.Dynamic_checks.t) =
14311438
| TypeOf x -> typeof (emit_check x)
14321439
| IsInstanceOf (Array, x) -> is_array (emit_check x)
14331440
| IsInstanceOf (instance, x) ->
1434-
let instance_name = Variant_runtime.Instance.to_string instance in
1435-
instanceof (emit_check x) (js_global instance_name)
1441+
instanceof (emit_check x) (class_constructor instance)
14361442
| Not x -> not (emit_check x)
14371443
| Expr x -> x
14381444

‎compiler/core/lam_compile.ml‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,7 @@ let compile output_prefix =
797797
(fun (block_type : Variant_runtime.block_type) ->
798798
match (literal, block_type) with
799799
| String _, StringType
800-
| (Int _ | Float _), (IntType | FloatType)
800+
| (Int _ | Float _), NumberType
801801
| BigInt _, BigintType
802802
| Bool _, BooleanType
803803
| _, UnknownType ->
@@ -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
@@ -909,13 +912,20 @@ let compile output_prefix =
909912
Some instance
910913
| _ -> None)
911914
in
915+
let instances =
916+
Ext_list.filter_map block_cases (function
917+
| Variant_runtime.InstanceType i -> Some i
918+
| _ -> None)
919+
in
912920
let rec build_if_chain remaining_clauses =
913921
match remaining_clauses with
914922
| ( Variant_runtime.Payload_shape (InstanceType instance_type),
915923
{J.switch_body} )
916924
:: rest ->
917925
S.if_
918-
(E.emit_check (IsInstanceOf (instance_type, Expr e)))
926+
(E.emit_check
927+
(Ast_untagged_variants.Dynamic_checks.is_case_instance ~instances
928+
instance_type (Expr e)))
919929
switch_body
920930
~else_:[build_if_chain rest]
921931
| _ -> (

0 commit comments

Comments
 (0)