@@ -9,30 +9,33 @@ let can_coerce_path (path : Path.t) =
9
9
let check_paths_same p1 p2 target_path =
10
10
Path. same p1 target_path && Path. same p2 target_path
11
11
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
36
39
37
40
let can_try_coerce_variant_to_primitive
38
41
((_ , p , typedecl ) : Path. t * Path. t * Types. type_declaration ) =
0 commit comments