@@ -131,15 +131,13 @@ let check_invariant ~(consts : (Location.t * tag) list)
131131 (* Keyed by [literal_key], with the spelling of the literal *)
132132 let nonstring_literals_consts = ref String_map. empty in
133133 let nonstring_literals_blocks = ref String_map. empty in
134- let instance_types = Hashtbl. create 1 in
135- let function_types = ref 0 in
136- let object_types = ref 0 in
137- let string_types = ref 0 in
138- let number_types = ref 0 in
139- let bigint_types = ref 0 in
140- let boolean_types = ref 0 in
141- let symbol_types = ref 0 in
142- let unknown_types = ref 0 in
134+ (* The kinds of the untagged payloads seen so far. A class is identified by
135+ its name. *)
136+ let kinds = Hashtbl. create 1 in
137+ let kind_key = function
138+ | InstanceType (Class {name} ) -> InstanceType (Class {name; ancestors = [] })
139+ | kind -> kind
140+ in
143141 let add_string_literal ~is_const ~loc s =
144142 let set =
145143 if is_const then string_literals_consts else string_literals_blocks
@@ -164,32 +162,6 @@ let check_invariant ~(consts : (Location.t * tag) list)
164162 | None -> () );
165163 map := String_map. add key s ! map
166164 in
167- let invariant loc name =
168- if ! unknown_types <> 0 && List. length blocks <> 1 then
169- raise
170- (Error (loc, InvalidUntaggedVariantDefinition (OnlyOneUnknown name)));
171- if ! object_types > 1 then
172- raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneObject ));
173- Hashtbl. iter
174- (fun _ (i , count ) ->
175- if count > 1 then
176- raise
177- (Error (loc, InvalidUntaggedVariantDefinition (AtMostOneInstance i))))
178- instance_types;
179- if ! function_types > 1 then
180- raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneFunction ));
181- if ! string_types > 1 then
182- raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneString ));
183- if ! number_types > 1 then
184- raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneNumber ));
185- if ! bigint_types > 1 then
186- raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneBigint ));
187- if ! boolean_types > 1 then
188- raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneBoolean ));
189- if ! symbol_types > 1 then
190- raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneSymbol ));
191- ()
192- in
193165 let check_literal ~is_const ~loc (tag : tag ) =
194166 match tag.literal with
195167 | String s -> add_string_literal ~is_const ~loc s
@@ -214,39 +186,34 @@ let check_invariant ~(consts : (Location.t * tag) list)
214186 Ext_list. rev_iter blocks (fun (loc , block ) ->
215187 match block with
216188 | Tagged {tag} -> check_literal ~is_const: false ~loc tag
189+ | Untagged {tag; block_type = UnknownType } ->
190+ (* No check tells an unknown payload apart from another payload *)
191+ if List. length blocks <> 1 then
192+ raise
193+ (Error
194+ (loc, InvalidUntaggedVariantDefinition (OnlyOneUnknown tag.name)))
217195 | Untagged {tag; block_type} ->
218- (match block_type with
219- | UnknownType -> incr unknown_types
220- | ObjectType -> incr object_types
221- | InstanceType i ->
222- (* A class is identified by its name *)
223- let name = Instance. to_string i in
224- let count =
225- match Hashtbl. find_opt instance_types name with
226- | Some (_ , count ) -> count
227- | None -> 0
228- in
229- Hashtbl. replace instance_types name (i, count + 1 )
230- | SymbolType -> incr symbol_types
231- | FunctionType -> incr function_types
232- | NumberType -> incr number_types
233- | BigintType -> incr bigint_types
234- | BooleanType ->
235- (* Literal cases are matched before payload cases, so a boolean
236- payload only receives the booleans that are not literal cases.
237- A partial overlap is allowed, like for strings and numbers. *)
238- if
239- String_map. mem " true" ! nonstring_literals_consts
240- && String_map. mem " false" ! nonstring_literals_consts
241- then
242- raise
243- (Error
244- ( loc,
245- InvalidUntaggedVariantDefinition
246- (UnreachableBoolean tag.name) ));
247- incr boolean_types
248- | StringType -> incr string_types);
249- invariant loc tag.name)
196+ (* Literal cases are matched before payload cases, so a boolean
197+ payload only receives the booleans that are not literal cases. A
198+ partial overlap is allowed, like for strings and numbers. *)
199+ if
200+ block_type = BooleanType
201+ && String_map. mem " true" ! nonstring_literals_consts
202+ && String_map. mem " false" ! nonstring_literals_consts
203+ then
204+ raise
205+ (Error
206+ ( loc,
207+ InvalidUntaggedVariantDefinition (UnreachableBoolean tag.name)
208+ ));
209+ let key = kind_key block_type in
210+ if Hashtbl. mem kinds key then
211+ raise
212+ (Error
213+ ( loc,
214+ InvalidUntaggedVariantDefinition (AtMostOneOfKind block_type)
215+ ));
216+ Hashtbl. add kinds key () )
250217
251218(* A constructor without [@as] is represented by its name *)
252219
0 commit comments