@@ -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 *)
525395let 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)}
0 commit comments