-
Notifications
You must be signed in to change notification settings - Fork 98
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
Add -dune-optional-output mode for dune's internal use #482
Open
NathanReb
wants to merge
1
commit into
ocaml-ppx:main
Choose a base branch
from
NathanReb:optional-ouput-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 12 additions & 0 deletions
12
test/driver/dune-optional-output/context_free_only_driver.ml
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 @@ | ||
open Ppxlib | ||
|
||
let rule = | ||
Context_free.Rule.extension | ||
(Extension.V3.declare "iam1" Extension.Context.expression | ||
Ast_pattern.(pstr nil) | ||
(fun ~ctxt -> | ||
let loc = Expansion_context.Extension.extension_point_loc ctxt in | ||
[%expr 1])) | ||
|
||
let () = Driver.register_transformation ~rules:[ rule ] "iam1" | ||
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,16 @@ | ||
open Ppxlib | ||
|
||
let rule = | ||
Context_free.Rule.extension | ||
(Extension.V3.declare "iam1" Extension.Context.expression | ||
Ast_pattern.(pstr nil) | ||
(fun ~ctxt -> | ||
let loc = Expansion_context.Extension.extension_point_loc ctxt in | ||
[%expr 1])) | ||
|
||
let () = Driver.register_transformation ~rules:[ rule ] "iam1" | ||
|
||
let () = | ||
Driver.register_transformation ~impl:(fun str -> str) "IdentityInDisguise" | ||
|
||
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,16 @@ | ||
(executable | ||
(name context_free_only_driver) | ||
(libraries ppxlib) | ||
(preprocess | ||
(pps ppxlib.metaquot)) | ||
(modules context_free_only_driver)) | ||
|
||
(executable | ||
(name driver_with_impl) | ||
(libraries ppxlib) | ||
(preprocess | ||
(pps ppxlib.metaquot)) | ||
(modules driver_with_impl)) | ||
|
||
(cram | ||
(deps context_free_only_driver.exe driver_with_impl.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,52 @@ | ||
The -dune-optional-output flag is meant for dune to be able | ||
to use ppx internally without having a build dependency on ppxlib | ||
or any ppx. | ||
|
||
When enabled, it should not write to the output if it can absolutely | ||
tell no transformation occured. | ||
|
||
We have a driver with a single context free rule to expand [%iam1] extension | ||
|
||
Let us consider the following file: | ||
|
||
$ cat > foo.ml << EOF | ||
> let x = 1 | ||
> let y = 2 | ||
> EOF | ||
|
||
If we call the driver with the -dune-optional-output flag, it should not write a file: | ||
|
||
$ ./context_free_only_driver.exe -impl -dune-optional-output -o foo.pp.ml foo.ml | ||
$ ls foo.* | ||
foo.ml | ||
|
||
We can see that it did not write foo.pp.ml | ||
|
||
Now if we actually use the extension: | ||
|
||
$ cat > bar.ml << EOF | ||
> let x = [%iam1] | ||
> let y = 2 | ||
> EOF | ||
|
||
It should actually detect the transformation and therefore write the output file: | ||
|
||
$ ./context_free_only_driver.exe -impl -dune-optional-output -o bar.pp.ml bar.ml | ||
$ ls bar.* | ||
bar.ml | ||
bar.pp.ml | ||
|
||
Now we have another driver that has the same context free rule but also another | ||
transformation with an "impl", i.e. a rule to rewrite the whole AST unconditionally. | ||
This rule does not rewrite anything and is just the identity rewriter. | ||
We cannot tell without actually comparing the ASTs if any rewriting happened so in | ||
that case we always write to the output. | ||
|
||
$ cat > baz.ml << EOF | ||
> let x = 1 | ||
> let y = 2 | ||
> EOF | ||
$ ./driver_with_impl.exe -impl -dune-optional-output -o baz.pp.ml baz.ml | ||
$ ls baz.* | ||
baz.ml | ||
baz.pp.ml |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not quite sure I follow why checking this name is enough for deciding that this rewrite is not context free? Is it okay because we assume that people will not use that name externally (probably a fine assumption)? For example, if you change the
driver_with_impl.ml
to be calledThe pp file is no longer generated. Sorry this might just be that I am too new to the codebase!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ppxlib's driver will assemble several AST traversal passes. One of them is meant to apply all context-free rules, it is built by ppxlib itself and is named "builtin:context-free".
All other passes come from ppx-es registering
str -> str
orsig -> sig
rewriting functions.Indeed there are no guarantees a user won't use that name when registering a ppx of their own but it is very unlikely.
I think this all part of the code could be refactored and simplified significantly and that might help distinguish between this special pass and all others.
In the meantime we could try to forbid this name for outside users. The transformation name is only used for error reporting as far as I remember so it usually is the name of the ppx itself, it's unlikely that we would break anything by adding such a restriction.
I think it should be alright but if you feel this is too much of a risk, I'm happy to look into refactoring this part in a way that allows us not to check by name.