Skip to content

Commit 5c1f6e3

Browse files
Merge pull request #558 from patricoferris/5.3-bump
5.3 AST bump
2 parents 53e4fa3 + 9793e46 commit 5c1f6e3

File tree

18 files changed

+355
-178
lines changed

18 files changed

+355
-178
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
unreleased
22
----------
33

4+
- Bump ppxlib's AST to 5.3.0 (#558, @patricoferris)
5+
46
- Fix 5.2 -> 5.3 migration of constants. Those used to always have a `none`
57
location which can lead to unhelpful error messages.
68
(#569, @NathanReb)

ast/ast.ml

+215-131
Large diffs are not rendered by default.

ast/ast_helper_lite.ml

+13-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
open Stdlib0
1818
module Location = Astlib.Location
1919
module Longident = Astlib.Longident
20-
open Astlib.Ast_502
20+
open Astlib.Ast_503
2121

2222
[@@@warning "-9"]
2323

@@ -52,16 +52,20 @@ let protect_ref =
5252
let with_default_loc l f = protect_ref (R (default_loc, l)) f
5353

5454
module Const = struct
55-
let integer ?suffix i = Pconst_integer (i, suffix)
56-
let int ?suffix i = integer ?suffix (Int.to_string i)
57-
let int32 ?(suffix = 'l') i = integer ~suffix (Int32.to_string i)
58-
let int64 ?(suffix = 'L') i = integer ~suffix (Int64.to_string i)
59-
let nativeint ?(suffix = 'n') i = integer ~suffix (Nativeint.to_string i)
60-
let float ?suffix f = Pconst_float (f, suffix)
61-
let char c = Pconst_char c
55+
let mk ?(loc = !default_loc) pconst_desc = { pconst_desc; pconst_loc = loc }
56+
let integer ?loc ?suffix i = mk ?loc (Pconst_integer (i, suffix))
57+
let int ?loc ?suffix i = integer ?loc ?suffix (Int.to_string i)
58+
let int32 ?loc ?(suffix = 'l') i = integer ?loc ~suffix (Int32.to_string i)
59+
let int64 ?loc ?(suffix = 'L') i = integer ?loc ~suffix (Int64.to_string i)
60+
61+
let nativeint ?loc ?(suffix = 'n') i =
62+
integer ?loc ~suffix (Nativeint.to_string i)
63+
64+
let float ?loc ?suffix f = mk ?loc (Pconst_float (f, suffix))
65+
let char ?loc c = mk ?loc (Pconst_char c)
6266

6367
let string ?quotation_delimiter ?(loc = !default_loc) s =
64-
Pconst_string (s, loc, quotation_delimiter)
68+
mk ~loc (Pconst_string (s, loc, quotation_delimiter))
6569
end
6670

6771
module Attr = struct

ast/ast_helper_lite.mli

+9-8
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
(** Copy of Ast_helper from OCaml 4.14 with docstring related stuff removed *)
1717

18-
open Astlib.Ast_502
18+
open Astlib.Ast_503
1919
open Asttypes
2020
open Parsetree
2121

@@ -38,14 +38,15 @@ val with_default_loc : loc -> (unit -> 'a) -> 'a
3838
(** {1 Constants} *)
3939

4040
module Const : sig
41-
val char : char -> constant
41+
val mk : ?loc:loc -> constant_desc -> constant
42+
val char : ?loc:loc -> char -> constant
4243
val string : ?quotation_delimiter:string -> ?loc:loc -> string -> constant
43-
val integer : ?suffix:char -> string -> constant
44-
val int : ?suffix:char -> int -> constant
45-
val int32 : ?suffix:char -> int32 -> constant
46-
val int64 : ?suffix:char -> int64 -> constant
47-
val nativeint : ?suffix:char -> nativeint -> constant
48-
val float : ?suffix:char -> string -> constant
44+
val integer : ?loc:loc -> ?suffix:char -> string -> constant
45+
val int : ?loc:loc -> ?suffix:char -> int -> constant
46+
val int32 : ?loc:loc -> ?suffix:char -> int32 -> constant
47+
val int64 : ?loc:loc -> ?suffix:char -> int64 -> constant
48+
val nativeint : ?loc:loc -> ?suffix:char -> nativeint -> constant
49+
val float : ?loc:loc -> ?suffix:char -> string -> constant
4950
end
5051

5152
(** {1 Attributes} *)

ast/import.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
(*$ open Ast_cinaps_helpers $*)
88

9-
module Js = Versions.OCaml_502
9+
module Js = Versions.OCaml_503
1010
module Ocaml = Versions.OCaml_current
1111

1212
module Select_ast (Ocaml : Versions.OCaml_version) = struct

ast/location_error.ml

+6-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,12 @@ let of_extension (extension : Ast.extension) =
4747
| {
4848
pstr_desc =
4949
Pstr_eval
50-
({ pexp_desc = Pexp_constant (Pconst_string (msg, _, _)); _ }, []);
50+
( {
51+
pexp_desc =
52+
Pexp_constant { pconst_desc = Pconst_string (msg, _, _); _ };
53+
_;
54+
},
55+
[] );
5156
_;
5257
} ->
5358
msg

astlib/pprintast.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
(* Extensive Rewrite: Hongbo Zhang: University of Pennsylvania *)
2222
(* TODO more fine-grained precedence pretty-printing *)
2323

24-
open Ast_502
24+
open Ast_503
2525
open Asttypes
2626
open Format
2727
open Location
@@ -310,7 +310,8 @@ let rec longident f = function
310310

311311
let longident_loc f x = pp f "%a" longident x.txt
312312

313-
let constant f = function
313+
let constant f c =
314+
match c.pconst_desc with
314315
| Pconst_char i -> pp f "%C" i
315316
| Pconst_string (i, _, None) -> pp f "%S" i
316317
| Pconst_string (i, _, Some delim) -> pp f "{%s|%s|%s}" delim i delim

astlib/pprintast.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
(* *)
1414
(**************************************************************************)
1515

16-
open Ast_502
16+
open Ast_503
1717

1818
type space_formatter = (unit, Format.formatter, unit) format
1919

src/ast_builder.ml

+38-14
Original file line numberDiff line numberDiff line change
@@ -98,33 +98,57 @@ module Default = struct
9898
failwith
9999
"Ppxlib.Ast_builder.nonrec_type_declaration: don't use this function"
100100

101-
let eint ~loc t = pexp_constant ~loc (Pconst_integer (Int.to_string t, None))
102-
let echar ~loc t = pexp_constant ~loc (Pconst_char t)
103-
let estring ~loc t = pexp_constant ~loc (Pconst_string (t, loc, None))
104-
let efloat ~loc t = pexp_constant ~loc (Pconst_float (t, None))
101+
let eint ~loc t =
102+
pexp_constant ~loc
103+
(Ast_helper.Const.mk ~loc @@ Pconst_integer (Int.to_string t, None))
104+
105+
let echar ~loc t =
106+
pexp_constant ~loc (Ast_helper.Const.mk ~loc @@ Pconst_char t)
107+
108+
let estring ~loc t =
109+
pexp_constant ~loc (Ast_helper.Const.mk ~loc @@ Pconst_string (t, loc, None))
110+
111+
let efloat ~loc t =
112+
pexp_constant ~loc (Ast_helper.Const.mk ~loc @@ Pconst_float (t, None))
105113

106114
let eint32 ~loc t =
107-
pexp_constant ~loc (Pconst_integer (Int32.to_string t, Some 'l'))
115+
pexp_constant ~loc
116+
(Ast_helper.Const.mk ~loc @@ Pconst_integer (Int32.to_string t, Some 'l'))
108117

109118
let eint64 ~loc t =
110-
pexp_constant ~loc (Pconst_integer (Int64.to_string t, Some 'L'))
119+
pexp_constant ~loc
120+
(Ast_helper.Const.mk ~loc @@ Pconst_integer (Int64.to_string t, Some 'L'))
111121

112122
let enativeint ~loc t =
113-
pexp_constant ~loc (Pconst_integer (Nativeint.to_string t, Some 'n'))
123+
pexp_constant ~loc
124+
(Ast_helper.Const.mk ~loc
125+
@@ Pconst_integer (Nativeint.to_string t, Some 'n'))
126+
127+
let pint ~loc t =
128+
ppat_constant ~loc
129+
(Ast_helper.Const.mk ~loc @@ Pconst_integer (Int.to_string t, None))
130+
131+
let pchar ~loc t =
132+
ppat_constant ~loc (Ast_helper.Const.mk ~loc @@ Pconst_char t)
133+
134+
let pstring ~loc t =
135+
ppat_constant ~loc (Ast_helper.Const.mk ~loc @@ Pconst_string (t, loc, None))
114136

115-
let pint ~loc t = ppat_constant ~loc (Pconst_integer (Int.to_string t, None))
116-
let pchar ~loc t = ppat_constant ~loc (Pconst_char t)
117-
let pstring ~loc t = ppat_constant ~loc (Pconst_string (t, loc, None))
118-
let pfloat ~loc t = ppat_constant ~loc (Pconst_float (t, None))
137+
let pfloat ~loc t =
138+
ppat_constant ~loc (Ast_helper.Const.mk ~loc @@ Pconst_float (t, None))
119139

120140
let pint32 ~loc t =
121-
ppat_constant ~loc (Pconst_integer (Int32.to_string t, Some 'l'))
141+
ppat_constant ~loc
142+
(Ast_helper.Const.mk ~loc @@ Pconst_integer (Int32.to_string t, Some 'l'))
122143

123144
let pint64 ~loc t =
124-
ppat_constant ~loc (Pconst_integer (Int64.to_string t, Some 'L'))
145+
ppat_constant ~loc
146+
(Ast_helper.Const.mk ~loc @@ Pconst_integer (Int64.to_string t, Some 'L'))
125147

126148
let pnativeint ~loc t =
127-
ppat_constant ~loc (Pconst_integer (Nativeint.to_string t, Some 'n'))
149+
ppat_constant ~loc
150+
(Ast_helper.Const.mk ~loc
151+
@@ Pconst_integer (Nativeint.to_string t, Some 'n'))
128152

129153
let ebool ~loc t =
130154
pexp_construct ~loc (Located.lident ~loc (Bool.to_string t)) None

src/context_free.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,11 @@ class map_top_down ?(expect_mismatch_handler = Expect_mismatch_handler.nop)
680680
match expr with
681681
| None -> super#expression base_ctxt e
682682
| Some e -> self#expression base_ctxt e))
683-
| Pexp_constant (Pconst_integer (s, Some c)) -> (
683+
| Pexp_constant { pconst_desc = Pconst_integer (s, Some c); _ } -> (
684684
try expand_constant Integer c s
685685
with exn when embed_errors ->
686686
return (exn_to_error_extension EC.expression e exn))
687-
| Pexp_constant (Pconst_float (s, Some c)) -> (
687+
| Pexp_constant { pconst_desc = Pconst_float (s, Some c); _ } -> (
688688
try expand_constant Float c s
689689
with exn when embed_errors ->
690690
return (exn_to_error_extension EC.expression e exn))

src/gen/gen_ast_builder.ml

+13-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ let doc_comment_from_attribue (attr : attribute) =
2020
{
2121
pstr_desc =
2222
Pstr_eval
23-
({ pexp_desc = Pexp_constant (Pconst_string (s, _, _)); _ }, _);
23+
( {
24+
pexp_desc =
25+
Pexp_constant
26+
{ pconst_desc = Pconst_string (s, _, _); _ };
27+
_;
28+
},
29+
_ );
2430
_;
2531
};
2632
] ->
@@ -245,7 +251,12 @@ let floating_comment s =
245251
pstr_desc =
246252
Pstr_eval
247253
( {
248-
pexp_desc = Pexp_constant (Pconst_string (s, loc, None));
254+
pexp_desc =
255+
Pexp_constant
256+
{
257+
pconst_desc = Pconst_string (s, loc, None);
258+
pconst_loc = loc;
259+
};
249260
pexp_loc = loc;
250261
pexp_loc_stack = [];
251262
pexp_attributes = [];

src/pp_ast.ml

+4
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ class lift_simple_val =
205205
~lift_record:super#module_expr ~desc:mod_.pmod_desc
206206
~attrs:mod_.pmod_attributes mod_
207207

208+
method! constant c =
209+
self#lift_record_with_desc ~lift_desc:self#constant_desc
210+
~lift_record:super#constant ~desc:c.pconst_desc ~attrs:[] c
211+
208212
method! structure_item stri = self#structure_item_desc stri.pstr_desc
209213
method! signature_item sigi = self#signature_item_desc sigi.psig_desc
210214

src/utils.ml

+10-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ let get_odoc_contents_if_comment = function
2020
pstr_desc =
2121
Pstr_eval
2222
( {
23-
pexp_desc = Pexp_constant (Pconst_string (text, loc, _));
23+
pexp_desc =
24+
Pexp_constant
25+
{ pconst_desc = Pconst_string (text, loc, _); _ };
2426
_;
2527
},
2628
_ );
@@ -43,7 +45,13 @@ let prettify_odoc_attributes =
4345
let open Ast_builder.Default in
4446
let loc = Location.none in
4547
let delim = Some (Common.valid_string_constant_delimiter txt) in
46-
let expr = pexp_constant ~loc (Pconst_string (txt, loc, delim)) in
48+
let expr =
49+
pexp_constant ~loc
50+
{
51+
pconst_desc = Pconst_string (txt, loc, delim);
52+
pconst_loc = loc;
53+
}
54+
in
4755
{ attr with attr_payload = PStr [ pstr_eval ~loc expr [] ] }
4856
| None -> attr
4957
end

test/deriving/inline/foo-deriver/ppx_foo_deriver.ml

+6-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,12 @@ let () =
8989
(Extension.declare "foo" Expression Ast_pattern.__
9090
(fun ~loc ~path:_ _payload ->
9191
{
92-
pexp_desc = Pexp_constant (Pconst_string ("foo", loc, None));
92+
pexp_desc =
93+
Pexp_constant
94+
{
95+
pconst_desc = Pconst_string ("foo", loc, None);
96+
pconst_loc = loc;
97+
};
9398
pexp_loc = loc;
9499
pexp_attributes = [];
95100
pexp_loc_stack = [];

test/driver/standalone_run_as_ppx/print_stuff.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ let print_string s ~loc =
88
mk_expression ~loc (Pexp_ident { txt = Lident "print_endline"; loc })
99
in
1010
let string_exp =
11-
mk_expression ~loc (Pexp_constant (Pconst_string (s, loc, None)))
11+
mk_expression ~loc
12+
(Pexp_constant
13+
{ pconst_desc = Pconst_string (s, loc, None); pconst_loc = loc })
1214
in
1315
mk_expression ~loc (Pexp_apply (print_exp, [ (Nolabel, string_exp) ]))
1416

test/expansion_inside_payloads/test.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ open Ppxlib
77

88
let expr_description ~loc ~error expr =
99
match expr.pexp_desc with
10-
| Pexp_constant (Pconst_integer _) ->
10+
| Pexp_constant { pconst_desc = Pconst_integer _; _ } ->
1111
Ast_builder.Default.estring ~loc "Payload is an integer"
1212
| Pexp_extension _ ->
1313
Ast_builder.Default.estring ~loc "Payload is an extension point"

test/ppxlib-pp-ast/dune

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
(cram
22
(package ppxlib-tools)
3+
; Migrations sometimes make subtle differences in the output.
4+
; For example, locations that are marked "ghost".
5+
(enabled_if
6+
(>= %{ocaml_version} "5.3.0"))
37
(deps
48
%{bin:ppxlib-pp-ast}
59
(package ppxlib)))

test/ppxlib-pp-ast/show-locs.t

+24-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ Now how it's printed with the flag:
6565
; ppat_attributes = __attrs
6666
}
6767
; pvb_expr =
68-
{ pexp_desc = Pexp_constant (Pconst_integer ( "2", None))
68+
{ pexp_desc =
69+
Pexp_constant
70+
{ pconst_desc = Pconst_integer ( "2", None)
71+
; pconst_loc = l1c8..9
72+
}
6973
; pexp_loc = l1c8..9
7074
; pexp_loc_stack = __lstack
7175
; pexp_attributes = __attrs
@@ -189,7 +193,25 @@ original form as opposed to the default, condensed one shown above:
189193
; ppat_attributes = __attrs
190194
}
191195
; pvb_expr =
192-
{ pexp_desc = Pexp_constant (Pconst_integer ( "2", None))
196+
{ pexp_desc =
197+
Pexp_constant
198+
{ pconst_desc = Pconst_integer ( "2", None)
199+
; pconst_loc =
200+
{ loc_start =
201+
{ pos_fname = "test.ml"
202+
; pos_lnum = 1
203+
; pos_bol = 0
204+
; pos_cnum = 8
205+
}
206+
; loc_end =
207+
{ pos_fname = "test.ml"
208+
; pos_lnum = 1
209+
; pos_bol = 0
210+
; pos_cnum = 9
211+
}
212+
; loc_ghost = false
213+
}
214+
}
193215
; pexp_loc =
194216
{ loc_start =
195217
{ pos_fname = "test.ml"

0 commit comments

Comments
 (0)