Skip to content

Commit 55c756b

Browse files
cknittclaude
andcommitted
Count untagged payload kinds in one table
Why: `check_invariant` kept a counter per payload kind and rechecked all of them after every case, with an error variant per kind. The kinds seen so far are one table, classes keyed by name, and a second case of a kind is reported where it is added. `AtMostOneOfKind` replaces the eight `AtMostOne*` errors and keeps their messages. 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>
1 parent 4f3acd4 commit 55c756b

2 files changed

Lines changed: 49 additions & 87 deletions

File tree

‎compiler/ml/ast_untagged_variants.ml‎

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@ open Variant_runtime
22

33
type untagged_error =
44
| OnlyOneUnknown of string
5-
| AtMostOneObject
6-
| AtMostOneInstance of Instance.t
7-
| AtMostOneFunction
8-
| AtMostOneString
9-
| AtMostOneNumber
10-
| AtMostOneBigint
11-
| AtMostOneBoolean
12-
| AtMostOneSymbol
5+
| AtMostOneOfKind of block_type (** never [UnknownType] *)
136
| UnreachableBoolean of string
147
| DuplicateLiteral of string
158
| SameLiteralValue of string * string
@@ -66,22 +59,24 @@ let report_error ppf =
6659
"Case " ^ name
6760
^ " has a payload that is not of one of the recognized shapes (object, \
6861
array, etc). Then it must be the only case with payloads."
69-
| AtMostOneObject -> "At most one case can be an object type."
70-
| AtMostOneInstance Array ->
71-
"At most one case can be an array or tuple type."
72-
| AtMostOneInstance i ->
73-
"At most one case can be an instance of " ^ Instance.to_string i ^ "."
74-
| AtMostOneFunction -> "At most one case can be a function type."
75-
| AtMostOneString -> "At most one case can be a string type."
76-
| AtMostOneBoolean -> "At most one case can be a boolean type."
77-
| AtMostOneSymbol -> "At most one case can be a symbol type."
62+
| AtMostOneOfKind kind ->
63+
"At most one case can be "
64+
^ (match kind with
65+
| ObjectType -> "an object type"
66+
| InstanceType Array -> "an array or tuple type"
67+
| InstanceType i -> "an instance of " ^ Instance.to_string i
68+
| FunctionType -> "a function type"
69+
| StringType -> "a string type"
70+
| NumberType -> "a number type (int or float)"
71+
| BigintType -> "a bigint type"
72+
| BooleanType -> "a boolean type"
73+
| SymbolType -> "a symbol type"
74+
| UnknownType -> "of an unknown type")
75+
^ "."
7876
| UnreachableBoolean name ->
7977
"Case " ^ name
8078
^ " can never match: both true and false are already cases of this \
8179
variant."
82-
| AtMostOneNumber ->
83-
"At most one case can be a number type (int or float)."
84-
| AtMostOneBigint -> "At most one case can be a bigint type."
8580
| DuplicateLiteral s -> "Duplicate literal " ^ s ^ "."
8681
| SameLiteralValue (literal, earlier) ->
8782
"The literal " ^ literal ^ " is the same JavaScript value as " ^ earlier

‎compiler/ml/variant_layout.ml‎

