Skip to content

Commit 3223f2d

Browse files
authored
Merge pull request #448 from gpetiot/5.2
Make mdx compatible with ocaml 5.2
2 parents b4f4f58 + 6c58ae5 commit 3223f2d

File tree

8 files changed

+31
-15
lines changed

8 files changed

+31
-15
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Handle the error-blocks syntax (#439, @jonludlam, @gpetiot)
66
- Allow execution of included OCaml code blocks (#446, @panglesd)
7+
- Make MDX compatible with OCaml 5.2 (#448, @gpetiot)
78

89
#### Fixed
910

lib/block.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,7 @@ let pp_contents ?syntax:_ ppf t =
177177

178178
let rec error_padding = function
179179
| [] -> []
180-
| [ (`Output _ as o); `Output padding ] when Util.String.all_blank padding ->
181-
[ o ]
180+
| [ o; `Output padding ] when Util.String.all_blank padding -> [ o ]
182181
| x :: xs ->
183182
let xs = error_padding xs in
184183
x :: xs

lib/top/compat_top.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,19 @@ let get_id_opt = function
280280
#if OCAML_VERSION >= (5, 1, 0)
281281
| Path.Pextra_ty _ -> None
282282
#endif
283+
284+
let mk_fun loc exp =
285+
let punit =
286+
Ast_helper.Pat.construct (Location.mkloc (Longident.Lident "()") loc) None
287+
in
288+
let label = Asttypes.Nolabel in
289+
let default = None in
290+
#if OCAML_VERSION >= (5, 2, 0)
291+
let param =
292+
{ Parsetree.pparam_loc= loc
293+
; pparam_desc= Pparam_val (label, default, punit) }
294+
in
295+
Ast_helper.Exp.function_ [param] None (Pfunction_body exp)
296+
#else
297+
Ast_helper.Exp.fun_ label default punit exp
298+
#endif

lib/top/compat_top.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,6 @@ val execute_phrase :
6969
val redirect_directive : string -> string
7070
val get_id_in_path : Path.t -> Ident.t
7171
val get_id_opt : Path.t -> Ident.t option
72+
73+
val mk_fun : Location.t -> Parsetree.expression -> Parsetree.expression
74+
(** [mk_fun loc e] produces [fun () -> e]. *)

lib/top/mdx_top.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,10 @@ module Rewrite = struct
196196
let preload = None in
197197
let open Ast_helper in
198198
let rewrite loc e =
199-
let punit =
200-
Pat.construct (Location.mkloc (Longident.Lident "()") loc) None
201-
in
202199
with_default_loc loc @@ fun () ->
203200
Exp.apply
204201
(Exp.ident (Location.mkloc runner loc))
205-
[ (Asttypes.Nolabel, Exp.fun_ Asttypes.Nolabel None punit e) ]
202+
[ (Asttypes.Nolabel, Compat_top.mk_fun loc e) ]
206203
in
207204
{ typ; rewrite; witness; preload }
208205

test/bin/mdx-test/expect/ocaml-errors-ellipsis/test-case.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
It is possible to use ellipsis (`...`) in the error blocks attached to OCaml blocks, here it is useful as the error message depends on the OCaml version:
22

3-
```ocaml
3+
```ocaml version<5.2
44
module Counter: Irmin.Contents.S with type t = int64 = struct
55
type t = int64
66
let t = Irmin.Type.int64

test/bin/mdx-test/expect/ocaml-errors/test-case.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ Error: This 'struct' might be unmatched
1414
```
1515

1616

17-
```ocaml version>=4.08
17+
```ocaml version>=5.2
1818
module Counter: Irmin.Contents.S with type t = int64 = struct
1919
type t = int64
2020
let t = Irmin.Type.int64
2121
```
2222
```mdx-error
2323
Line 4, characters 3-3:
24-
Error: Syntax error: 'end' expected
24+
Error: Syntax error: end expected
2525
Line 1, characters 56-62:
26-
This 'struct' might be unmatched
26+
This struct might be unmatched
2727
```
2828

2929

@@ -44,7 +44,7 @@ end
4444
```
4545
```mdx-error
4646
Line 4, characters 3-3:
47-
Error: Syntax error: 'end' expected
47+
Error: Syntax error: end expected
4848
Line 1, characters 56-62:
49-
This 'struct' might be unmatched
49+
This struct might be unmatched
5050
```

test/bin/mdx-test/expect/ocaml-errors/test-case.md.expected

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ Error: This 'struct' might be unmatched
1414
```
1515

1616

17-
```ocaml version>=4.08
17+
```ocaml version>=5.2
1818
module Counter: Irmin.Contents.S with type t = int64 = struct
1919
type t = int64
2020
let t = Irmin.Type.int64
2121
```
2222
```mdx-error
2323
Line 4, characters 3-3:
24-
Error: Syntax error: 'end' expected
24+
Error: Syntax error: end expected
2525
Line 1, characters 56-62:
26-
This 'struct' might be unmatched
26+
This struct might be unmatched
2727
```
2828

2929

0 commit comments

Comments
 (0)