Skip to content

Drop items without source text in Code_matcher - #648

Open
nmatschke wants to merge 1 commit into
ocaml-ppx:mainfrom
janestreet:deriving-inline-vs-generated-items
Open

Drop items without source text in Code_matcher#648
nmatschke wants to merge 1 commit into
ocaml-ppx:mainfrom
janestreet:deriving-inline-vs-generated-items

Conversation

@nmatschke

@nmatschke nmatschke commented Jul 31, 2026

Copy link
Copy Markdown

Code_matcher assumes that the items following an inline block's anchor are
exactly the items that were parsed from the source file, and rewrites their source text
in place. That assumption breaks when the anchor is itself inside code generated by
another ppx. In particular, ppx_template wraps template-mangled definitions in (**/**)
markers so odoc hides them, and those markers inherit the location of the node they were
generated from. So

[%%template:
type t = int [@@kind.explicit k = (value, value_or_null)] [@@deriving_inline globalize]

[@@@end]]

expands to a (**/**) item whose location is the whole type declaration, and the
corrected code replaces the declaration with the derived signature instead of inserting
it after.

Fix this by dropping items that start before the end of the anchor item, since those
cannot correspond to source text following it.

@NathanReb NathanReb left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks sane to me but I think it is worth a test!

@NathanReb

Copy link
Copy Markdown
Collaborator

Could you also rebase it on top of main to fix the 5.5 builds in the CI, sign-off your commits with -s and add a changelog entry please?

Comment thread src/code_matcher.ml
match_loop ~end_pos ~mismatch_handler ~expected ~source

(* [source] is supposed to be the list of items that follow the [@@deriving_inline]
item in the source file. That is not the case when the [@@deriving_inline] item is

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds a little bit odd, I think adding the ppx_template example here would make a bit more sense as otherwise, ppx generated deriving_inline nodes sounds a bit crazy and something we don't necessarily want to support.

@NathanReb

Copy link
Copy Markdown
Collaborator

Can you elaborate a bit more on ppx_template and explain what it does and why a ppx would generate deriving_inline nodes?

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.

2 participants