@@ -91,6 +91,12 @@ type switch_names = {consts: tag array; blocks: block array}
91
91
92
92
let untagged = " unboxed"
93
93
94
+ let block_type_can_be_undefined = function
95
+ | IntType | StringType | FloatType | BigintType | BooleanType | InstanceType _
96
+ | FunctionType | ObjectType ->
97
+ false
98
+ | UnknownType -> true
99
+
94
100
let has_untagged (attrs : Parsetree.attributes ) =
95
101
Ext_list. exists attrs (function {txt} , _ -> txt = untagged)
96
102
@@ -328,23 +334,35 @@ let check_invariant ~is_untagged_def ~(consts : (Location.t * tag) list)
328
334
invariant loc block.tag.name
329
335
| None -> () )
330
336
337
+ let get_cstr_loc_tag (cstr : Types.constructor_declaration ) =
338
+ ( cstr.cd_loc,
339
+ {
340
+ name = Ident. name cstr.cd_id;
341
+ tag_type = process_tag_type cstr.cd_attributes;
342
+ } )
343
+
344
+ let constructor_declaration_from_constructor_description ~env
345
+ (cd : Types.constructor_description ) : Types. constructor_declaration option
346
+ =
347
+ match cd.cstr_res.desc with
348
+ | Tconstr (path , _ , _ ) -> (
349
+ match Env. find_type path env with
350
+ | {type_kind = Type_variant cstrs } ->
351
+ Ext_list. find_opt cstrs (fun cstr ->
352
+ if cstr.cd_id.name = cd.cstr_name then Some cstr else None )
353
+ | _ -> None )
354
+ | _ -> None
355
+
331
356
let names_from_type_variant ?(is_untagged_def = false ) ~env
332
357
(cstrs : Types.constructor_declaration list ) =
333
- let get_cstr_name (cstr : Types.constructor_declaration ) =
334
- ( cstr.cd_loc,
335
- {
336
- name = Ident. name cstr.cd_id;
337
- tag_type = process_tag_type cstr.cd_attributes;
338
- } )
339
- in
340
358
let get_block (cstr : Types.constructor_declaration ) : block =
341
- let tag = snd (get_cstr_name cstr) in
359
+ let tag = snd (get_cstr_loc_tag cstr) in
342
360
{tag; tag_name = get_tag_name cstr; block_type = get_block_type ~env cstr}
343
361
in
344
362
let consts, blocks =
345
363
Ext_list. fold_left cstrs ([] , [] ) (fun (consts , blocks ) cstr ->
346
364
if is_nullary_variant cstr.cd_args then
347
- (get_cstr_name cstr :: consts, blocks)
365
+ (get_cstr_loc_tag cstr :: consts, blocks)
348
366
else (consts, (cstr.cd_loc, get_block cstr) :: blocks))
349
367
in
350
368
check_invariant ~is_untagged_def ~consts ~blocks ;
0 commit comments