Skip to content

Commit bf09be8

Browse files
author
Nathan Rebours
committed
Add support for 5.4 bivariant params in type_extension
Signed-off-by: Nathan Rebours <nathan.rebours@ocamlpro.com>
1 parent 406bfcc commit bf09be8

File tree

5 files changed

+67
-40
lines changed

5 files changed

+67
-40
lines changed

astlib/encoding_504.ml

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -390,38 +390,52 @@ module To_503 = struct
390390
| None, _ -> None
391391
| Some _, _ -> invalid_encoding ~loc:typ.ptyp_loc "bivariant type parameter"
392392

393-
let encode_bivariant_pstr_type ~loc rec_flag tds =
393+
let encode_bivariant_pstr ~loc pstr_desc =
394394
let loc = { loc with Location.loc_ghost = true } in
395395
let ext =
396396
( { txt = Ext_name.bivariant_pstr; loc },
397-
PStr [ { pstr_loc = loc; pstr_desc = Pstr_type (rec_flag, tds) } ] )
397+
PStr [ { pstr_loc = loc; pstr_desc } ] )
398398
in
399399
Pstr_extension (ext, [])
400400

401-
let encode_bivariant_psig_type ~loc rec_flag tds =
401+
let encode_bivariant_pstr_type ~loc rec_flag tds =
402+
encode_bivariant_pstr ~loc (Pstr_type (rec_flag, tds))
403+
404+
let encode_bivariant_pstr_typext ~loc te =
405+
encode_bivariant_pstr ~loc (Pstr_typext te)
406+
407+
let encode_bivariant_psig ~loc psig_desc =
402408
let loc = { loc with Location.loc_ghost = true } in
403409
let ext =
404410
( { txt = Ext_name.bivariant_psig; loc },
405-
PSig [ { psig_loc = loc; psig_desc = Psig_type (rec_flag, tds) } ] )
411+
PSig [ { psig_loc = loc; psig_desc } ] )
406412
in
407413
Psig_extension (ext, [])
408414

415+
let encode_bivariant_psig_type ~loc rec_flag tds =
416+
encode_bivariant_psig ~loc (Psig_type (rec_flag, tds))
417+
409418
let encode_bivariant_psig_typesubst ~loc tds =
410-
let loc = { loc with Location.loc_ghost = true } in
411-
let ext =
412-
( { txt = Ext_name.bivariant_psig; loc },
413-
PSig [ { psig_loc = loc; psig_desc = Psig_typesubst tds } ] )
414-
in
415-
Psig_extension (ext, [])
419+
encode_bivariant_psig ~loc (Psig_typesubst tds)
420+
421+
let encode_bivariant_psig_typext ~loc te =
422+
encode_bivariant_psig ~loc (Psig_typext te)
416423

417424
let decode_bivariant_pstr ~loc payload =
418425
match payload with
419-
| PStr [ { pstr_desc = Pstr_type _ as x; _ } ] -> x
426+
| PStr [ { pstr_desc = (Pstr_type _ | Pstr_typext _) as x; _ } ] -> x
420427
| _ -> invalid_encoding ~loc "bivariant structure_item"
421428

422429
let decode_bivariant_psig ~loc payload =
423430
match payload with
424-
| PSig [ { psig_desc = (Psig_type _ | Psig_typesubst _) as x; _ } ] -> x
431+
| PSig
432+
[
433+
{
434+
psig_desc = (Psig_type _ | Psig_typesubst _ | Psig_typext _) as x;
435+
_;
436+
};
437+
] ->
438+
x
425439
| _ -> invalid_encoding ~loc "bivariant signature_item"
426440
end
427441

astlib/encoding_504.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,18 @@ module To_503 : sig
4040
val encode_bivariant_pstr_type :
4141
loc:Location.t -> rec_flag -> type_declaration list -> structure_item_desc
4242

