From f655b5e48f392ea14a3ed564f2928f508168f074 Mon Sep 17 00:00:00 2001 From: nmatschke Date: Fri, 31 Jul 2026 14:53:25 -0400 Subject: [PATCH] drop items without source text --- src/code_matcher.ml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/code_matcher.ml b/src/code_matcher.ml index c80afea6..a33c408b 100644 --- a/src/code_matcher.ml +++ b/src/code_matcher.ml @@ -187,9 +187,23 @@ struct mismatch_handler loc [ x ]); 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 + itself part of code generated by another ppx: the generator is free to insert items + of its own after it. + + Such items have no corresponding source text; their location is inherited from the + node they were generated from, and hence starts at or before the start of the + [@@deriving_inline] item. Were we to keep them, we would end up replacing the + source text of the [@@deriving_inline] item itself with the generated code. *) + let drop_items_without_source_text ~pos items = + List.filter items ~f:(fun item -> + Location.compare_pos (M.get_loc item).loc_start pos >= 0) + let do_match ~pos ~expected ~mismatch_handler source = let open Result in extract_prefix ~pos source >>| fun (source, end_pos) -> + let source = drop_items_without_source_text ~pos source in match_loop ~end_pos ~mismatch_handler ~expected ~source end