Skip to content

Commit 11b3577

Browse files
committed
fix: display panic where lines could appear in multiple output rows
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.
1 parent e55f7b6 commit 11b3577

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

src/display/side_by_side.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ pub(crate) fn print(
545545
}
546546

547547
let matched_lines = all_matched_lines_filled(lhs_mps, rhs_mps, &lhs_lines, &rhs_lines);
548-
let mut matched_lines_to_print = &matched_lines[..];
548+
let matched_lines_to_print = &matched_lines[..];
549549

550550
let mut lhs_max_visible_line = 1.into();
551551
let mut rhs_max_visible_line = 1.into();
@@ -612,12 +612,11 @@ pub(crate) fn print(
612612
display_options.num_context_lines as usize,
613613
);
614614
let aligned_lines = &matched_lines_to_print[start_i..end_i];
615-
// We iterate through hunks in order, so we know the next hunk
616-
// must appear after start_i. This makes
617-
// `matched_lines_indexes_for_hunk` faster on later
618-
// iterations, and this function is hot on large textual
619-
// diffs.
620-
matched_lines_to_print = &matched_lines_to_print[start_i..];
615+
// Note: We previously sliced matched_lines_to_print here as an
616+
// optimization, assuming hunks appear in order. However, hunks
617+
// from matched_novel_lines may not be strictly ordered by their
618+
// position in matched_lines when there are interleaved LHS-only
619+
// and RHS-only novel sections. See issue #770.
621620

622621
let no_lhs_changes = hunk.novel_lhs.is_empty();
623622
let no_rhs_changes = hunk.novel_rhs.is_empty();

0 commit comments

Comments
 (0)