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

Add -dune-optional-output mode for dune's internal use #482

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

NathanReb
Copy link
Collaborator

Fixes #461

This PR adds a new command line flag that tells the driver not to write to the output file if there is no rewriting to be done.

It's not 100% accurate if there are non context free transformations registered as we do not compare the AST for this feature but simply keep track of generated code via a hook.

If any non context free transformation is registered, we simply assume it will rewrite something and always output.

CHANGES.md Outdated Show resolved Hide resolved
@NathanReb NathanReb force-pushed the optional-ouput-mode branch 2 times, most recently from 0695d62 to 0609cea Compare March 22, 2024 09:51
src/driver.ml Outdated Show resolved Hide resolved
test/driver/dune-optional-output/run.t Outdated Show resolved Hide resolved
test/driver/dune-optional-output/run.t Outdated Show resolved Hide resolved
(* Meant to be used after partitioning *)
let rewrites_not_context_free t =
match t with
| { name; _ } when String.equal name builtin_context_free_name -> false
Copy link
Collaborator

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 called

let () =
  Driver.register_transformation ~impl:(fun str -> str) "<builtin:context-free>"

The pp file is no longer generated. Sorry this might just be that I am too new to the codebase!

Copy link
Collaborator Author

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 or sig -> 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.

Fixes ocaml-ppx#461

This PR adds a new command line flag that tells the driver
not to write to the output file if there is no rewriting to be done.

It's not 100% accurate if there are non context free transformations
registered as we do not compare the AST for this feature but simply
keep track of generated code via a hook.

If any non context free transformation is registered, we simply assume
it will rewrite something and always output.

Signed-off-by: Nathan Rebours <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a driver mode with no output if there is no rewriting to do
2 participants