Skip to content

Commit dbe05fb

Browse files
cknittclaude
andcommitted
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>
1 parent c59f1fc commit dbe05fb

34 files changed

Lines changed: 841 additions & 148 deletions

‎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, so that the subclass case is checked first.
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.

‎compiler/core/js_exp_make.ml‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,14 +1385,25 @@ let literal_tag = function
13851385
| Null -> nil
13861386
| Undefined -> undefined
13871387

1388+
(* The constructor of a class at a global path such as "Intl.Locale": a
1389+
global with properties, so that a binding that shadows the global's name
1390+
is noticed. *)
1391+
let class_constructor (instance : Variant_runtime.Instance.t) =
1392+
match
1393+
String.split_on_char '.' (Variant_runtime.Instance.to_string instance)
1394+
with
1395+
| [] -> assert false
1396+
| global :: properties -> List.fold_left dot (js_global global) properties
1397+
13881398
(* The [typeof] string an untagged payload answers to. *)
13891399
let block_type_name = function
13901400
| Variant_runtime.IntType | FloatType -> str "number"
13911401
| BigintType -> str "bigint"
13921402
| BooleanType -> str "boolean"
1403+
| SymbolType -> str "symbol"
13931404
| FunctionType -> str "function"
13941405
| StringType -> str "string"
1395-
| InstanceType i -> js_global (Variant_runtime.Instance.to_string i)
1406+
| InstanceType i -> class_constructor i
13961407
| ObjectType -> str "object"
13971408
| UnknownType ->
13981409
(* TODO: this should not happen *)
@@ -1417,8 +1428,7 @@ let rec emit_check (check : t Ast_untagged_variants.Dynamic_checks.t) =
14171428
| TypeOf x -> typeof (emit_check x)
14181429
| IsInstanceOf (Array, x) -> is_array (emit_check x)
14191430
| IsInstanceOf (instance, x) ->
1420-
let instance_name = Variant_runtime.Instance.to_string instance in
1421-
instanceof (emit_check x) (js_global instance_name)
1431+
instanceof (emit_check x) (class_constructor instance)
14221432
| Not x -> not (emit_check x)
14231433
| Expr x -> x
14241434

‎compiler/core/lam_compile.ml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,13 +885,20 @@ let compile output_prefix =
885885
let needs_array_guard =
886886
has_object_typeof && type_has_array_case && not clauses_have_array_case
887887
in
888+
let instances =
889+
Ext_list.filter_map block_cases (function
890+
| Variant_runtime.InstanceType i -> Some i
891+
| _ -> None)
892+
in
888893
let rec build_if_chain remaining_clauses =
889894
match remaining_clauses with
890895
| ( Variant_runtime.Payload_shape (InstanceType instance_type),
891896
{J.switch_body} )
892897
:: rest ->
893898
S.if_
894-
(E.emit_check (IsInstanceOf (instance_type, Expr e)))
899+
(E.emit_check
900+
(Ast_untagged_variants.Dynamic_checks.is_case_instance ~instances
901+
instance_type (Expr e)))
895902
switch_body
896903
~else_:[build_if_chain rest]
897904
| _ -> (

‎compiler/ext/config.ml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
let cmi_magic_number = "Caml1999I040"
1+
let cmi_magic_number = "Caml1999I041"
22

33
(* Magic numbers for marshaled values of the *current* parsetree, whose layout
44
changes across compiler versions. *)
@@ -13,7 +13,7 @@ and ast0_impl_magic_number = "Caml1999M022"
1313

1414
and ast0_intf_magic_number = "Caml1999N022"
1515

16-
and cmt_magic_number = "Caml1999T047"
16+
and cmt_magic_number = "Caml1999T048"
1717

1818
let get_load_path () = (Compiler_request_state.current ()).load_path
1919
let set_load_path path = (Compiler_request_state.current ()).load_path <- path

0 commit comments

Comments
 (0)