@@ -44,34 +44,52 @@ let scrape env ty =
4444 records the type at the definition type so for ['a option]
4545 it will always be [Tvar]
4646*)
47- let cannot_inhabit_none_like_value (typ : Types.type_expr ) (env : Env.t ) =
47+ let rec type_cannot_contain_undefined (typ : Types.type_expr ) (env : Env.t ) =
4848 match scrape env typ with
4949 | Tconstr (p , _ ,_ ) ->
5050 (* all built in types could not inhabit none-like values:
5151 int, char, float, bool, unit, exn, array, list, nativeint,
5252 int32, int64, lazy_t, bytes
5353 *)
5454 (match Predef. type_is_builtin_path_but_option p with
55- | For_sure_yes -> true
55+ | For_sure_yes -> true
5656 | For_sure_no -> false
57- | NA ->
58-
59- begin match (Env. find_type p env).type_kind with
57+ | NA ->
58+ let untagged = ref false in
59+ begin match
60+ let decl = Env. find_type p env in
61+ let () =
62+ if Ast_untagged_variants. has_untagged decl.type_attributes
63+ then untagged := true in
64+ decl.type_kind with
6065 | exception _ ->
6166 false
62- | Types. Type_abstract | Types. Type_open -> false
63- | Types. Type_record _ -> true
64- | ( Types. Type_variant
67+ | Type_abstract | Type_open -> false
68+ | Type_record _ -> true
69+ | Type_variant
6570 ([{cd_id = {name= " None" }; cd_args = Cstr_tuple [] };
6671 {cd_id = {name = " Some" }; cd_args = Cstr_tuple [_]}]
6772 |
6873 [{cd_id = {name= " Some" }; cd_args = Cstr_tuple [_] };
6974 {cd_id = {name = " None" }; cd_args = Cstr_tuple [] }]
7075 | [{cd_id= {name = " ()" }; cd_args = Cstr_tuple [] }]
71- ))
72- (* | Types.Type_variant *)
76+ )
7377 -> false (* conservative *)
74- | _ -> true
78+ | Type_variant cdecls ->
79+ Ext_list. for_all cdecls (fun cd ->
80+ if Ast_untagged_variants. has_undefined_literal cd.cd_attributes
81+ then false
82+ else if ! untagged then
83+ match cd.cd_args with
84+ | Cstr_tuple [t] ->
85+ Ast_untagged_variants. type_is_builtin_object t || type_cannot_contain_undefined t env
86+ | Cstr_tuple [] -> true
87+ | Cstr_tuple (_ ::_ ::_ ) -> true (* Not actually possible for untagged *)
88+ | Cstr_record [{ld_type= t}] ->
89+ Ast_untagged_variants. type_is_builtin_object t || type_cannot_contain_undefined t env
90+ | Cstr_record ([] | _ ::_ ::_ ) -> true
91+ else
92+ true )
7593 end )
7694 | Ttuple _
7795 | Tvariant _
0 commit comments