Skip to content

Commit 3ec844e

Browse files
cknittclaude
andcommitted
Move the variant invariant check next to the layout
check_invariant checks a layout, and Variant_layout, which derives the layout, is its only caller; Ast_untagged_variants keeps the attributes and errors. Its doc comment, which described another function, now says what it checks. 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 83ea3c1 commit 3ec844e

3 files changed

Lines changed: 132 additions & 131 deletions

File tree

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ A literal may equal a value of a payload case: `@as("a") A | S(string)`,
262262
`@as(true) A | B(bool)` are accepted. Rule 7 says which case such a value
263263
belongs to.
264264

265-
- Implementation: `ast_untagged_variants.ml:check_invariant`,
265+
- Implementation: `variant_layout.ml:check_invariant`,
266266
`variant_runtime.ml:literal_value_key`.
267267
- Tests: the fixtures above; `tests/tests/src/unboxed_variant_fold_test.res`
268268
(a number and a bigint literal with the same digits); `TaggedDuplicateLiteral.res`,

‎compiler/ml/ast_untagged_variants.ml‎

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -392,136 +392,6 @@ let is_nullary_variant (x : Types.constructor_arguments) =
392392
| Types.Cstr_tuple [] -> true
393393
| _ -> false
394394

395-
(* A literal's JavaScript value, which a [switch] compares with [===]. Ints
396-
are emitted as 32-bit numbers, so [1], [1.0] and [4294967297] are one
397-
number, while [1n] is a bigint. *)
398-
let check_invariant ~(consts : (Location.t * tag) list)
399-
~(blocks : (Location.t * block) list) =
400-
let module String_set = Set.Make (String) in
401-
let module String_map = Map.Make (String) in
402-
let string_literals_consts = ref String_set.empty in
403-
let string_literals_blocks = ref String_set.empty in
404-
(* Keyed by [literal_key], with the spelling of the literal *)
405-
let nonstring_literals_consts = ref String_map.empty in
406-
let nonstring_literals_blocks = ref String_map.empty in
407-
let instance_types = Hashtbl.create 1 in
408-
let function_types = ref 0 in
409-
let object_types = ref 0 in
410-
let string_types = ref 0 in
411-
let number_types = ref 0 in
412-
let bigint_types = ref 0 in
413-
let boolean_types = ref 0 in
414-
let symbol_types = ref 0 in
415-
let unknown_types = ref 0 in
416-
let add_string_literal ~is_const ~loc s =
417-
let set =
418-
if is_const then string_literals_consts else string_literals_blocks
419-
in
420-
if String_set.mem s !set then
421-
raise (Error (loc, InvalidUntaggedVariantDefinition (DuplicateLiteral s)));
422-
set := String_set.add s !set
423-
in
424-
let add_nonstring_literal ~is_const ~loc literal s =
425-
let map =
426-
if is_const then nonstring_literals_consts else nonstring_literals_blocks
427-
in
428-
let key = Variant_runtime.literal_value_key literal in
429-
(match String_map.find_opt key !map with
430-
| Some earlier when earlier = s ->
431-
raise (Error (loc, InvalidUntaggedVariantDefinition (DuplicateLiteral s)))
432-
| Some earlier ->
433-
raise
434-
(Error
435-
( loc,
436-
InvalidUntaggedVariantDefinition (SameLiteralValue (s, earlier)) ))
437-
| None -> ());
438-
map := String_map.add key s !map
439-
in
440-
let invariant loc name =
441-
if !unknown_types <> 0 && List.length blocks <> 1 then
442-
raise
443-
(Error (loc, InvalidUntaggedVariantDefinition (OnlyOneUnknown name)));
444-
if !object_types > 1 then
445-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneObject));
446-
Hashtbl.iter
447-
(fun _ (i, count) ->
448-
if count > 1 then
449-
raise
450-
(Error (loc, InvalidUntaggedVariantDefinition (AtMostOneInstance i))))
451-
instance_types;
452-
if !function_types > 1 then
453-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneFunction));
454-
if !string_types > 1 then
455-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneString));
456-
if !number_types > 1 then
457-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneNumber));
458-
if !bigint_types > 1 then
459-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneBigint));
460-
if !boolean_types > 1 then
461-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneBoolean));
462-
if !symbol_types > 1 then
463-
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneSymbol));
464-
()
465-
in
466-
let check_literal ~is_const ~loc (tag : tag) =
467-
match tag.literal with
468-
| String s -> add_string_literal ~is_const ~loc s
469-
| Int i as literal ->
470-
add_nonstring_literal ~is_const ~loc literal (string_of_int i)
471-
| Float f as literal -> add_nonstring_literal ~is_const ~loc literal f
472-
| BigInt i as literal ->
473-
add_nonstring_literal ~is_const ~loc literal (i ^ "n")
474-
| Bool b as literal ->
475-
add_nonstring_literal ~is_const ~loc literal
476-
(if b then "true" else "false")
477-
| Null as literal -> add_nonstring_literal ~is_const ~loc literal "null"
478-
| Undefined as literal ->
479-
add_nonstring_literal ~is_const ~loc literal "undefined"
480-
in
481-
482-
Ext_list.rev_iter consts (fun (loc, literal) ->
483-
check_literal ~is_const:true ~loc literal);
484-
(* The blocks of a variant are all tagged or, when the type is [@unboxed],
485-
all untagged. A tagged block is told apart from the others by its tag; an
486-
untagged one by the kind of its payload, whatever its [@as]. *)
487-
Ext_list.rev_iter blocks (fun (loc, block) ->
488-
match block with
489-
| Tagged {tag} -> check_literal ~is_const:false ~loc tag
490-
| Untagged {tag; block_type} ->
491-
(match block_type with
492-
| UnknownType -> incr unknown_types
493-
| ObjectType -> incr object_types
494-
| InstanceType i ->
495-
(* A class is identified by its name *)
496-
let name = Instance.to_string i in
497-
let count =
498-
match Hashtbl.find_opt instance_types name with
499-
| Some (_, count) -> count
500-
| None -> 0
501-
in
502-
Hashtbl.replace instance_types name (i, count + 1)
503-
| SymbolType -> incr symbol_types
504-
| FunctionType -> incr function_types
505-
| NumberType -> incr number_types
506-
| BigintType -> incr bigint_types
507-
| BooleanType ->
508-
(* Literal cases are matched before payload cases, so a boolean
509-
payload only receives the booleans that are not literal cases.
510-
A partial overlap is allowed, like for strings and numbers. *)
511-
if
512-
String_map.mem "true" !nonstring_literals_consts
513-
&& String_map.mem "false" !nonstring_literals_consts
514-
then
515-
raise
516-
(Error
517-
( loc,
518-
InvalidUntaggedVariantDefinition
519-
(UnreachableBoolean tag.name) ));
520-
incr boolean_types
521-
| StringType -> incr string_types);
522-
invariant loc tag.name)
523-
524-
(* A constructor without [@as] is represented by its name *)
525395
let declared_tag (cstr : Types.constructor_declaration) =
526396
let name = Ident.name cstr.cd_id in
527397
{name; literal = Option.value cstr.cd_runtime_tag ~default:(String name)}

