@@ -2,9 +2,16 @@ open Stdlib0
22module From = Ast_504
33module To = Ast_503
44
5- let migration_error loc missing_feature =
6- Location. raise_errorf ~loc
7- " migration error: %s are not supported before OCaml 5.4" missing_feature
5+ module Bivariant_param = struct
6+ type exn + =
7+ | T
8+ | Type_decl of Ast_503.Parsetree .type_declaration
9+ | Type_decl_list of Ast_503.Parsetree .type_declaration list
10+
11+ (* TODO: register exception printers to display those as location errors
12+ pointing to the right AST element and displaying a clear migration error
13+ message *)
14+ end
815
916let rec copy_toplevel_phrase :
1017 Ast_504.Parsetree. toplevel_phrase -> Ast_503.Parsetree. toplevel_phrase =
@@ -528,12 +535,14 @@ and copy_structure : Ast_504.Parsetree.structure -> Ast_503.Parsetree.structure
528535and copy_structure_item :
529536 Ast_504.Parsetree. structure_item -> Ast_503.Parsetree. structure_item =
530537 fun { Ast_504.Parsetree. pstr_desc; Ast_504.Parsetree. pstr_loc } ->
538+ let loc = copy_location pstr_loc in
531539 {
532- Ast_503.Parsetree. pstr_desc = copy_structure_item_desc pstr_desc;
533- Ast_503.Parsetree. pstr_loc = copy_location pstr_loc;
540+ Ast_503.Parsetree. pstr_desc =
541+ copy_structure_item_desc_with_loc ~loc pstr_desc;
542+ Ast_503.Parsetree. pstr_loc = loc;
534543 }
535544
536- and copy_structure_item_desc :
545+ and copy_structure_item_desc_with_loc ~ loc :
537546 Ast_504.Parsetree. structure_item_desc ->
538547 Ast_503.Parsetree. structure_item_desc = function
539548 | Ast_504.Parsetree. Pstr_eval (x0 , x1 ) ->
@@ -543,9 +552,12 @@ and copy_structure_item_desc :
543552 (copy_rec_flag x0, List. map copy_value_binding x1)
544553 | Ast_504.Parsetree. Pstr_primitive x0 ->
545554 Ast_503.Parsetree. Pstr_primitive (copy_value_description x0)
546- | Ast_504.Parsetree. Pstr_type (x0 , x1 ) ->
547- Ast_503.Parsetree. Pstr_type
548- (copy_rec_flag x0, List. map copy_type_declaration x1)
555+ | Ast_504.Parsetree. Pstr_type (x0 , x1 ) -> (
556+ let rec_flag = copy_rec_flag x0 in
557+ match copy_type_declaration_list x1 with
558+ | tds -> Ast_503.Parsetree. Pstr_type (rec_flag, tds)
559+ | exception Bivariant_param. Type_decl_list tds ->
560+ Encoding_504.To_503. encode_bivariant_pstr_type ~loc rec_flag tds)
549561 | Ast_504.Parsetree. Pstr_typext x0 ->
550562 Ast_503.Parsetree. Pstr_typext (copy_type_extension x0)
551563 | Ast_504.Parsetree. Pstr_exception x0 ->
@@ -570,6 +582,9 @@ and copy_structure_item_desc :
570582 | Ast_504.Parsetree. Pstr_extension (x0 , x1 ) ->
571583 Ast_503.Parsetree. Pstr_extension (copy_extension x0, copy_attributes x1)
572584
585+ and copy_structure_item_desc stri_d =
586+ copy_structure_item_desc_with_loc ~loc: Location. none stri_d
587+
573588and copy_include_declaration :
574589 Ast_504.Parsetree. include_declaration ->
575590 Ast_503.Parsetree. include_declaration =
@@ -1143,6 +1158,21 @@ and copy_extension_constructor_kind :
11431158 | Ast_504.Parsetree. Pext_rebind x0 ->
11441159 Ast_503.Parsetree. Pext_rebind (copy_loc copy_Longident_t x0)
11451160
1161+ and copy_type_params params =
1162+ let contains_bivariant_param = ref false in
1163+ let params' =
1164+ List. map
1165+ (fun (typ , (variance , injectivity )) ->
1166+ let typ' = copy_core_type typ in
1167+ let injectivity' = copy_injectivity injectivity in
1168+ try (typ', (copy_variance variance, injectivity'))
1169+ with Bivariant_param. T ->
1170+ contains_bivariant_param := true ;
1171+ Encoding_504.To_503. encode_bivariant_param typ' injectivity')
1172+ params
1173+ in
1174+ (params', ! contains_bivariant_param)
1175+
11461176and copy_type_declaration :
11471177 Ast_504.Parsetree. type_declaration -> Ast_503.Parsetree. type_declaration =
11481178 fun {
@@ -1155,28 +1185,43 @@ and copy_type_declaration :
11551185 Ast_504.Parsetree. ptype_attributes;
11561186 Ast_504.Parsetree. ptype_loc;
11571187 } ->
1158- {
1159- Ast_503.Parsetree. ptype_name = copy_loc (fun x -> x) ptype_name;
1160- Ast_503.Parsetree. ptype_params =
1161- List. map
1162- (fun x ->
1163- let x0, x1 = x in
1164- ( copy_core_type x0,
1165- let x0, x1 = x1 in
1166- (copy_variance x0, copy_injectivity x1) ))
1167- ptype_params;
1168- Ast_503.Parsetree. ptype_cstrs =
1169- List. map
1170- (fun x ->
1171- let x0, x1, x2 = x in
1172- (copy_core_type x0, copy_core_type x1, copy_location x2))
1173- ptype_cstrs;
1174- Ast_503.Parsetree. ptype_kind = copy_type_kind ptype_kind;
1175- Ast_503.Parsetree. ptype_private = copy_private_flag ptype_private;
1176- Ast_503.Parsetree. ptype_manifest = Option. map copy_core_type ptype_manifest;
1177- Ast_503.Parsetree. ptype_attributes = copy_attributes ptype_attributes;
1178- Ast_503.Parsetree. ptype_loc = copy_location ptype_loc;
1179- }
1188+ let params, contains_bivariant = copy_type_params ptype_params in
1189+ let td =
1190+ {
1191+ Ast_503.Parsetree. ptype_name = copy_loc (fun x -> x) ptype_name;
1192+ Ast_503.Parsetree. ptype_params = params;
1193+ Ast_503.Parsetree. ptype_cstrs =
1194+ List. map
1195+ (fun x ->
1196+ let x0, x1, x2 = x in
1197+ (copy_core_type x0, copy_core_type x1, copy_location x2))
1198+ ptype_cstrs;
1199+ Ast_503.Parsetree. ptype_kind = copy_type_kind ptype_kind;
1200+ Ast_503.Parsetree. ptype_private = copy_private_flag ptype_private;
1201+ Ast_503.Parsetree. ptype_manifest =
1202+ Option. map copy_core_type ptype_manifest;
1203+ Ast_503.Parsetree. ptype_attributes = copy_attributes ptype_attributes;
1204+ Ast_503.Parsetree. ptype_loc = copy_location ptype_loc;
1205+ }
1206+ in
1207+ if contains_bivariant then raise (Bivariant_param. Type_decl td) else td
1208+
1209+ and copy_type_declaration_list :
1210+ Ast_504.Parsetree. type_declaration list ->
1211+ Ast_503.Parsetree. type_declaration list =
1212+ fun l ->
1213+ let contains_bivariant_param = ref false in
1214+ let tds =
1215+ List. map
1216+ (fun td ->
1217+ try copy_type_declaration td
1218+ with Bivariant_param. Type_decl td' ->
1219+ contains_bivariant_param := true ;
1220+ td')
1221+ l
1222+ in
1223+ if ! contains_bivariant_param then raise (Bivariant_param. Type_decl_list tds)
1224+ else tds
11801225
11811226and copy_private_flag :
11821227 Ast_504.Asttypes. private_flag -> Ast_503.Asttypes. private_flag = function
@@ -1253,7 +1298,7 @@ and copy_variance : Ast_504.Asttypes.variance -> Ast_503.Asttypes.variance =
12531298 | Ast_504.Asttypes. Covariant -> Ast_503.Asttypes. Covariant
12541299 | Ast_504.Asttypes. Contravariant -> Ast_503.Asttypes. Contravariant
12551300 | Ast_504.Asttypes. NoVariance -> Ast_503.Asttypes. NoVariance
1256- | Ast_504.Asttypes. Bivariant -> migration_error Location. none " bivariance "
1301+ | Ast_504.Asttypes. Bivariant -> raise Bivariant_param. T
12571302
12581303and copy_value_description :
12591304 Ast_504.Parsetree. value_description -> Ast_503.Parsetree. value_description =
0 commit comments