Skip to content

Commit a84406a

Browse files
committed
Untagged variants: at most one number type , and at most one string type.
1 parent 8c3edee commit a84406a

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

jscomp/ml/ast_untagged_variants.ml

+25-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type untaggedError = OnlyOneUnknown | AtMostOneObject | AtMostOneArray | DuplicateLiteral of string
1+
type untaggedError = OnlyOneUnknown | AtMostOneObject | AtMostOneArray | AtMostOneString | AtMostOneNumber | DuplicateLiteral of string
22
type error =
33
| InvalidVariantAsAnnotation
44
| Duplicated_bs_as
@@ -22,6 +22,8 @@ let report_error ppf =
2222
| OnlyOneUnknown -> "An unknown case must be the only case with payloads."
2323
| AtMostOneObject -> "At most one case can be an object type."
2424
| AtMostOneArray -> "At most one case can be an array type."
25+
| AtMostOneString -> "At most one case can be a string type."
26+
| AtMostOneNumber -> "At most one case can be a number type (int or float)."
2527
| DuplicateLiteral s -> "Duplicate literal " ^ s ^ "."
2628
)
2729

@@ -146,9 +148,11 @@ let checkInvariant ~isUntaggedDef ~(consts : (Location.t * literal) list) ~(bloc
146148
let module StringSet = Set.Make(String) in
147149
let string_literals = ref StringSet.empty in
148150
let nonstring_literals = ref StringSet.empty in
149-
let arrays = ref 0 in
150-
let objects = ref 0 in
151-
let unknowns = ref 0 in
151+
let arrayTypes = ref 0 in
152+
let objectTypes = ref 0 in
153+
let stringTypes = ref 0 in
154+
let numberTypes = ref 0 in
155+
let unknownTypes = ref 0 in
152156
let addStringLiteral ~loc s =
153157
if StringSet.mem s !string_literals then
154158
raise (Error (loc, InvalidUntaggedVariantDefinition (DuplicateLiteral s)));
@@ -158,12 +162,16 @@ let checkInvariant ~isUntaggedDef ~(consts : (Location.t * literal) list) ~(bloc
158162
raise (Error (loc, InvalidUntaggedVariantDefinition (DuplicateLiteral s)));
159163
nonstring_literals := StringSet.add s !nonstring_literals in
160164
let invariant loc =
161-
if !unknowns <> 0 && (List.length blocks <> 1)
165+
if !unknownTypes <> 0 && (List.length blocks <> 1)
162166
then raise (Error (loc, InvalidUntaggedVariantDefinition OnlyOneUnknown));
163-
if !objects > 1
167+
if !objectTypes > 1
164168
then raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneObject));
165-
if !arrays > 1
169+
if !arrayTypes > 1
166170
then raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneArray));
171+
if !stringTypes > 1
172+
then raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneString));
173+
if !numberTypes > 1
174+
then raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneNumber));
167175
() in
168176
Ext_list.rev_iter consts (fun (loc, literal) -> match literal.literal_type with
169177
| Some (String s) ->
@@ -185,15 +193,21 @@ let checkInvariant ~isUntaggedDef ~(consts : (Location.t * literal) list) ~(bloc
185193
if isUntaggedDef then
186194
Ext_list.rev_iter blocks (fun (loc, block) -> match block.block_type with
187195
| Some Unknown ->
188-
incr unknowns;
196+
incr unknownTypes;
189197
invariant loc
190198
| Some Object ->
191-
incr objects;
199+
incr objectTypes;
192200
invariant loc
193201
| Some Array ->
194-
incr arrays;
202+
incr arrayTypes;
195203
invariant loc
196-
| _ -> ())
204+
| Some (IntType | FloatType) ->
205+
incr numberTypes;
206+
invariant loc
207+
| Some StringType ->
208+
incr stringTypes;
209+
invariant loc
210+
| None -> ())
197211

198212
let names_from_type_variant ?(isUntaggedDef=false) (cstrs : Types.constructor_declaration list) =
199213
let get_cstr_name (cstr: Types.constructor_declaration) =

0 commit comments

Comments
 (0)