-
Notifications
You must be signed in to change notification settings - Fork 99
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 [@@@expand_inline]
and support for floating attribute context free transformations
#560
base: main
Are you sure you want to change the base?
Conversation
Seems like |
patdiff dependency cone includes core which we don't want to have as a dependency even for testing as it makes it hard to support the whole range of compilers we want to support. You can overwrite the default diff command with the |
Out of curiosity, what's the use case for the simpler Do you have a concrete usecase for it already? It seems to go a bit against the context-free rules philosophy, where the result of any expansion rules is recursively expanded. |
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.
The code looks good to me, thanks!
This needs a changelog entry and it would be great to add a bit of documentation for the [@@@expand_inline ...]
feature.
As I mentioned in the comments earlier, I'd rather avoid adding the expect
with no expand versions unless strictly necessary.
Looking through our current handling of other |
I think in some cases you don't want to expand on inlined code, for example maybe you have a deriver that itself outputs code that has type t = (int * string) * (char * string) [@@deriving_inline derive_compare_for_parts]
type p1 = int * string [@@deriving compare]
type p2 = char * string [@@deriving compare]
[@@@end] In the above example, if you're intention is just to communicate the types, expanding the |
We don't have a concrete use case yet, but similar to my above comment, it may be useful to write a transformation where the output is not expanded: [@@@make_variants "foo", "bar", "baz"]
type t = | Foo | Bar | Baz [@@deriving compare]
[@@@end] However, I would be fine removing the |
5f145b9
to
306c190
Compare
The thing that puzzles me with this behaviour is that
Those Anyway, I digress here as this is not related to this PR. I'd be in favor of keeping only the |
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.
Thanks for the documentation and for fixing the diff command in the tests!
Let's wait on @ceastlund take on the expect
(without expand) variants to decide what to do with those!
Wouldn't it? When (in any case, I still think it would be okay to get rid of the |
…ibutes. Signed-off-by: Jay Mody <[email protected]>
Signed-off-by: Jay Mody <[email protected]>
Signed-off-by: Jay Mody <[email protected]>
Signed-off-by: Jay Mody <[email protected]>
Signed-off-by: Jay Mody <[email protected]>
Signed-off-by: Jay Mody <[email protected]>
306c190
to
a7255c4
Compare
Yes good point, that's what I failed to see as I was only thinking in terms of a single ppxlib driver invocation. Indeed if you promote the generated code, it will be expanded next time you compile it. I guess I only thought about |
I don't have strong opinions on whether we include the non- |
Adds new context free transformations
[@@@expand_inline <unexpanded-structure>] <expanded-structure> [@@@end]
and[@@@expand_inline: <unexpanded-signature>] <expanded-signature> [@@@end]
.For example:
Would trigger the corrections (assuming we've registered ppxes to handle
[%ten]
and[%str]
accordingly):More generally, we provide users of
ppxlib
four new ways to create aContext_free.Rule.t
:For example:
Will register a transformation that can be used as:
attr_str_floating_expect
registers an expect transformation from a floating attributes in a structure item context.attr_str_floating_expect_and_expand
is similar, but it also "expands" the output of the transformation by running it against other context free transformations. Thesig
versions of these functions behave similarly, but for floating attributes in signature item contexts.Motivation
@@@expand_inline
provides an alternative way to view ppx expanded code. Currently, the best way to do this would be to run yourppx.exe
against an entire file and save that output to a separate file. This requires setting up a new build rule and switching files every time you want to see the output. Furthermore, there's no way to select which parts of a program you want to expand with file based expansion.@@@expand_inline
does not have these limitations.@@@expand_inline
allows a library to use a ppx that depends on the library itself. For example:Foo
ppx.exe
, which depends onFoo
Foo
again, but this time we can add anexpand_inline
block where the payload can contain ppx code handled byppx.exe
. Since the expanded code gets inlined, and attributes are ignored by the compiler, we can repeat this cycle.Reviewing
It's probably best to initially review this PR commit-by-commit:
attr_{str,sig}_floating_expect{,_and_expand}
transformations.expand_inline
usingattr_{str,sig}_floating_expect_and_expand
.[@@ocaml.doc "foo"]
comments to[@@ocaml.doc {|foo|}
. This is useful if code inside the payload of anexpand_inline
contains doc comments. Without this, long doc comments will get formatted using the regular""
syntax, which makes the doc comment unreadable in the expanded code.Considerations
expand_inline
doesn't expand anything if used alongside the-no-merge
flag. This just means that users should probably not be usingexpand_inline
(or any_and_expand
transformation) alongside-no-merge
.@@@deriving.end
to@@@ppxlib.inline.end
.