@@ -91,6 +91,12 @@ type switch_names = {consts: tag array; blocks: block array}
9191
9292let untagged = " unboxed"
9393
94+ let block_type_can_be_undefined = function
95+ | IntType | StringType | FloatType | BigintType | BooleanType | InstanceType _
96+ | FunctionType | ObjectType ->
97+ false
98+ | UnknownType -> true
99+
94100let has_untagged (attrs : Parsetree.attributes ) =
95101 Ext_list. exists attrs (function {txt} , _ -> txt = untagged)
96102
@@ -328,23 +334,35 @@ let check_invariant ~is_untagged_def ~(consts : (Location.t * tag) list)
328334 invariant loc block.tag.name
329335 | None -> () )
330336
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+
331356let names_from_type_variant ?(is_untagged_def = false ) ~env
332357 (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
340358 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
342360 {tag; tag_name = get_tag_name cstr; block_type = get_block_type ~env cstr}
343361 in
344362 let consts, blocks =
345363 Ext_list. fold_left cstrs ([] , [] ) (fun (consts , blocks ) cstr ->
346364 if is_nullary_variant cstr.cd_args then
347- (get_cstr_name cstr :: consts, blocks)
365+ (get_cstr_loc_tag cstr :: consts, blocks)
348366 else (consts, (cstr.cd_loc, get_block cstr) :: blocks))
349367 in
350368 check_invariant ~is_untagged_def ~consts ~blocks ;
0 commit comments