43+
val encode_bivariant_pstr_typext :
44+
loc:Location.t -> type_extension -> structure_item_desc
45+
4346
val encode_bivariant_psig_type :
4447
loc:Location.t -> rec_flag -> type_declaration list -> signature_item_desc
4548

4649
val encode_bivariant_psig_typesubst :
4750
loc:Location.t -> type_declaration list -> signature_item_desc
4851

52+
val encode_bivariant_psig_typext :
53+
loc:Location.t -> type_extension -> signature_item_desc
54+
4955
val decode_bivariant_pstr : loc:Location.t -> payload -> structure_item_desc
5056
val decode_bivariant_psig : loc:Location.t -> payload -> signature_item_desc
5157
end

astlib/migrate_503_504.ml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,14 +1140,7 @@ and copy_type_extension :
11401140
{
11411141
Ast_504.Parsetree.ptyext_path =
11421142
copy_loc (copy_Longident_t ~loc:ptyext_path.loc) ptyext_path;
1143-
Ast_504.Parsetree.ptyext_params =
1144-
List.map
1145-
(fun x ->
1146-
let x0, x1 = x in
1147-
( copy_core_type x0,
1148-
let x0, x1 = x1 in
1149-
(copy_variance x0, copy_injectivity x1) ))
1150-
ptyext_params;
1143+
Ast_504.Parsetree.ptyext_params = copy_type_params ptyext_params;
11511144
Ast_504.Parsetree.ptyext_constructors =
11521145
List.map copy_extension_constructor ptyext_constructors;
11531146
Ast_504.Parsetree.ptyext_private = copy_private_flag ptyext_private;

astlib/migrate_504_503.ml

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module To = Ast_503
55
module Bivariant_param = struct
66
type exn +=
77
| T
8+
| Type_ext of Ast_503.Parsetree.type_extension
89
| Type_decl of Ast_503.Parsetree.type_declaration
910
| Type_decl_list of Ast_503.Parsetree.type_declaration list
1011

@@ -558,8 +559,11 @@ and copy_structure_item_desc_with_loc ~loc :
558559
| tds -> Ast_503.Parsetree.Pstr_type (rec_flag, tds)
559560
| exception Bivariant_param.Type_decl_list tds ->
560561
Encoding_504.To_503.encode_bivariant_pstr_type ~loc rec_flag tds)
561-
| Ast_504.Parsetree.Pstr_typext x0 ->
562-
Ast_503.Parsetree.Pstr_typext (copy_type_extension x0)
562+
| Ast_504.Parsetree.Pstr_typext x0 -> (
563+
match copy_type_extension x0 with
564+
| ty_ext -> Ast_503.Parsetree.Pstr_typext ty_ext
565+
| exception Bivariant_param.Type_ext ty_ext ->
566+
Encoding_504.To_503.encode_bivariant_pstr_typext ~loc ty_ext)
563567
| Ast_504.Parsetree.Pstr_exception x0 ->
564568
Ast_503.Parsetree.Pstr_exception (copy_type_exception x0)
565569
| Ast_504.Parsetree.Pstr_module x0 ->
@@ -843,8 +847,11 @@ and copy_signature_item_desc_with_loc ~loc :
843847
| tds -> Ast_503.Parsetree.Psig_typesubst tds
844848
| exception Bivariant_param.Type_decl_list tds ->
845849
Encoding_504.To_503.encode_bivariant_psig_typesubst ~loc tds)
846-
| Ast_504.Parsetree.Psig_typext x0 ->
847-
Ast_503.Parsetree.Psig_typext (copy_type_extension x0)
850+
| Ast_504.Parsetree.Psig_typext x0 -> (
851+
match copy_type_extension x0 with
852+
| ty_ext -> Ast_503.Parsetree.Psig_typext ty_ext
853+
| exception Bivariant_param.Type_ext ty_ext ->
854+
Encoding_504.To_503.encode_bivariant_psig_typext ~loc ty_ext)
848855
| Ast_504.Parsetree.Psig_exception x0 ->
849856
Ast_503.Parsetree.Psig_exception (copy_type_exception x0)
850857
| Ast_504.Parsetree.Psig_module x0 ->
@@ -1125,22 +1132,19 @@ and copy_type_extension :
11251132
Ast_504.Parsetree.ptyext_loc;
11261133
Ast_504.Parsetree.ptyext_attributes;
11271134
} ->
1128-
{
1129-
Ast_503.Parsetree.ptyext_path = copy_loc copy_Longident_t ptyext_path;
1130-
Ast_503.Parsetree.ptyext_params =
1131-
List.map
1132-
(fun x ->
1133-
let x0, x1 = x in
1134-
( copy_core_type x0,
1135-
let x0, x1 = x1 in
1136-
(copy_variance x0, copy_injectivity x1) ))
1137-
ptyext_params;
1138-
Ast_503.Parsetree.ptyext_constructors =
1139-
List.map copy_extension_constructor ptyext_constructors;
1140-
Ast_503.Parsetree.ptyext_private = copy_private_flag ptyext_private;
1141-
Ast_503.Parsetree.ptyext_loc = copy_location ptyext_loc;
1142-
Ast_503.Parsetree.ptyext_attributes = copy_attributes ptyext_attributes;
1143-
}
1135+
let params, contains_bivariant = copy_type_params ptyext_params in
1136+
let te =
1137+
{
1138+
Ast_503.Parsetree.ptyext_path = copy_loc copy_Longident_t ptyext_path;
1139+
Ast_503.Parsetree.ptyext_params = params;
1140+
Ast_503.Parsetree.ptyext_constructors =
1141+
List.map copy_extension_constructor ptyext_constructors;
1142+
Ast_503.Parsetree.ptyext_private = copy_private_flag ptyext_private;
1143+
Ast_503.Parsetree.ptyext_loc = copy_location ptyext_loc;
1144+
Ast_503.Parsetree.ptyext_attributes = copy_attributes ptyext_attributes;
1145+
}
1146+
in
1147+
if contains_bivariant then raise (Bivariant_param.Type_ext te) else te
11441148

