@@ -44,34 +44,52 @@ let scrape env ty =
44
44
records the type at the definition type so for ['a option]
45
45
it will always be [Tvar]
46
46
*)
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 ) =
48
48
match scrape env typ with
49
49
| Tconstr (p , _ ,_ ) ->
50
50
(* all built in types could not inhabit none-like values:
51
51
int, char, float, bool, unit, exn, array, list, nativeint,
52
52
int32, int64, lazy_t, bytes
53
53
*)
54
54
(match Predef. type_is_builtin_path_but_option p with
55
- | For_sure_yes -> true
55
+ | For_sure_yes -> true
56
56
| 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
60
65
| exception _ ->
61
66
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
65
70
([{cd_id = {name= " None" }; cd_args = Cstr_tuple [] };
66
71
{cd_id = {name = " Some" }; cd_args = Cstr_tuple [_]}]
67
72
|
68
73
[{cd_id = {name= " Some" }; cd_args = Cstr_tuple [_] };
69
74
{cd_id = {name = " None" }; cd_args = Cstr_tuple [] }]
70
75
| [{cd_id= {name = " ()" }; cd_args = Cstr_tuple [] }]
71
- ))
72
- (* | Types.Type_variant *)
76
+ )
73
77
-> 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 )
75
93
end )
76
94
| Ttuple _
77
95
| Tvariant _
0 commit comments