Lines changed: 34 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,13 @@ let check_invariant ~(consts : (Location.t * tag) list)
131131
(* Keyed by [literal_key], with the spelling of the literal *)
132132
let nonstring_literals_consts = ref String_map.empty in
133133
let nonstring_literals_blocks = ref String_map.empty in
134-
let instance_types = Hashtbl.create 1 in
135-
let function_types = ref 0 in
136-
let object_types = ref 0 in
137-
let string_types = ref 0 in
138-
let number_types = ref 0 in
139-
let bigint_types = ref 0 in
140-
let boolean_types = ref 0 in
141-
let symbol_types = ref 0 in
142-
let unknown_types = ref 0 in
134+
(* The kinds of the untagged payloads seen so far. A class is identified by
135+
its name. *)
136+
let kinds = Hashtbl.create 1 in
137+
let kind_key = function
138+
| InstanceType (Class {name}) -> InstanceType (Class {name; ancestors = []})
139+
| kind -> kind
140+
in
143141
let add_string_literal ~is_const ~loc s =
144142
let set =
145143
if is_const then string_literals_consts else string_literals_blocks
@@ -164,32 +162,6 @@ let check_invariant ~(consts : (Location.t * tag) list)
164162
| None -> ());
165163
map := String_map.add key s !map
166164
in
167-
let invariant loc name =
168-
if !unknown_types <> 0 && List.length blocks <> 1 then
169-
raise
170-
(Error (loc, InvalidUntaggedVariantDefinition (OnlyOneUnknown name)));
171-
if !object_types > 1 then
172-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneObject));
173-
Hashtbl.iter
174-
(fun _ (i, count) ->
175-
if count > 1 then
176-
raise
177-
(Error (loc, InvalidUntaggedVariantDefinition (AtMostOneInstance i))))
178-
instance_types;
179-
if !function_types > 1 then
180-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneFunction));
181-
if !string_types > 1 then
182-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneString));
183-
if !number_types > 1 then
184-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneNumber));
185-
if !bigint_types > 1 then
186-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneBigint));
187-
if !boolean_types > 1 then
188-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneBoolean));
189-
if !symbol_types > 1 then
190-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneSymbol));
191-
()
192-
in
193165
let check_literal ~is_const ~loc (tag : tag) =
194166
match tag.literal with
195167
| String s -> add_string_literal ~is_const ~loc s
@@ -214,39 +186,34 @@ let check_invariant ~(consts : (Location.t * tag) list)
214186
Ext_list.rev_iter blocks (fun (loc, block) ->
215187
match block with
216188
| Tagged {tag} -> check_literal ~is_const:false ~loc tag
189+
| Untagged {tag; block_type = UnknownType} ->
190+
(* No check tells an unknown payload apart from another payload *)
191+
if List.length blocks <> 1 then
192+
raise
193+
(Error
194+
(loc, InvalidUntaggedVariantDefinition (OnlyOneUnknown tag.name)))
217195
| Untagged {tag; block_type} ->
218-
(match block_type with
219-
| UnknownType -> incr unknown_types
220-
| ObjectType -> incr object_types
221-
| InstanceType i ->
222-
(* A class is identified by its name *)
223-
let name = Instance.to_string i in
224-
let count =
225-
match Hashtbl.find_opt instance_types name with
226-
| Some (_, count) -> count
227-
| None -> 0
228-
in
229-
Hashtbl.replace instance_types name (i, count + 1)
230-
| SymbolType -> incr symbol_types
231-
| FunctionType -> incr function_types
232-
| NumberType -> incr number_types
233-
| BigintType -> incr bigint_types
234-
| BooleanType ->
235-
(* Literal cases are matched before payload cases, so a boolean
236-
payload only receives the booleans that are not literal cases.
237-
A partial overlap is allowed, like for strings and numbers. *)
238-
if
239-
String_map.mem "true" !nonstring_literals_consts
240-
&& String_map.mem "false" !nonstring_literals_consts
241-
then
242-
raise
243-
(Error
244-
( loc,
245-
InvalidUntaggedVariantDefinition
246-
(UnreachableBoolean tag.name) ));
247-
incr boolean_types
248-
| StringType -> incr string_types);
249-
invariant loc tag.name)
196+
(* Literal cases are matched before payload cases, so a boolean
197+
payload only receives the booleans that are not literal cases. A
198+
partial overlap is allowed, like for strings and numbers. *)
199+
if
200+
block_type = BooleanType
201+
&& String_map.mem "true" !nonstring_literals_consts
202+
&& String_map.mem "false" !nonstring_literals_consts
203+
then
204+
raise
205+
(Error
206+
( loc,
207+
InvalidUntaggedVariantDefinition (UnreachableBoolean tag.name)
208+
));
209+
let key = kind_key block_type in
210+
if Hashtbl.mem kinds key then
211+
raise
212+
(Error
213+
( loc,
214+
InvalidUntaggedVariantDefinition (AtMostOneOfKind block_type)
215+
));
216+
Hashtbl.add kinds key ())
250217

251218
(* A constructor without [@as] is represented by its name *)
252219

0 commit comments

Comments
 (0)