-
Notifications
You must be signed in to change notification settings - Fork 103
Add context-free rule type that replaces AST nodes with attributes #574
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
Merged
NathanReb
merged 11 commits into
ocaml-ppx:main
from
janestreet:jrickard/context-free-attr-replace
Jun 17, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
44b209c
Add ability for the context-free pass to replace items with attributes
Skepfyr 412d532
Formatting
Skepfyr a7a8fc2
Get attr_replace test running
Skepfyr c242dfe
Fix attr_replace test
Skepfyr 4e10d5d
Minor fixes & formatting
Skepfyr 74e3e3d
Clarify docs to recommend extenders over attr_replace
Skepfyr 0a79ff8
Add caveat in mli docs for attr_replace
Skepfyr 534021a
Review feedback
Skepfyr a412a48
Fix test
Skepfyr 621a236
Add test that replacements happen recursively
Skepfyr f0ec31c
Add CHANGELOG entry and remove unused file
Skepfyr 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
To clarify this change: some attributes get duplicated in the AST (that is, they point to the same location) but we only want to include them once in
not_seenso thatremove_seenworks as expected.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.
This sounds sane! For future reference, do you have an example of when that happens? Something we can use to reproduce it?
Might actually be worth adding a test for this.
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 do but I'm unsure how to test it. OCaml 5.00 (and below) parses
let v: (unit [@attr]) = ()aslet v: (unit [@attr]) = ((): (unit [@attr]))so the attr is visible down both bits of thevalue_bindingcausing it to get registered twice. I can't work out how to write a test that migrates the AST in the necessary way to check this.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.
If we take it out, does something fail internally here at Jane Street? If so, can we turn that into an external reproduction? If not, should this fix just be a separate PR?
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.
If I understand the problem correctly our tests would need to run on OCaml <= 5.0, parse a source file with an attribute set as you described above, and have a ppx driver run on it and "consume" the attribute. Before your fix this should trigger the bug and we should get an unused attribute warning because of the second "shadow" attribute not being marked as seen. After your fix this should go away. Am I correct?
I think you can simply restrict the compiler version under which the test is run, using dune's
enabled_ifstanza field, as it is done here for example.You make it sound like it's trickier than that so I might have missed something.
Either way, I think this would be best fixed in its own PR as @ceastlund suggests!
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 would quite like this change in this PR because ppx_template fails internally without it, however I could move it out and we can maintain the patch while we work out what's going on. However, this is my current understanding:
From AST version 500 and earlier,
value_bindingwould include the attribute in the AST twice, which would cause weirdness described. No-one hit this because nothing usedAttribute_tablefor attributes on value bindings before.The AST migration from 500 to 501 fixes the duplication, this means that all versions of ppxlib since then can't hit this specific version of the bug, I don't know any other places where attributes get duplicated so I can't write a test for this.
I hit this because of weirdness in our internal toolchain setup that I haven't got to the bottom of.
Given that I'd quite like to include this change, it feels conceptually correct and it fixes an issue for us internally, but I'm relatively sure that it makes no difference to ppxlib's behaviour.
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 fine merging this as is, the change seem sane. I was suggesting we add a test to prevent reintroducing it someday but if it's too cumbersome to write a meaningful test, let's go ahead and merge!