Skip to content

Commit 2e8e968

Browse files
Merge pull request #1091 from ocsigen/ppx-type-wrong-1085
ppx: add test for #1085, add standalone ppx executable
2 parents 9302bd0 + 252ebb9 commit 2e8e968

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

examples/ppx_lwt_standalone/dune

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
; causes build errors when in src/ppx (when building other packages but not this one)
2+
; can't use (package lwt_ppx) because dune complains it'd be useless without a public_name
3+
; so this lives in examples/
4+
(executable
5+
(name ppx_lwt_standalone)
6+
(modules ppx_lwt_standalone)
7+
(libraries
8+
lwt_ppx
9+
ppxlib))
10+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let () = Ppxlib.Driver.standalone ()

src/ppx/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(public_name lwt_ppx)
33
(synopsis "Lwt PPX syntax extension")
44
(libraries ppxlib)
5+
(modules ppx_lwt)
56
(ppx_runtime_libraries lwt)
67
(kind ppx_rewriter)
78
(preprocess

src/ppx/ppx_lwt.ml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ let gen_name i = lwt_prefix ^ string_of_int i
5656
let gen_bindings l =
5757
let aux i binding =
5858
{ binding with
59-
pvb_pat = pvar ~loc:binding.pvb_expr.pexp_loc (gen_name i)
59+
pvb_pat = pvar ~loc:binding.pvb_expr.pexp_loc (gen_name i);
60+
pvb_constraint = None;
6061
}
6162
in
6263
List.mapi aux l
@@ -72,7 +73,13 @@ let gen_binds e_loc l e =
7273
in
7374
let fun_ =
7475
let loc = e_loc in
75-
[%expr (fun [%p binding.pvb_pat] -> [%e aux (i+1) t])]
76+
match binding.pvb_constraint with
77+
| None -> [%expr (fun [%p binding.pvb_pat] -> [%e aux (i+1) t])]
78+
| Some (Pvc_constraint { locally_abstract_univars = []; typ }) ->
79+
[%expr (fun ([%p binding.pvb_pat] : [%t typ]) -> [%e aux (i+1) t])]
80+
| _ ->
81+
(* no support for more advanced type annotations *)
82+
Location.Error.(raise (make ~loc "unsupported value binding constraint" ~sub:[]))
7683
in
7784
let new_exp =
7885
let loc = e_loc in
@@ -382,6 +389,18 @@ class mapper = object (self)
382389
(Lwt_main.run [@ocaml.ppwarning [%e warning]])
383390
[%e super#expression exp]]
384391

392+
| [%stri let%lwt [%p? var] : [%t? typ] = [%e? exp]] ->
393+
let warning =
394+
estring ~loc:!default_loc
395+
("let%lwt should not be used at the module item level.\n" ^
396+
"Replace let%lwt x = e by let x = Lwt_main.run (e)")
397+
in
398+
let loc = !default_loc in
399+
[%stri
400+
let [%p var] : [%t typ] =
401+
(Lwt_main.run [@ocaml.ppwarning [%e warning]])
402+
[%e super#expression exp]]
403+
385404
| x -> super#structure_item x);
386405
end
387406

test/ppx/main.ml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ open Lwt
77
an outer call to Lwt_main.run, and nested calls to Lwt_main.run are not
88
allowed. *)
99
[@@@ocaml.warning "-22"]
10-
let%lwt structure_let_result = Lwt.return_true
10+
let%lwt structure_let_result : bool = Lwt.return_true
1111
[@@@ocaml.warning "+22"]
1212

1313
let __trace_ctxt = "test" (* TODO: figure out how to make this implicit *)
@@ -164,6 +164,23 @@ let suite = suite "ppx" [
164164
(fun () ->
165165
Lwt.return structure_let_result
166166
) ;
167+
168+
(* as reported in https://github.com/ocsigen/lwt/issues/1085 *)
169+
test "1085-int"
170+
(fun () ->
171+
let%lwt (_ : int) = Lwt.return 0 in
172+
Lwt.return_true
173+
) ;
174+
test "1085-int-again"
175+
(fun () ->
176+
let%lwt _ : int = Lwt.return 0 in
177+
Lwt.return_true
178+
) ;
179+
test "1085-any"
180+
(fun () ->
181+
let%lwt _ : _ = Lwt.return 0 in
182+
Lwt.return_true
183+
) ;
167184
]
168185

169186
let _ = Test.run "ppx" [ suite ]

0 commit comments

Comments
 (0)