Skip to content

Commit e7ff301

Browse files
authored
Merge pull request #100 from Octachron/Support_OCaml_5.4
Update for 5.4
2 parents 375d04d + 2ba9237 commit e7ff301

File tree

8 files changed

+50
-23
lines changed

8 files changed

+50
-23
lines changed

CHANGES.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ Unreleased
1414
type%import loc = Location.t
1515
```
1616

17+
* Support for OCaml 5.4 (???, @octachron)
18+
19+
1.11.0
20+
------
21+
1722
* Support for OCaml 5.2 (#94, @kit-ty-kate)
1823

1924
1.10.0

src/compat/gen.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ let include_table =
1717
; ("types_type_kind", [(5, 2); (4, 13)])
1818
; ("init_path", [(4, 9)])
1919
; ("env_lookup", [(4, 10)])
20-
; ("types_desc", [(4, 14)]) ]
20+
; ("types_desc", [(4, 14)])
21+
; ("tuple", [5,4])
22+
; ("longident", [5,4])
23+
]
2124

2225
let rec gen_compat real_version (f_prefix, version_list) =
2326
match version_list with

src/compat/longident_ge_000.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let longident_inj x = x
2+
let longident_proj x = x

src/compat/longident_ge_504.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let longident_inj = Location.mknoloc
2+
let longident_proj x = x.Location.txt

src/compat/tuple_ge_000.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let tuple f xs =
2+
Ppxlib.Ast_helper.Typ.tuple (List.map (fun x -> f x) xs)

src/compat/tuple_ge_504.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let tuple f xs =
2+
Ppxlib.Ast_helper.Typ.tuple (List.map (fun (_,x) -> f x) xs)

src/ppx_import.ml

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,17 @@ let lazy_env =
3333
Compat.init_path ();
3434
Ocaml_common.Compmisc.initial_env () )
3535

36-
let string_of_lid lid =
37-
let rec print lid acc =
38-
match lid with
39-
| Longident.Lident s -> s :: acc
40-
| Longident.Ldot (lid, id) -> print lid ("." :: id :: acc)
41-
| Longident.Lapply (la, lb) -> print la ("(" :: print lb (")" :: acc))
42-
in
43-
String.concat "" (print lid [])
36+
let string_of_lid' lid = Format.asprintf "%a" Pprintast.longident lid
37+
let string_of_lid lid = Format.asprintf "%a" Astlib.Pprintast.longident lid
38+
39+
(* Remove locations *)
40+
let rec longident_without_loc =
41+
let proj x = Compat.longident_proj x in
42+
let conv x = longident_without_loc (proj x) in
43+
function
44+
| Longident.Ldot (x,y) -> Astlib.Longident.Ldot (conv x, proj y)
45+
| Longident.Lapply (f,x) -> Astlib.Longident.Lapply (conv f, conv x)
46+
| Longident.Lident x -> Astlib.Longident.Lident x
4447

4548
let try_find_module ~loc env lid =
4649
(* Note: we are careful to call `Env.lookup_module` and not
@@ -75,7 +78,7 @@ let try_find_module_type ~loc env lid =
7578
let error =
7679
Printf.sprintf
7780
"[%%import]: cannot access the signature of the abstract module %s"
78-
(string_of_lid lid)
81+
(string_of_lid' lid)
7982
in
8083
raise_error ~loc error
8184
| Some module_type -> module_type )
@@ -106,7 +109,7 @@ let locate_sig ~loc env lid =
106109
let head, path =
107110
try
108111
match Ppxlib.Longident.flatten_exn lid with
109-
| head :: path -> (Longident.Lident head, path)
112+
| head :: path -> (head, path)
110113
| _ -> assert false
111114
with Invalid_argument _ ->
112115
let error =
@@ -115,9 +118,9 @@ let locate_sig ~loc env lid =
115118
in
116119
raise_error ~loc error
117120
in
121+
let chead = Longident.Lident head in
118122
let head_module_type =
119-
match
120-
(try_find_module ~loc env head, lazy (try_find_module_type ~loc env head))
123+
match (try_find_module ~loc env chead, lazy (try_find_module_type ~loc env chead))
121124
with
122125
| Some mty, _ -> mty
123126
| None, (lazy (Some mty)) -> mty
@@ -147,10 +150,10 @@ let locate_sig ~loc env lid =
147150
let sub_module_type =
148151
loop (List.map Compat.migrate_signature_item sig_items)
149152
in
150-
(Longident.Ldot (lid, path_item), sub_module_type)
153+
(Astlib.Longident.Ldot(lid,path_item), sub_module_type)
151154
in
152155
let _lid, sub_module_type =
153-
List.fold_left get_sub_module_type (head, head_module_type) path
156+
List.fold_left get_sub_module_type (Astlib.Longident.Lident head, head_module_type) path
154157
in
155158
open_module_type ~loc env lid sub_module_type
156159

@@ -193,7 +196,7 @@ let get_modtype_decl ~loc sig_items parent_lid elem =
193196
raise_error ~loc error
194197
| Some decl -> decl
195198

196-
let longident_of_path = Untypeast.lident_of_path
199+
let longident_of_path x = longident_without_loc (Untypeast.lident_of_path x)
197200

198201
let mknoloc (txt : 'a) : 'a Ppxlib.Location.loc =
199202
{txt; loc = Ppxlib.Location.none}
@@ -216,8 +219,7 @@ let rec core_type_of_type_expr ~subst (type_expr : Ocaml_common.Types.type_expr)
216219
| _ -> lhs
217220
in
218221
Ppxlib.Ast_helper.Typ.arrow label lhs (core_type_of_type_expr ~subst rhs)
219-
| Ttuple xs ->
220-
Ppxlib.Ast_helper.Typ.tuple (List.map (core_type_of_type_expr ~subst) xs)
222+
| Ttuple xs -> Compat.tuple (core_type_of_type_expr ~subst) xs
221223
| Tconstr (path, args, _) -> (
222224
let lid = longident_of_path path in
223225
let args = List.map (core_type_of_type_expr ~subst) args in
@@ -426,11 +428,11 @@ let type_declaration ~tool_name ~input_name (type_decl : Ppxlib.type_declaration
426428
(string_of_lid lid)
427429
in
428430
raise_error ~loc error
429-
| Lident _ as head_id ->
431+
| Lident hid ->
430432
(* In this case, we know for sure that the user intends this lident
431433
as a type name, so we use Typetexp.find_type and let the failure
432434
cases propagate to the user. *)
433-
Compat.find_type env ~loc head_id |> snd
435+
Compat.find_type env ~loc (Lident hid) |> snd
434436
| Ldot (parent_id, elem) ->
435437
let sig_items = locate_sig ~loc env parent_id in
436438
get_type_decl ~loc sig_items parent_id elem
@@ -531,11 +533,11 @@ let module_type ~tool_name ~input_name (package_type : Ppxlib.package_type) =
531533
(string_of_lid lid)
532534
in
533535
raise_error ~loc error
534-
| Longident.Lident _ as head_id ->
536+
| Longident.Lident head_id ->
535537
(* In this case, we know for sure that the user intends this lident
536538
as a module type name, so we use Typetexp.find_type and
537539
let the failure cases propagate to the user. *)
538-
Compat.find_modtype env ~loc head_id |> snd
540+
Compat.find_modtype env ~loc (Lident head_id) |> snd
539541
| Longident.Ldot (parent_id, elem) ->
540542
let sig_items = locate_sig ~loc env parent_id in
541543
get_modtype_decl ~loc sig_items parent_id elem

src_test/ppx_deriving/test_ppx_import.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ module Test_optional : S_optional = struct
4040
let f ?(opt = 0) () = ignore opt
4141
end
4242

43+
module Location = struct
44+
type t = Location.t
45+
let pp = Location.print_loc
46+
[%%import type 'a loc = 'a Location.loc [@@deriving show]]
47+
end
48+
4349
[%%import: type longident = Longident.t [@@deriving show]]
4450

4551
[%%import:
@@ -49,9 +55,12 @@ type package_type =
4955
core_type := (Parsetree.core_type [@printer Pprintast.core_type]);
5056
Asttypes.loc :=
5157
(Asttypes.loc [@polyprinter fun pp fmt x -> pp fmt x.Asttypes.txt]);
52-
Longident.t := (Longident.t [@printer pp_longident])] )
58+
Longident.t := (Longident.t [@printer pp_longident]);
59+
attributes := (Parsetree.attributes [@printer (fun _ _ -> ())])
60+
])
5361
[@@deriving show]]
5462

63+
5564
module type Hashable = [%import: (module Hashtbl.HashedType)]
5665

5766
[%%import: type self_t = Test_self_import.t]

0 commit comments

Comments
 (0)