Skip to content

Commit da56f8e

Browse files
committed
reuse logic from ast_untagged_variants
1 parent 1915b50 commit da56f8e

File tree

5 files changed

+21
-40
lines changed

5 files changed

+21
-40
lines changed

jscomp/core/matching_polyfill.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25+
let () = Ast_untagged_variants.extract_concrete_typedecl := Ctype.extract_concrete_typedecl
26+
2527
let names_from_construct_pattern (pat : Typedtree.pattern) =
2628
let rec resolve_path n (path : Path.t) =
2729
match Env.find_type path pat.pat_env with

jscomp/ml/ast_uncurried.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ let coreTypeIsUncurriedFun (typ : Parsetree.core_type) =
6969
true
7070
| _ -> false
7171

72-
let typeIsUncurriedFun (typ : Types.type_expr) =
73-
match typ.desc with
74-
| Tconstr (Pident {name = "function$"}, [{desc = Tarrow _}; _], _) ->
75-
true
76-
| _ -> false
77-
72+
let typeIsUncurriedFun = Ast_uncurried_utils.typeIsUncurriedFun
7873

7974
let typeExtractUncurriedFun (typ : Parsetree.core_type) =
8075
match typ.ptyp_desc with

jscomp/ml/ast_uncurried_utils.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
let typeIsUncurriedFun (typ : Types.type_expr) =
2+
match typ.desc with
3+
| Tconstr (Pident {name = "function$"}, [{desc = Tarrow _}; _], _) ->
4+
true
5+
| _ -> false

jscomp/ml/ast_untagged_variants.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ let process_untagged (attrs : Parsetree.attributes) =
7777
| _ -> ());
7878
!st
7979

80+
let extract_concrete_typedecl: (Env.t ->
81+
Types.type_expr ->
82+
Path.t * Path.t * Types.type_declaration) ref = ref (Obj.magic ())
83+
8084
let process_tag_type (attrs : Parsetree.attributes) =
8185
let st : tag_type option ref = ref None in
8286
Ext_list.iter attrs (fun ({txt; loc}, payload) ->
@@ -137,7 +141,7 @@ let get_block_type ~env (cstr : Types.constructor_declaration) :
137141
when Path.same path Predef.path_array ->
138142
Some ArrayType
139143
| true, Cstr_tuple [({desc = Tconstr _} as t)]
140-
when Ast_uncurried.typeIsUncurriedFun t ->
144+
when Ast_uncurried_utils.typeIsUncurriedFun t ->
141145
Some FunctionType
142146
| true, Cstr_tuple [{desc = Tarrow _}] -> Some FunctionType
143147
| true, Cstr_tuple [{desc = Tconstr (path, _, _)}]
@@ -148,7 +152,7 @@ let get_block_type ~env (cstr : Types.constructor_declaration) :
148152
Some ObjectType
149153
| true, Cstr_tuple [ty] -> (
150154
let default = Some UnknownType in
151-
match Ctype.extract_concrete_typedecl env ty with
155+
match !extract_concrete_typedecl env ty with
152156
| _, _, {type_kind = Type_record (_, Record_unboxed _)} -> default
153157
| _, _, {type_kind = Type_record (_, _)} -> Some ObjectType
154158
| _ -> default

jscomp/ml/variant_coercion.ml

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -63,57 +63,32 @@ let is_variant_typedecl
6363
| {type_kind = Type_variant constructors} -> Some constructors
6464
| _ -> None
6565

66-
let find_attribute_payload_as_string name attrs =
67-
match find_attribute_payload name attrs with
68-
| None -> None
69-
| Some payload -> Ast_payload.is_single_string payload
70-
7166
let variant_representation_matches (c1_attrs : Parsetree.attributes)
7267
(c2_attrs : Parsetree.attributes) =
7368
match
74-
(find_as_attribute_payload c1_attrs, find_as_attribute_payload c2_attrs)
69+
(Ast_untagged_variants.process_tag_type c1_attrs, Ast_untagged_variants.process_tag_type c2_attrs)
7570
with
7671
| None, None -> true
77-
| Some p1, Some p2 -> (
78-
let string_matches = match
79-
(Ast_payload.is_single_string p1, Ast_payload.is_single_string p2)
80-
with
81-
| Some (a, _), Some (b, _) when a = b -> true
82-
| _ -> false in
83-
if string_matches then true else
84-
let float_matches = match
85-
(Ast_payload.is_single_float p1, Ast_payload.is_single_float p2)
86-
with
87-
| Some a, Some b when a = b -> true
88-
| _ -> false in
89-
if float_matches then true else
90-
let int_matches = match
91-
(Ast_payload.is_single_int p1, Ast_payload.is_single_int p2)
92-
with
93-
| Some a, Some b when a = b -> true
94-
| _ -> false in
95-
if int_matches then true else
96-
false)
72+
| Some s1, Some s2 when s1 = s2 -> true
9773
| _ -> false
9874

9975
let variant_configuration_can_be_coerced (a1 : Parsetree.attributes)
10076
(a2 : Parsetree.attributes) =
10177
let unboxed =
10278
match
103-
(find_attribute_payload "unboxed" a1, find_attribute_payload "unboxed" a2)
79+
(Ast_untagged_variants.process_untagged a1, Ast_untagged_variants.process_untagged a2)
10480
with
105-
| Some (PStr []), Some (PStr []) -> true
106-
| None, None -> true
81+
| true, true | false, false -> true
10782
| _ -> false
10883
in
10984
if not unboxed then false
11085
else
11186
let tag =
11287
match
113-
( find_attribute_payload_as_string "tag" a1,
114-
find_attribute_payload_as_string "tag" a2 )
88+
(Ast_untagged_variants.process_tag_name a1,
89+
Ast_untagged_variants.process_tag_name a2 )
11590
with
116-
| Some (tag1, _), Some (tag2, _) when tag1 = tag2 -> true
91+
| Some (tag1), Some (tag2) when tag1 = tag2 -> true
11792
| None, None -> true
11893
| _ -> false
11994
in

0 commit comments

Comments
 (0)