Skip to content

Fix panic in matched_lines_indexes_for_hunk#944

Open
gthb wants to merge 6 commits intoWilfred:masterfrom
gthb:fix-issue-770
Open

Fix panic in matched_lines_indexes_for_hunk#944
gthb wants to merge 6 commits intoWilfred:masterfrom
gthb:fix-issue-770

Conversation

@gthb
Copy link
Copy Markdown
Contributor

@gthb gthb commented Jan 28, 2026

What

Fix the panic in #770 where the assertion "Hunk lines should be present in matched lines" failed.

How

In matched_novel_lines, when a novel line on one side picks an opposite line using next_opposite, also update the opposite side's highest tracker. This prevents the same line from being added again when that side is processed, which was the source of duplicate entries that caused panics downstream.

Fix an apparent bug in the RHS arm: when looking up the opposite position for RHS novel lines, the filter should use highest_lhs, not highest_rhs.

Remove the slicing optimization in side_by_side::print that assumed hunks appear in matched_lines order. Looks like the rationale:

We iterate through hunks in order, so we know the next hunk must appear after start_i

does not (currently) hold (maybe because something changed since this optimization landed?): when there are interleaved LHS-only and RHS-only novel sections, hunks from matched_novel_lines may not be strictly ordered by their position in matched_lines. So the slicing would remove lines that later hunks still needed to display, leading to that panic.

gthb added 5 commits January 27, 2026 23:55
Add repro files from issue Wilfred#770 into `sample_files`.

Add test case `test_display_hunks_out_of_order_issue_770` which reproduces the
panic in a more targeted way.

(Add `test_util.rs` with helpers for succinctly creating MatchedPos values)
Remove the slicing optimization in side_by_side.rs that assumed hunks appear in
matched_lines order. It seems the rationale "We iterate through hunks in order,
so we know the next hunk must appear after start_i" doesn't hold (maybe because
something changed since this optimization landed?). When there are interleaved
LHS-only and RHS-only novel sections, hunks from matched_novel_lines may not be
strictly ordered by their position in matched_lines.

Fixes Wilfred#770 where the slicing would remove lines that later hunks still needed
to display, leading to a panic.
These Nix files trigger a panic in matched_lines_indexes_for_hunk

Minimized from the files in Wilfred#770 (comment)
In matched_novel_lines, when a novel line on one side picks an opposite line
using next_opposite, also update the opposite side's highest tracker. This
prevents the same line from being added again when that side is processed,
which was the source of duplicate entries that caused panics downstream.

Also fix an apparent bug in the RHS arm: when looking up the opposite position
for RHS novel lines, the filter should use highest_lhs, not highest_rhs.
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.

1 participant