@@ -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 *)
533383let declared_tag (cstr : Types.constructor_declaration ) =
534384 let name = Ident. name cstr.cd_id in
0 commit comments