Skip to content

Commit 95be068

Browse files
committed
refactor
1 parent e737fab commit 95be068

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

jscomp/ml/ctype.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3957,7 +3957,7 @@ let rec subtype_rec env trace t1 t2 cstrs =
39573957
(* type coercion for variants to primitives *)
39583958
(match Variant_coercion.can_try_coerce_variant_to_primitive (extract_concrete_typedecl env t1) with
39593959
| Some (constructors, unboxed) ->
3960-
if constructors |> Variant_coercion.can_coerce_variant ~path ~unboxed then
3960+
if constructors |> Variant_coercion.variant_has_same_runtime_representation_as_target ~targetPath:path ~unboxed then
39613961
cstrs
39623962
else
39633963
(trace, t1, t2, !univar_pairs)::cstrs

jscomp/ml/variant_coercion.ml

+27-24
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,33 @@ let can_coerce_path (path : Path.t) =
99
let check_paths_same p1 p2 target_path =
1010
Path.same p1 target_path && Path.same p2 target_path
1111

12-
let can_coerce_variant ~(path : Path.t) ~unboxed
13-
(constructors : Types.constructor_declaration list) =
14-
constructors
15-
|> List.for_all (fun (c : Types.constructor_declaration) ->
16-
let args = c.cd_args in
17-
let asPayload =
18-
Ast_untagged_variants.process_tag_type c.cd_attributes
19-
in
20-
match args with
21-
| Cstr_tuple [{desc = Tconstr (p, [], _)}] when unboxed ->
22-
let path_same = check_paths_same p path in
23-
(* unboxed String(string) :> string *)
24-
path_same Predef.path_string
25-
|| (* unboxed Number(float) :> float *)
26-
path_same Predef.path_float
27-
| Cstr_tuple [] -> (
28-
(* Check that @as payloads match with the target path to coerce to.
29-
No @as means the default encoding, which is string *)
30-
match asPayload with
31-
| None | Some (String _) -> Path.same path Predef.path_string
32-
| Some (Int _) -> Path.same path Predef.path_int
33-
| Some (Float _) -> Path.same path Predef.path_float
34-
| Some (Null | Undefined | Bool _ | Untagged _) -> false)
35-
| _ -> false)
12+
(* Checks if every case of the variant has the same runtime representation as the target type. *)
13+
let variant_has_same_runtime_representation_as_target ~(targetPath : Path.t)
14+
~unboxed (constructors : Types.constructor_declaration list) =
15+
(* Helper function to check if a constructor has the same runtime representation as the target type *)
16+
let has_same_runtime_representation (c : Types.constructor_declaration) =
17+
let args = c.cd_args in
18+
let asPayload = Ast_untagged_variants.process_tag_type c.cd_attributes in
19+
20+
match args with
21+
| Cstr_tuple [{desc = Tconstr (p, [], _)}] when unboxed ->
22+
let path_same = check_paths_same p targetPath in
23+
(* unboxed String(string) :> string *)
24+
path_same Predef.path_string
25+
|| (* unboxed Number(float) :> float *)
26+
path_same Predef.path_float
27+
| Cstr_tuple [] -> (
28+
(* Check that @as payloads match with the target path to coerce to.
29+
No @as means the default encoding, which is string *)
30+
match asPayload with
31+
| None | Some (String _) -> Path.same targetPath Predef.path_string
32+
| Some (Int _) -> Path.same targetPath Predef.path_int
33+
| Some (Float _) -> Path.same targetPath Predef.path_float
34+
| Some (Null | Undefined | Bool _ | Untagged _) -> false)
35+
| _ -> false
36+
in
37+
38+
List.for_all has_same_runtime_representation constructors
3639

3740
let can_try_coerce_variant_to_primitive
3841
((_, p, typedecl) : Path.t * Path.t * Types.type_declaration) =

0 commit comments

Comments
 (0)