-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add --use-compiler-pp flag to standalone driver
This flag allows user to force the driver to print AST as source code using the installed compiler printers rather than its own. This can be useful in situation where one is using the driver source output on older OCaml version to prevent outputting incompatible syntax. In addition it can prevent the driver to output uninterpreted extensions when migrating down newer compiler features before we bumped the internal AST.
- Loading branch information
Showing
11 changed files
with
223 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,4 +34,5 @@ val reconcile : | |
output:string option -> | ||
input_name:string -> | ||
target:target -> | ||
use_compiler_pprint:bool -> | ||
unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
open Ppxlib | ||
|
||
let existential ~loc = | ||
let lident = { loc; txt = Longident.parse "Constructor" } in | ||
let pattern = | ||
{ | ||
ppat_loc = loc; | ||
ppat_loc_stack = []; | ||
ppat_attributes = []; | ||
ppat_desc = | ||
Ppat_construct (lident, Some ([ { loc; txt = "a" } ], [%pat? _])); | ||
} | ||
in | ||
[%stri let f x = match x with [%p pattern] -> ()] | ||
|
||
let named_existential = | ||
Context_free.Rule.extension | ||
(Extension.V3.declare "named_existentials" Extension.Context.structure_item | ||
Ast_pattern.(pstr nil) | ||
(fun ~ctxt -> | ||
let loc = Expansion_context.Extension.extension_point_loc ctxt in | ||
existential ~loc)) | ||
|
||
let () = | ||
Driver.V2.register_transformation ~rules:[ named_existential ] | ||
"named_existentials" | ||
|
||
let str_type_decl = | ||
Deriving.Generator.V2.make_noarg (fun ~ctxt _type_decl -> | ||
let loc = Expansion_context.Deriver.derived_item_loc ctxt in | ||
[ existential ~loc ]) | ||
|
||
let _ = Deriving.add ~str_type_decl "named_existentials" | ||
let () = Driver.standalone () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
(executable | ||
(name driver) | ||
(enabled_if | ||
(< %{ocaml_version} "4.13")) | ||
(libraries ppxlib) | ||
(preprocess | ||
(pps ppxlib.metaquot))) | ||
|
||
(cram | ||
(enabled_if | ||
(< %{ocaml_version} "4.13")) | ||
(deps driver.exe)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
The --use-compiler-pp flag can be used when using the driver's source code | ||
output, either directly are when generating a .corrected file, to force | ||
printing the AST as source using the installed compiler's printer. | ||
|
||
Our driver has a deriver and an extension that produces a pattern-matching with | ||
named existentials. | ||
|
||
This feature has been introduced in 4.13 so the syntax is unsupported before that. | ||
|
||
If we run the driver in source output mode, without the `--use-compiler-pp` flag, | ||
it will successfully print out the source using the 4.13 syntax. If we're running | ||
on an older compiler, like we are for this test, that can be troublesome. | ||
If instead we use the flag, this will force the migration thus causing an error as | ||
named existentials can't be migrated down to 4.12. | ||
|
||
Let's consider the following file: | ||
$ cat > test.ml << EOF | ||
> [%%named_existentials] | ||
> EOF | ||
Running the driver will generate a function with a single pattern matching in it: | ||
$ ./driver.exe test.ml | ||
let f x = match x with | Constructor (type a) _ -> () | ||
Now if we run it with `--use-compiler-pp`, we should get the migration error: | ||
$ ./driver.exe --use-compiler-pp test.ml | ||
File "test.ml", line 1, characters 0-22: | ||
1 | [%%named_existentials] | ||
^^^^^^^^^^^^^^^^^^^^^^ | ||
Error: migration error: existentials in pattern-matching is not supported before OCaml 4.13 | ||
[1] | ||
This should also work for correction based code gen: | ||
$ cat > test_inline.ml << EOF | ||
> type t = int | ||
> [@@deriving_inline named_existentials] | ||
> [@@@end] | ||
> EOF | ||
If we run the driver without `--use-compiler-pp`: | ||
$ ./driver.exe test_inline.ml -diff-cmd - | ||
type t = int[@@deriving_inline named_existentials] | ||
[@@@end ] | ||
$ cat test_inline.ml.ppx-corrected | ||
type t = int | ||
[@@deriving_inline named_existentials] | ||
let _ = fun (_ : t) -> () | ||
let f x = match x with | Constructor (type a) _ -> () | ||
let _ = f | ||
[@@@end] | ||
and with the flag: | ||
$ ./driver.exe test_inline.ml -diff-cmd - --use-compiler-pp | ||
File "test_inline.ml", lines 1-2, characters 0-38: | ||
1 | type t = int | ||
2 | [@@deriving_inline named_existentials] | ||
Error: migration error: existentials in pattern-matching is not supported before OCaml 4.13 | ||
[1] |