Skip to content

Commit 3aafb3f

Browse files
cknittclaude
andcommitted
Recognize untagged payloads only by annotations and builtin types
Now that the stdlib's classes are annotated, the list of stdlib type names the classifier matched before and after expansion is gone, and so is the name-based dict check: a payload is recognized by its `@instanceOf` or `@typeOf`, or by the builtin type it expands to. Generated code is unchanged. 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 1107e5d commit 3aafb3f

4 files changed

Lines changed: 23 additions & 74 deletions

File tree

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.

‎compiler/ml/ast_untagged_variants.ml‎

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -248,47 +248,6 @@ let check_runtime_annotation ~loc ~is_abstract_or_abbreviation
248248
| Instance_of _ -> instance_of
249249
| Type_of _ -> type_of) ))
250250

251-
let type_is_builtin_object (t : Types.type_expr) =
252-
match t.desc with
253-
| Tconstr (Path.Pident ident, [_], _) when Ident.name ident = "dict" -> true
254-
| Tconstr (path, _, _) ->
255-
let name = Path.name path in
256-
name = "Stdlib.Dict.t" || name = "Stdlib_Dict.t"
257-
| _ -> false
258-
259-
let type_to_instanceof_backed_obj (t : Types.type_expr) =
260-
let cls name = Some (Instance.Class {name; ancestors = []}) in
261-
match t.desc with
262-
| Tconstr (path, _, _) when Path.same path Predef.path_promise ->
263-
cls "Promise"
264-
| Tconstr (path, _, _) when Path.same path Predef.path_array ->
265-
Some Instance.Array
266-
| Tconstr (path, _, _) -> (
267-
match Path.name path with
268-
| "Stdlib_ArrayBuffer.t" -> cls "ArrayBuffer"
269-
| "Stdlib.BigInt64Array.t" -> cls "BigInt64Array"
270-
| "Stdlib.BigUint64Array.t" -> cls "BigUint64Array"
271-
| "Stdlib.DataView.t" -> cls "DataView"
272-
| "Stdlib_Date.t" -> cls "Date"
273-
| "Stdlib.Float32Array.t" -> cls "Float32Array"
274-
| "Stdlib.Float64Array.t" -> cls "Float64Array"
275-
| "Stdlib.Int16Array.t" -> cls "Int16Array"
276-
| "Stdlib.Int32Array.t" -> cls "Int32Array"
277-
| "Stdlib.Int8Array.t" -> cls "Int8Array"
278-
| "Stdlib_RegExp.t" -> cls "RegExp"
279-
| "Stdlib.Uint16Array.t" -> cls "Uint16Array"
280-
| "Stdlib.Uint32Array.t" -> cls "Uint32Array"
281-
| "Stdlib.Uint8Array.t" -> cls "Uint8Array"
282-
| "Stdlib.Uint8ClampedArray.t" -> cls "Uint8ClampedArray"
283-
| "Stdlib_File.t" -> cls "File"
284-
| "Stdlib_Blob.t" -> cls "Blob"
285-
| "Stdlib.Set.t" -> cls "Set"
286-
| "Stdlib.Map.t" -> cls "Map"
287-
| "Stdlib.WeakSet.t" -> cls "WeakSet"
288-
| "Stdlib.WeakMap.t" -> cls "WeakMap"
289-
| _ -> None)
290-
| _ -> None
291-
292251
let process_tag_name (attrs : Parsetree.attributes) =
293252
let st = ref None in
294253
Ext_list.iter attrs (fun ({txt; loc}, payload) ->

‎compiler/ml/typeopt.ml‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,12 @@ let rec type_cannot_contain_undefined (typ : Types.type_expr) (env : Env.t) =
9292
&& ((not payload_is_unboxed)
9393
||
9494
match cd.cd_args with
95-
| Cstr_tuple [t] ->
96-
Ast_untagged_variants.type_is_builtin_object t
97-
|| type_cannot_contain_undefined t env
95+
| Cstr_tuple [t] -> type_cannot_contain_undefined t env
9896
| Cstr_tuple [] -> true
9997
| Cstr_tuple (_ :: _ :: _) ->
10098
true (* Not actually possible for an unboxed payload. *)
10199
| Cstr_record [{ld_type = t}] ->
102-
Ast_untagged_variants.type_is_builtin_object t
103-
|| type_cannot_contain_undefined t env
100+
type_cannot_contain_undefined t env
104101
| Cstr_record ([] | _ :: _ :: _) -> true)
105102
&& all_cases_cannot_contain_undefined (position + 1) rest
106103
in

‎compiler/ml/variant_layout.ml‎

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,26 @@ let get_block_type_from_typ ~env (t : Types.type_expr) : block_type option =
6060
match declared_block_type ~env t with
6161
| Some _ as declared -> declared
6262
| None -> (
63-
(* First check the original (unexpanded) type for typed arrays and other instance types *)
64-
match type_to_instanceof_backed_obj t with
65-
| Some instance_type -> Some (InstanceType instance_type)
66-
| None -> (
67-
(* If original type didn't match, expand and try standard checks *)
68-
let expanded_t = Ctype.expand_head env t in
69-
match expanded_t with
70-
| {desc = Tconstr (path, _, _)} when Path.same path Predef.path_string ->
71-
Some StringType
72-
| {desc = Tconstr (path, _, _)} when Path.same path Predef.path_int ->
73-
Some IntType
74-
| {desc = Tconstr (path, _, _)} when Path.same path Predef.path_float ->
75-
Some FloatType
76-
| {desc = Tconstr (path, _, _)} when Path.same path Predef.path_bigint ->
77-
Some BigintType
78-
| {desc = Tconstr (path, _, _)} when Path.same path Predef.path_bool ->
79-
Some BooleanType
80-
| {desc = Tarrow _} -> Some FunctionType
81-
| {desc = Tconstr _} as expanded_t when type_is_builtin_object expanded_t
82-
->
83-
Some ObjectType
84-
| {desc = Tconstr _} as expanded_t
85-
when type_to_instanceof_backed_obj expanded_t |> Option.is_some -> (
86-
match type_to_instanceof_backed_obj expanded_t with
87-
| None -> None
88-
| Some instance_type -> Some (InstanceType instance_type))
89-
| {desc = Ttuple _} -> Some (InstanceType Array)
90-
| _ -> None))
63+
(* Builtin types have no declaration to annotate *)
64+
match (Ctype.expand_head env t).desc with
65+
| Tconstr (path, _, _) when Path.same path Predef.path_string ->
66+
Some StringType
67+
| Tconstr (path, _, _) when Path.same path Predef.path_int -> Some IntType
68+
| Tconstr (path, _, _) when Path.same path Predef.path_float ->
69+
Some FloatType
70+
| Tconstr (path, _, _) when Path.same path Predef.path_bigint ->
71+
Some BigintType
72+
| Tconstr (path, _, _) when Path.same path Predef.path_bool ->
73+
Some BooleanType
74+
| Tconstr (path, _, _) when Path.same path Predef.path_dict ->
75+
Some ObjectType
76+
| Tconstr (path, _, _) when Path.same path Predef.path_array ->
77+
Some (InstanceType Array)
78+
| Tconstr (path, _, _) when Path.same path Predef.path_promise ->
79+
Some (InstanceType (Class {name = "Promise"; ancestors = []}))
80+
| Ttuple _ -> Some (InstanceType Array)
81+
| Tarrow _ -> Some FunctionType
82+
| _ -> None)
9183

9284
let get_block_type ~env (cstr : Types.constructor_declaration) : block_type =
9385
match cstr.cd_args with

0 commit comments

Comments
 (0)