Skip to content

Commit 35c5ab8

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 02d144c commit 35c5ab8

3 files changed

Lines changed: 151 additions & 151 deletions

File tree

‎compiler/ml/UntaggedVariants.md‎

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

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

‎compiler/ml/ast_untagged_variants.ml‎

Lines changed: 0 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -379,156 +379,6 @@ let is_nullary_variant (x : Types.constructor_arguments) =
379379
| Types.Cstr_tuple [] -> true
380380
| _ -> false
381381

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

‎compiler/ml/variant_layout.ml‎

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,156 @@ 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, untagged payloads by distinct kinds,
198+
checked as soon as one case is untagged *)
199+
let check_invariant ~is_untagged_def ~(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+
(* A tagged block is told apart from the others by its tag; an untagged one
286+
is its payload, whatever its [@as] *)
287+
Ext_list.rev_iter blocks (fun (loc, block) ->
288+
match block with
289+
| Tagged {tag} -> check_literal ~is_const:false ~loc tag
290+
| Untagged _ -> ());
291+
(* The payload kinds are checked as soon as one case is untagged, even when
292+
the type itself is not [@unboxed] *)
293+
let has_untagged_block =
294+
List.exists
295+
(function
296+
| _, Untagged _ -> true
297+
| _, Tagged _ -> false)
298+
blocks
299+
in
300+
if is_untagged_def || has_untagged_block then (
301+
(* Tagged blocks are objects with a tag field: together one object case *)
302+
if
303+
List.exists
304+
(function
305+
| _, Tagged _ -> true
306+
| _, Untagged _ -> false)
307+
blocks
308+
then incr object_types;
309+
Ext_list.rev_iter blocks (fun (loc, block) ->
310+
match block with
311+
| Untagged {tag; block_type} ->
312+
(match block_type with
313+
| UnknownType -> incr unknown_types
314+
| ObjectType -> incr object_types
315+
| InstanceType i ->
316+
(* A class is identified by its name *)
317+
let name = Instance.to_string i in
318+
let count =
319+
match Hashtbl.find_opt instance_types name with
320+
| Some (_, count) -> count
321+
| None -> 0
322+
in
323+
Hashtbl.replace instance_types name (i, count + 1)
324+
| SymbolType -> incr symbol_types
325+
| FunctionType -> incr function_types
326+
| NumberType -> incr number_types
327+
| BigintType -> incr bigint_types
328+
| BooleanType ->
329+
(* Literal cases are matched before payload cases, so a boolean
330+
payload only receives the booleans that are not literal cases.
331+
A partial overlap is allowed, like for strings and numbers. *)
332+
if
333+
String_map.mem "true" !nonstring_literals_consts
334+
&& String_map.mem "false" !nonstring_literals_consts
335+
then
336+
raise
337+
(Error
338+
( loc,
339+
InvalidUntaggedVariantDefinition
340+
(UnreachableBoolean tag.name) ));
341+
incr boolean_types
342+
| StringType -> incr string_types);
343+
invariant loc tag.name
344+
| Tagged _ -> ()))
345+
196346
let layout_from_type_variant ~(configuration : configuration) ~env
197347
(cstrs : Types.constructor_declaration list) : Variant_runtime.layout =
198348
let get_block (cstr : Types.constructor_declaration) : block =

0 commit comments

Comments
 (0)