@@ -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 ) ->
@@ -544,8 +553,12 @@ and copy_structure_item_desc :
544553 | Ast_504.Parsetree. Pstr_primitive x0 ->
545554 Ast_503.Parsetree. Pstr_primitive (copy_value_description x0)
546555 | Ast_504.Parsetree. Pstr_type (x0 , x1 ) ->
547- Ast_503.Parsetree. Pstr_type
548- (copy_rec_flag x0, List. map copy_type_declaration x1)
556+ let rec_flag = copy_rec_flag x0 in
557+ (match copy_type_declaration_list x1 with
558+ | tds ->
559+ Ast_503.Parsetree. Pstr_type (rec_flag, tds)
560+ | exception Bivariant_param. Type_decl_list tds ->
561+ Encoding_504.To_503. encode_bivariant_pstr_type ~loc rec_flag tds)
549562 | Ast_504.Parsetree. Pstr_typext x0 ->
550563 Ast_503.Parsetree. Pstr_typext (copy_type_extension x0)
551564 | Ast_504.Parsetree. Pstr_exception x0 ->
@@ -570,6 +583,9 @@ and copy_structure_item_desc :
570583 | Ast_504.Parsetree. Pstr_extension (x0 , x1 ) ->
571584 Ast_503.Parsetree. Pstr_extension (copy_extension x0, copy_attributes x1)
572585
586+ and copy_structure_item_desc stri_d =
587+ copy_structure_item_desc_with_loc ~loc: Location. none stri_d
588+
573589and copy_include_declaration :
574590 Ast_504.Parsetree. include_declaration ->
575591 Ast_503.Parsetree. include_declaration =
@@ -1143,6 +1159,21 @@ and copy_extension_constructor_kind :
11431159 | Ast_504.Parsetree. Pext_rebind x0 ->
11441160 Ast_503.Parsetree. Pext_rebind (copy_loc copy_Longident_t x0)
11451161
1162+ and copy_type_params params =
1163+ let contains_bivariant_param = ref false in
1164+ let params' =
1165+ List. map
1166+ (fun (typ , (variance , injectivity )) ->
1167+ let typ' = copy_core_type typ in
1168+ let injectivity' = copy_injectivity injectivity in
1169+ try (typ', (copy_variance variance, injectivity'))
1170+ with Bivariant_param. T ->
1171+ contains_bivariant_param := true ;
1172+ Encoding_504.To_503. encode_bivariant_param typ' injectivity')
1173+ params
1174+ in
1175+ params', ! contains_bivariant_param
1176+
11461177and copy_type_declaration :
11471178 Ast_504.Parsetree. type_declaration -> Ast_503.Parsetree. type_declaration =
11481179 fun {
@@ -1155,28 +1186,46 @@ and copy_type_declaration :
11551186 Ast_504.Parsetree. ptype_attributes;
11561187 Ast_504.Parsetree. ptype_loc;
11571188 } ->
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- }
1189+ let params, contains_bivariant = copy_type_params ptype_params in
1190+ let td =
1191+ {
1192+ Ast_503.Parsetree. ptype_name = copy_loc (fun x -> x) ptype_name;
1193+ Ast_503.Parsetree. ptype_params = params;
1194+ Ast_503.Parsetree. ptype_cstrs =
1195+ List. map
1196+ (fun x ->
1197+ let x0, x1, x2 = x in
1198+ (copy_core_type x0, copy_core_type x1, copy_location x2))
1199+ ptype_cstrs;
1200+ Ast_503.Parsetree. ptype_kind = copy_type_kind ptype_kind;
1201+ Ast_503.Parsetree. ptype_private = copy_private_flag ptype_private;
1202+ Ast_503.Parsetree. ptype_manifest = 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
1208+ raise (Bivariant_param. Type_decl td)
1209+ else
1210+ td
1211+
1212+ and copy_type_declaration_list :
1213+ Ast_504.Parsetree. type_declaration list ->
1214+ Ast_503.Parsetree. type_declaration list = fun l ->
1215+ let contains_bivariant_param = ref false in
1216+ let tds =
1217+ List. map
1218+ (fun td ->
1219+ try copy_type_declaration td
1220+ with Bivariant_param. Type_decl td' ->
1221+ contains_bivariant_param := true ;
1222+ td')
1223+ l
1224+ in
1225+ if ! contains_bivariant_param then
1226+ raise (Bivariant_param. Type_decl_list tds)
1227+ else
1228+ tds
11801229
11811230and copy_private_flag :
11821231 Ast_504.Asttypes. private_flag -> Ast_503.Asttypes. private_flag = function
@@ -1253,7 +1302,7 @@ and copy_variance : Ast_504.Asttypes.variance -> Ast_503.Asttypes.variance =
12531302 | Ast_504.Asttypes. Covariant -> Ast_503.Asttypes. Covariant
12541303 | Ast_504.Asttypes. Contravariant -> Ast_503.Asttypes. Contravariant
12551304 | Ast_504.Asttypes. NoVariance -> Ast_503.Asttypes. NoVariance
1256- | Ast_504.Asttypes. Bivariant -> migration_error Location. none " bivariance "
1305+ | Ast_504.Asttypes. Bivariant -> raise Bivariant_param. T
12571306
12581307and copy_value_description :
12591308 Ast_504.Parsetree. value_description -> Ast_503.Parsetree. value_description =
0 commit comments