Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.3 effect syntax #552

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions astlib/migrate_502_503.ml
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ and copy_pattern_desc :
(copy_loc (fun x -> Option.map (fun x -> x) x) x0)
| Ast_502.Parsetree.Ppat_exception x0 ->
Ast_503.Parsetree.Ppat_exception (copy_pattern x0)
| Ast_502.Parsetree.Ppat_extension
( { txt = "ppxlib.migration.ppat_effect"; _ },
PPat ({ ppat_desc = Ppat_tuple [ e; c ]; _ }, None) ) ->
Ast_503.Parsetree.Ppat_effect (copy_pattern e, copy_pattern c)
| Ast_502.Parsetree.Ppat_extension x0 ->
Ast_503.Parsetree.Ppat_extension (copy_extension x0)
| Ast_502.Parsetree.Ppat_open (x0, x1) ->
Expand Down
16 changes: 11 additions & 5 deletions astlib/migrate_503_502.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ open Stdlib0
module From = Ast_503
module To = Ast_502

let migration_error loc missing_feature =
Location.raise_errorf ~loc
"migration error: %s is not supported before OCaml 5.03" missing_feature

let rec copy_toplevel_phrase :
Ast_503.Parsetree.toplevel_phrase -> Ast_502.Parsetree.toplevel_phrase =
function
Expand Down Expand Up @@ -330,7 +326,17 @@ and copy_pattern_desc loc :
(copy_loc (fun x -> Option.map (fun x -> x) x) x0)
| Ast_503.Parsetree.Ppat_exception x0 ->
Ast_502.Parsetree.Ppat_exception (copy_pattern x0)
| Ast_503.Parsetree.Ppat_effect _ -> migration_error loc "effect pattern"
| Ast_503.Parsetree.Ppat_effect (e, c) ->
Ast_502.Parsetree.Ppat_extension
( Location.{ txt = "ppxlib.migration.ppat_effect"; loc },
Ast_502.Parsetree.PPat
( {
ppat_desc = Ppat_tuple [ copy_pattern e; copy_pattern c ];
ppat_attributes = [];
ppat_loc_stack = [];
ppat_loc = loc;
},
None ) )
| Ast_503.Parsetree.Ppat_extension x0 ->
Ast_502.Parsetree.Ppat_extension (copy_extension x0)
| Ast_503.Parsetree.Ppat_open (x0, x1) ->
Expand Down
18 changes: 18 additions & 0 deletions test/503_migrations/preserve-effect-downward/driver.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module To_before_503 =
Ppxlib_ast.Convert (Ppxlib_ast.Js) (Ppxlib_ast__.Versions.OCaml_502)

module From_before_503 =
Ppxlib_ast.Convert
(Ppxlib_ast__.Versions.OCaml_502)
(Ppxlib_ast.Compiler_version)

let impl _ctxt str =
(* This manual migration is here to ensure the test still works even once our
internal AST has been bumped past 5.3 *)
let before_503_ast = To_before_503.copy_structure str in
let roundtrip = From_before_503.copy_structure before_503_ast in
Ocaml_common.Pprintast.structure Format.std_formatter roundtrip;
str

let () = Ppxlib.Driver.V2.register_transformation ~impl "503-downward-roundtrip"
let () = Ppxlib.Driver.standalone ()
10 changes: 10 additions & 0 deletions test/503_migrations/preserve-effect-downward/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
(executable
(name driver)
(enabled_if
(>= %{ocaml_version} "5.3"))
(libraries ppxlib ocaml-compiler-libs.common compiler-libs.common))

(cram
(enabled_if
(>= %{ocaml_version} "5.3"))
(deps driver.exe))
24 changes: 24 additions & 0 deletions test/503_migrations/preserve-effect-downward/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
We should be able to migrate the effect syntax downward and back. This allows users
to run ppx-es on 5.3 on source files that use the effect syntax until we update our
internal AST to fully support it.

We have a custom driver that will force migration of the AST down to 5.2 and back to
the compiler's version and print it as source code using the compiler's printer,
regardless of ppxlib's internal AST version.

If we run the driver on the following source file:

$ cat > test.ml << EOF
> let handler f =
> match f () with
> | x -> x
> | effect Random_bits, k -> Effect.Deep.continue k (Random.bits ())
> EOF

it should successfully roundtrip to 5.2 and print the source code unchanged:

$ ./driver.exe test.ml -o test.pp.ml
let handler f =
match f () with
| x -> x
| effect Random_bits, k -> Effect.Deep.continue k (Random.bits ())
Loading