From cf0279991d630d51f12a1d4b2933d05eecc4464f Mon Sep 17 00:00:00 2001 From: CastilloDel Date: Sat, 4 Feb 2023 18:52:27 +0100 Subject: [PATCH] Fix suggestions rendering when the span is multiline --- compiler/rustc_errors/src/emitter.rs | 80 ++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_errors/src/emitter.rs b/compiler/rustc_errors/src/emitter.rs index faeaa548619..61e1155a79f 100644 --- a/compiler/rustc_errors/src/emitter.rs +++ b/compiler/rustc_errors/src/emitter.rs @@ -2175,30 +2175,69 @@ impl EmitterWriter { file_lines: &FileLines, is_multiline: bool, ) { - // Print the span column to avoid confusion - buffer.puts(*row_num, 0, &self.maybe_anonymized(line_start + line_pos), Style::LineNumber); if let DisplaySuggestion::Diff = show_code_change { // Add the line number for both addition and removal to drive the point home. // // N - fn foo(bar: A) { // N + fn foo(bar: impl T) { + let number_of_lines = file_lines.lines.len(); + for (index, line_to_remove) in + file_lines.lines.iter().take(number_of_lines - 1).enumerate() + { + buffer.puts( + *row_num - 1, + 0, + &self.maybe_anonymized(line_start + line_pos + index), + Style::LineNumber, + ); + buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal); + buffer.puts( + *row_num - 1, + max_line_num_len + 3, + &normalize_whitespace( + &file_lines.file.get_line(line_to_remove.line_index).unwrap(), + ), + Style::NoStyle, + ); + *row_num += 1; + } + let last_line = &file_lines + .file + .get_line(file_lines.lines[number_of_lines - 1].line_index) + .unwrap(); + if last_line != line { + buffer.puts( + *row_num - 1, + 0, + &self.maybe_anonymized(line_start + line_pos + number_of_lines - 1), + Style::LineNumber, + ); + buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal); + buffer.puts( + *row_num - 1, + max_line_num_len + 3, + &normalize_whitespace(last_line), + Style::NoStyle, + ); + buffer.puts( + *row_num, + 0, + &self.maybe_anonymized(line_start + line_pos), + Style::LineNumber, + ); + buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition); + // print the suggestion + buffer.append(*row_num, &normalize_whitespace(line), Style::NoStyle); + } else { + *row_num -= 2; + } + } else if is_multiline { buffer.puts( - *row_num - 1, + *row_num, 0, &self.maybe_anonymized(line_start + line_pos), Style::LineNumber, ); - buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal); - buffer.puts( - *row_num - 1, - max_line_num_len + 3, - &normalize_whitespace( - &file_lines.file.get_line(file_lines.lines[line_pos].line_index).unwrap(), - ), - Style::NoStyle, - ); - buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition); - } else if is_multiline { match &highlight_parts[..] { [SubstitutionHighlight { start: 0, end }] if *end == line.len() => { buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition); @@ -2210,13 +2249,20 @@ impl EmitterWriter { buffer.puts(*row_num, max_line_num_len + 1, "~ ", Style::Addition); } } + // print the suggestion + buffer.append(*row_num, &normalize_whitespace(line), Style::NoStyle); } else { + buffer.puts( + *row_num, + 0, + &self.maybe_anonymized(line_start + line_pos), + Style::LineNumber, + ); draw_col_separator(buffer, *row_num, max_line_num_len + 1); + // print the suggestion + buffer.append(*row_num, &normalize_whitespace(line), Style::NoStyle); } - // print the suggestion - buffer.append(*row_num, &normalize_whitespace(line), Style::NoStyle); - // Colorize addition/replacements with green. for &SubstitutionHighlight { start, end } in highlight_parts { // Account for tabs when highlighting (#87972).