Skip to content

Commit 265e7a2

Browse files
patricoferrisNathan Rebours
authored andcommitted
Support migrating external types
Signed-off-by: Patrick Ferris <patrick@sirref.org>
1 parent 69dcf50 commit 265e7a2

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

astlib/encoding_505.ml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module Ext_name = struct
22
let pexp_struct_item = "ppxlib.migration.pexp_struct_item_505"
33
let ptyp_functor = "ppxlib.migration.ptyp_functor_505"
44
let ppat_unpack = "ppxlib.migration.ppat_unpack_505"
5+
let ptype_kind_external = "ppxlib.migration.ptype_kind_external_505"
56
end
67

78
let invalid_encoding ~loc name =
@@ -57,4 +58,36 @@ module To_504 = struct
5758
} ->
5859
(arg, attr.attr_name, pkg, typ)
5960
| _ -> invalid_encoding ~loc Ext_name.ptyp_functor
61+
62+
let encode_ptype_kind_external name =
63+
let name_attr =
64+
{
65+
attr_name = { txt = name; loc = Location.none };
66+
attr_loc = Location.none;
67+
attr_payload = PStr [];
68+
}
69+
in
70+
let si =
71+
{ pstr_desc = Pstr_attribute name_attr; pstr_loc = Location.none }
72+
in
73+
{
74+
attr_name = { txt = Ext_name.ptype_kind_external; loc = Location.none };
75+
attr_loc = Location.none;
76+
attr_payload = PStr [ si ];
77+
}
78+
79+
let decode_ptype_kind_external = function
80+
| {
81+
attr_name = { txt; _ };
82+
attr_payload =
83+
PStr
84+
[
85+
{
86+
pstr_desc = Pstr_attribute { attr_name = { txt = name; _ }; _ };
87+
};
88+
];
89+
}
90+
when String.equal txt Ext_name.ptype_kind_external ->
91+
Some name
92+
| _ -> None
6093
end

astlib/migrate_504_505.ml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,22 @@ and copy_type_declaration :
623623
Ast_504.Parsetree.ptype_attributes;
624624
Ast_504.Parsetree.ptype_loc;
625625
} ->
626+
let external_name, ptype_attributes =
627+
let rec aux so_far = function
628+
| [] -> (None, List.rev so_far)
629+
| Ast_504.Parsetree.({ attr_name = { txt; _ }; _ } as attr) :: rest
630+
when String.equal txt Encoding_505.Ext_name.ptype_kind_external ->
631+
( Encoding_505.To_504.decode_ptype_kind_external attr,
632+
List.rev_append so_far rest )
633+
| attr :: rest -> aux (attr :: so_far) rest
634+
in
635+
aux [] ptype_attributes
636+
in
637+
let ptype_kind =
638+
match external_name with
639+
| Some name -> Ast_505.Parsetree.Ptype_external name
640+
| None -> copy_type_kind ptype_kind
641+
in
626642
{
627643
Ast_505.Parsetree.ptype_name = copy_loc (fun x -> x) ptype_name;
628644
Ast_505.Parsetree.ptype_params =
@@ -639,7 +655,7 @@ and copy_type_declaration :
639655
let x0, x1, x2 = x in
640656
(copy_core_type x0, copy_core_type x1, copy_location x2))
641657
ptype_cstrs;
642-
Ast_505.Parsetree.ptype_kind = copy_type_kind ptype_kind;
658+
Ast_505.Parsetree.ptype_kind;
643659
Ast_505.Parsetree.ptype_private = copy_private_flag ptype_private;
644660
Ast_505.Parsetree.ptype_manifest = Option.map copy_core_type ptype_manifest;
645661
Ast_505.Parsetree.ptype_attributes = copy_attributes ptype_attributes;

astlib/migrate_505_504.ml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,13 @@ and copy_type_declaration :
617617
Ast_505.Parsetree.ptype_attributes;
618618
Ast_505.Parsetree.ptype_loc;
619619
} ->
620+
let ptype_attributes =
621+
match ptype_kind with
622+
| Ptype_external name ->
623+
let attr = Encoding_505.To_504.encode_ptype_kind_external name in
624+
attr :: copy_attributes ptype_attributes
625+
| _ -> copy_attributes ptype_attributes
626+
in
620627
{
621628
Ast_504.Parsetree.ptype_name = copy_loc (fun x -> x) ptype_name;
622629
Ast_504.Parsetree.ptype_params =
@@ -636,7 +643,7 @@ and copy_type_declaration :
636643
Ast_504.Parsetree.ptype_kind = copy_type_kind ptype_kind;
637644
Ast_504.Parsetree.ptype_private = copy_private_flag ptype_private;
638645
Ast_504.Parsetree.ptype_manifest = Option.map copy_core_type ptype_manifest;
639-
Ast_504.Parsetree.ptype_attributes = copy_attributes ptype_attributes;
646+
Ast_504.Parsetree.ptype_attributes;
640647
Ast_504.Parsetree.ptype_loc = copy_location ptype_loc;
641648
}
642649

@@ -649,7 +656,9 @@ and copy_type_kind : Ast_505.Parsetree.type_kind -> Ast_504.Parsetree.type_kind
649656
Ast_504.Parsetree.Ptype_record (List.map copy_label_declaration x0)
650657
| Ast_505.Parsetree.Ptype_open -> Ast_504.Parsetree.Ptype_open
651658
| Ast_505.Parsetree.Ptype_external x0 ->
652-
Location.raise_errorf "External types are not supported."
659+
(* Note that copy_type_declaration should handle this for us and we need
660+
only put _something_ here. *)
661+
Ast_504.Parsetree.Ptype_open
653662

654663
and copy_label_declaration :
655664
Ast_505.Parsetree.label_declaration -> Ast_504.Parsetree.label_declaration =

test/505_migrations/run.t

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ to encode this feature into attributes and this test, along with this comment,
6161
will need updated.
6262

6363
$ ./driver.exe test.ml --use-compiler-pp
64-
File "test.ml", line 1:
65-
Error: External types are not supported.
66-
[1]
64+
type t = external "t"
6765

6866
3. Ptyp_functor
6967

0 commit comments

Comments
 (0)