Jump to definition of functions generated by `ppx_deriving`

Seems like Merlin cannot “Jump to definition” to functions generated by ppx_deriving. I wonder why this is? Not sure if this is an error in my setup, a bug in Merlin, an omission in ppx_deriving or just something that is not just implemented yet?

I have merlin 5.3-502 and ppx_deriving 6.03 and ocaml 5.2.0.

Merlin cannot jump to the definition because there is no textual definition to speak of.
PPXs work by mapping over compiler parsetree values so the function generated by ppx_deriving only exist compiled files.

Having the generated definition shown somehow is technically possible though, but i’m guessing this is not a trivial thing to do and would require special support.

4 Likes

Hello.

If you are using dune, then in the _build directory (I think) you may find files with the generated “code”: but usually they are not human readable. You may find useful the following link:

There, it is commented a feature named [@@deriving_inline] and it is explained:

Some derivers are only needed for boilerplate generation. When that’s the case, there is no strong requirement for them to be included as a hard dependency: the added boilerplate code can be pretty printed and added to the source code by the PPX. This mechanism can be implemented using Dune and ppxlib.

In that cases, it might be possible to promote part of the changes to the source code as explained in that section, although I think it is generally not a recommended practice in most of the situations. Further details are in that ocaml.org page above mentioned.

Additionally, when using an lsp-based plugin (such as the one in vscode), you can actually peek at the generated code by hovering on the annotation:

hoverppx

5 Likes

Thanks for the explanations! Actually, I don’t really care about the definition of the generated functions: I just want to know where they come from. So for me, just jumping to the ppx_deriving attribute would be useful.

That should be the case. In my project the cursor jumps to the beginning of the type definition with the deriving attribute.

I created a minimal example where it doesn’t seem to work for me.

If I go here, put my cursor ('m using emacs) on make and do C-c C-l, i.e. merlin-locate, I get the error:

merlin-call-locate: 'make' seems to originate from '_none_' whose ML file could not be found

My merlin-mode version is ;; Package-Version: 20240925.900 which I think is the latest.

2 Likes

Thanks a lot for taking the time to make a reproduction. Would you mind opening an issue on Merlin’s tracker with a link to the repro ? That would be very useful to remind us to have a look :slightly_smiling_face:

1 Like

Sure, here: Cannot locate function generated by `ppx_deriving` · Issue #1910 · ocaml/merlin · GitHub

1 Like