‎compiler/ml/variant_layout.ml‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,137 @@ let get_block_type ~env (cstr : Types.constructor_declaration) : block_type =
193193
(* Nullary constructors are handled before computing a payload shape. *)
194194
assert false
195195

196+
(* The cases of a variant must be told apart at run time: nullary cases and
197+
tagged blocks by distinct literals, the untagged payloads of an [@unboxed]
198+
variant by distinct kinds *)
199+
let check_invariant ~(consts : (Location.t * tag) list)
200+
~(blocks : (Location.t * block) list) =
201+
let module String_set = Set.Make (String) in
202+
let module String_map = Map.Make (String) in
203+
let string_literals_consts = ref String_set.empty in
204+
let string_literals_blocks = ref String_set.empty in
205+
(* Keyed by [literal_key], with the spelling of the literal *)
206+
let nonstring_literals_consts = ref String_map.empty in
207+
let nonstring_literals_blocks = ref String_map.empty in
208+
let instance_types = Hashtbl.create 1 in
209+
let function_types = ref 0 in
210+
let object_types = ref 0 in
211+
let string_types = ref 0 in
212+
let number_types = ref 0 in
213+
let bigint_types = ref 0 in
214+
let boolean_types = ref 0 in
215+
let symbol_types = ref 0 in
216+
let unknown_types = ref 0 in
217+
let add_string_literal ~is_const ~loc s =
218+
let set =
219+
if is_const then string_literals_consts else string_literals_blocks
220+
in
221+
if String_set.mem s !set then
222+
raise (Error (loc, InvalidUntaggedVariantDefinition (DuplicateLiteral s)));
223+
set := String_set.add s !set
224+
in
225+
let add_nonstring_literal ~is_const ~loc literal s =
226+
let map =
227+
if is_const then nonstring_literals_consts else nonstring_literals_blocks
228+
in
229+
let key = Variant_runtime.literal_value_key literal in
230+
(match String_map.find_opt key !map with
231+
| Some earlier when earlier = s ->
232+
raise (Error (loc, InvalidUntaggedVariantDefinition (DuplicateLiteral s)))
233+
| Some earlier ->
234+
raise
235+
(Error
236+
( loc,
237+
InvalidUntaggedVariantDefinition (SameLiteralValue (s, earlier)) ))
238+
| None -> ());
239+
map := String_map.add key s !map
240+
in
241+
let invariant loc name =
242+
if !unknown_types <> 0 && List.length blocks <> 1 then
243+
raise
244+
(Error (loc, InvalidUntaggedVariantDefinition (OnlyOneUnknown name)));
245+
if !object_types > 1 then
246+
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneObject));
247+
Hashtbl.iter
248+
(fun _ (i, count) ->
249+
if count > 1 then
250+
raise
251+
(Error (loc, InvalidUntaggedVariantDefinition (AtMostOneInstance i))))
252+
instance_types;
253+
if !function_types > 1 then
254+
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneFunction));
255+
if !string_types > 1 then
256+
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneString));
257+
if !number_types > 1 then
258+
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneNumber));
259+
if !bigint_types > 1 then
260+
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneBigint));
261+
if !boolean_types > 1 then
262+
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneBoolean));
263+
if !symbol_types > 1 then
264+
raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneSymbol));
265+
()
266+
in
267+
let check_literal ~is_const ~loc (tag : tag) =
268+
match tag.literal with
269+
| String s -> add_string_literal ~is_const ~loc s
270+
| Int i as literal ->
271+
add_nonstring_literal ~is_const ~loc literal (string_of_int i)
272+
| Float f as literal -> add_nonstring_literal ~is_const ~loc literal f
273+
| BigInt i as literal ->
274+
add_nonstring_literal ~is_const ~loc literal (i ^ "n")
275+
| Bool b as literal ->
276+
add_nonstring_literal ~is_const ~loc literal
277+
(if b then "true" else "false")
278+
| Null as literal -> add_nonstring_literal ~is_const ~loc literal "null"
279+
| Undefined as literal ->
280+
add_nonstring_literal ~is_const ~loc literal "undefined"
281+
in
282+
283+
Ext_list.rev_iter consts (fun (loc, literal) ->
284+
check_literal ~is_const:true ~loc literal);
285+
(* The blocks of a variant are all tagged or, when the type is [@unboxed],
286+
all untagged. A tagged block is told apart from the others by its tag; an
287+
untagged one by the kind of its payload, whatever its [@as]. *)
288+
Ext_list.rev_iter blocks (fun (loc, block) ->
289+
match block with
290+
| Tagged {tag} -> check_literal ~is_const:false ~loc tag
291+
| Untagged {tag; block_type} ->
292+
(match block_type with
293+
| UnknownType -> incr unknown_types
294+
| ObjectType -> incr object_types
295+
| InstanceType i ->
296+
(* A class is identified by its name *)
297+
let name = Instance.to_string i in
298+
let count =
299+
match Hashtbl.find_opt instance_types name with
300+
| Some (_, count) -> count
301+
| None -> 0
302+
in
303+
Hashtbl.replace instance_types name (i, count + 1)
304+
| SymbolType -> incr symbol_types
305+
| FunctionType -> incr function_types
306+
| NumberType -> incr number_types
307+
| BigintType -> incr bigint_types
308+
| BooleanType ->
309+
(* Literal cases are matched before payload cases, so a boolean
310+
payload only receives the booleans that are not literal cases.
311+
A partial overlap is allowed, like for strings and numbers. *)
312+
if
313+
String_map.mem "true" !nonstring_literals_consts
314+
&& String_map.mem "false" !nonstring_literals_consts
315+
then
316+
raise
317+
(Error
318+
( loc,
319+
InvalidUntaggedVariantDefinition
320+
(UnreachableBoolean tag.name) ));
321+
incr boolean_types
322+
| StringType -> incr string_types);
323+
invariant loc tag.name)
324+
325+
(* A constructor without [@as] is represented by its name *)
326+
196327
let layout_from_type_variant ~(configuration : configuration) ~env
197328
(cstrs : Types.constructor_declaration list) : Variant_runtime.layout =
198329
let get_block (cstr : Types.constructor_declaration) : block =

0 commit comments

Comments
 (0)