11451149
and copy_extension_constructor :
11461150
Ast_504.Parsetree.extension_constructor ->

test/encoding/504/migrations/run.t

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,36 @@ We also check that bivariant type parameters are correctly encoded and migrated:
4747

4848
$ cat > bivariant.ml << EOF
4949
> type +-'a t = A
50+
> type +-'a extensible += B
5051
> EOF
5152

5253
$ ./id_driver.exe bivariant.ml
5354
[%%ppxlib.migration.bivariant_str_item_5_4 type 'a t =
5455
| A ]
56+
[%%ppxlib.migration.bivariant_str_item_5_4 type 'a extensible +=
57+
| B ]
5558

5659
$ ./id_driver.exe bivariant.ml --use-compiler-pp
5760
type +-'a t =
5861
| A
62+
type +-'a extensible +=
63+
| B
5964
Bivariant are also correctly handled in a signature context:
6065

6166
$ cat > bivariant.mli << EOF
6267
> type +-'a t
6368
> type +-'a u := 'a v
69+
> type +-'a extensible += B
6470
> EOF
6571

6672
$ ./id_driver.exe bivariant.mli
6773
[%%ppxlib.migration.bivariant_sig_item_5_4 : type 'a t]
6874
[%%ppxlib.migration.bivariant_sig_item_5_4 : type 'a u := 'a v]
75+
[%%ppxlib.migration.bivariant_sig_item_5_4 : type 'a extensible +=
76+
| B ]
6977

7078
$ ./id_driver.exe bivariant.mli --use-compiler-pp
7179
type +-'a t
7280
type +-'a u := 'a v
81+
type +-'a extensible +=
82+
| B

0 commit comments

Comments
 (0)