Skip to content

Commit e769b96

Browse files
Octachronejgallego
authored andcommitted
[v1.x] Backport of "#100: Update for 5.4"
1 parent 485c152 commit e769b96

File tree

8 files changed

+53
-25
lines changed

8 files changed

+53
-25
lines changed

CHANGES.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
1.12.0
2+
------
3+
4+
* Support for OCaml 5.4 (#100, @octachron, backport #102, @egallego)
5+
16
1.11.0
27
------
38

4-
* Support for OCaml 5.2 (#94, @kit-ty-kate, backport to 1.x by
5-
@ejgallego #97)
9+
* Support for OCaml 5.2 (#94, @kit-ty-kate)
610

711
1.10.0
812
------

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: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,17 @@ let lazy_env =
2727
Compat.init_path ();
2828
Ocaml_common.Compmisc.initial_env () )
2929

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

3942
let try_find_module ~loc env lid =
4043
(* Note: we are careful to call `Env.lookup_module` and not
@@ -68,7 +71,7 @@ let try_find_module_type ~loc env lid =
6871
| None ->
6972
Location.raise_errorf ~loc
7073
"[%%import]: cannot access the signature of the abstract module %s"
71-
(string_of_lid lid)
74+
(string_of_lid' lid)
7275
| Some module_type -> module_type )
7376
with Not_found -> None
7477

@@ -93,12 +96,16 @@ let open_module_type ~loc env lid module_type =
9396
let locate_sig ~loc env lid =
9497
let head, path =
9598
match Ppxlib.Longident.flatten_exn lid with
96-
| head :: path -> (Longident.Lident head, path)
99+
| head :: path -> (head, path)
97100
| _ -> assert false
101+
| exception Invalid_argument _ ->
102+
Location.raise_errorf ~loc
103+
"[%%import] cannot import a functor application %s"
104+
(string_of_lid lid)
98105
in
106+
let chead = Longident.Lident head in
99107
let head_module_type =
100-
match
101-
(try_find_module ~loc env head, lazy (try_find_module_type ~loc env head))
108+
match (try_find_module ~loc env chead, lazy (try_find_module_type ~loc env chead))
102109
with
103110
| Some mty, _ -> mty
104111
| None, (lazy (Some mty)) -> mty
@@ -124,10 +131,10 @@ let locate_sig ~loc env lid =
124131
let sub_module_type =
125132
loop (List.map Compat.migrate_signature_item sig_items)
126133
in
127-
(Longident.Ldot (lid, path_item), sub_module_type)
134+
(Astlib.Longident.Ldot(lid,path_item), sub_module_type)
128135
in
129136
let _lid, sub_module_type =
130-
List.fold_left get_sub_module_type (head, head_module_type) path
137+
List.fold_left get_sub_module_type (Astlib.Longident.Lident head, head_module_type) path
131138
in
132139
open_module_type ~loc env lid sub_module_type
133140

@@ -164,7 +171,7 @@ let get_modtype_decl ~loc sig_items parent_lid elem =
164171
elem (string_of_lid parent_lid)
165172
| Some decl -> decl
166173

167-
let longident_of_path = Untypeast.lident_of_path
174+
let longident_of_path x = longident_without_loc (Untypeast.lident_of_path x)
168175

169176
let mknoloc (txt : 'a) : 'a Ppxlib.Location.loc =
170177
{txt; loc = Ppxlib.Location.none}
@@ -187,8 +194,7 @@ let rec core_type_of_type_expr ~subst (type_expr : Ocaml_common.Types.type_expr)
187194
| _ -> lhs
188195
in
189196
Ppxlib.Ast_helper.Typ.arrow label lhs (core_type_of_type_expr ~subst rhs)
190-
| Ttuple xs ->
191-
Ppxlib.Ast_helper.Typ.tuple (List.map (core_type_of_type_expr ~subst) xs)
197+
| Ttuple xs -> Compat.tuple (core_type_of_type_expr ~subst) xs
192198
| Tconstr (path, args, _) -> (
193199
let lid = longident_of_path path in
194200
let args = List.map (core_type_of_type_expr ~subst) args in
@@ -387,11 +393,11 @@ let type_declaration ~tool_name ~input_name (type_decl : Ppxlib.type_declaration
387393
Location.raise_errorf ~loc
388394
"[%%import] cannot import a functor application %s"
389395
(string_of_lid lid)
390-
| Lident _ as head_id ->
396+
| Lident hid ->
391397
(* In this case, we know for sure that the user intends this lident
392398
as a type name, so we use Typetexp.find_type and let the failure
393399
cases propagate to the user. *)
394-
Compat.find_type env ~loc head_id |> snd
400+
Compat.find_type env ~loc (Lident hid) |> snd
395401
| Ldot (parent_id, elem) ->
396402
let sig_items = locate_sig ~loc env parent_id in
397403
get_type_decl ~loc sig_items parent_id elem
@@ -484,11 +490,11 @@ let module_type ~tool_name ~input_name (package_type : Ppxlib.package_type) =
484490
Location.raise_errorf ~loc
485491
"[%%import] cannot import a functor application %s"
486492
(string_of_lid lid)
487-
| Longident.Lident _ as head_id ->
493+
| Longident.Lident head_id ->
488494
(* In this case, we know for sure that the user intends this lident
489495
as a module type name, so we use Typetexp.find_type and
490496
let the failure cases propagate to the user. *)
491-
Compat.find_modtype env ~loc head_id |> snd
497+
Compat.find_modtype env ~loc (Lident head_id) |> snd
492498
| Longident.Ldot (parent_id, elem) ->
493499
let sig_items = locate_sig ~loc env parent_id in
494500
get_modtype_decl ~loc sig_items parent_id elem

src_test/ppx_deriving/test_ppx_import.ml

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

41+
module Location = struct
42+
type t = Location.t
43+
let pp = Location.print_loc
44+
type 'a loc = [%import: 'a Location.loc] [@@deriving show]
45+
end
46+
4147
type longident = [%import: Longident.t] [@@deriving show]
4248

4349
type package_type =
@@ -47,7 +53,8 @@ type package_type =
4753
core_type := (Parsetree.core_type [@printer Pprintast.core_type]);
4854
Asttypes.loc :=
4955
(Asttypes.loc [@polyprinter fun pp fmt x -> pp fmt x.Asttypes.txt]);
50-
Longident.t := (Longident.t [@printer pp_longident])] )]
56+
Longident.t := (Longident.t [@printer pp_longident]);
57+
attributes := (Parsetree.attributes [@printer (fun _ _ -> ())])])]
5158
[@@deriving show]
5259

5360
module type Hashable = [%import: (module Hashtbl.HashedType)]

0 commit comments

Comments
 (0)