@@ -6,15 +6,28 @@ let can_coerce_path (path : Path.t) =
6
6
|| Path. same path Predef. path_int
7
7
|| Path. same path Predef. path_float
8
8
9
- let can_coerce_variant ~(path : Path.t )
9
+ let check_paths_same p1 p2 target_path =
10
+ Path. same p1 target_path && Path. same p2 target_path
11
+
12
+ let can_coerce_variant ~(path : Path.t ) ~unboxed
10
13
(constructors : Types.constructor_declaration list ) =
11
14
constructors
12
15
|> List. for_all (fun (c : Types.constructor_declaration ) ->
13
16
let args = c.cd_args in
14
- let payload = Ast_untagged_variants. process_tag_type c.cd_attributes in
17
+ let asPayload =
18
+ Ast_untagged_variants. process_tag_type c.cd_attributes
19
+ in
15
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
16
27
| Cstr_tuple [] -> (
17
- match payload with
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
18
31
| None | Some (String _ ) -> Path. same path Predef. path_string
19
32
| Some (Int _ ) -> Path. same path Predef. path_int
20
33
| Some (Float _ ) -> Path. same path Predef. path_float
@@ -24,10 +37,10 @@ let can_coerce_variant ~(path : Path.t)
24
37
let can_try_coerce_variant_to_primitive
25
38
((_ , p , typedecl ) : Path. t * Path. t * Types. type_declaration ) =
26
39
match typedecl with
27
- | {type_kind = Type_variant constructors; type_params = [] }
40
+ | {type_kind = Type_variant constructors; type_params = [] ; type_attributes }
28
41
when Path. name p <> " bool" ->
29
42
(* bool is represented as a variant internally, so we need to account for that *)
30
- Some constructors
43
+ Some ( constructors, type_attributes |> Ast_untagged_variants. has_untagged)
31
44
| _ -> None
32
45
33
46
let variant_representation_matches (c1_attrs : Parsetree.attributes )
0 commit comments