Skip to content

Commit 3dd0044

Browse files
committed
Clean up and comment EmitterWriter.draw_code_line
1 parent cf02799 commit 3dd0044

File tree

1 file changed

+32
-59
lines changed

1 file changed

+32
-59
lines changed

compiler/rustc_errors/src/emitter.rs

+32-59
Original file line numberDiff line numberDiff line change
@@ -1882,9 +1882,8 @@ impl EmitterWriter {
18821882
&mut buffer,
18831883
&mut row_num,
18841884
&Vec::new(),
1885-
p,
1885+
p + line_start,
18861886
l,
1887-
line_start,
18881887
show_code_change,
18891888
max_line_num_len,
18901889
&file_lines,
@@ -1907,9 +1906,8 @@ impl EmitterWriter {
19071906
&mut buffer,
19081907
&mut row_num,
19091908
&Vec::new(),
1910-
p,
1909+
p + line_start,
19111910
l,
1912-
line_start,
19131911
show_code_change,
19141912
max_line_num_len,
19151913
&file_lines,
@@ -1925,9 +1923,8 @@ impl EmitterWriter {
19251923
&mut buffer,
19261924
&mut row_num,
19271925
&Vec::new(),
1928-
p,
1926+
p + line_start,
19291927
l,
1930-
line_start,
19311928
show_code_change,
19321929
max_line_num_len,
19331930
&file_lines,
@@ -1941,9 +1938,8 @@ impl EmitterWriter {
19411938
&mut buffer,
19421939
&mut row_num,
19431940
highlight_parts,
1944-
line_pos,
1941+
line_pos + line_start,
19451942
line,
1946-
line_start,
19471943
show_code_change,
19481944
max_line_num_len,
19491945
&file_lines,
@@ -2167,49 +2163,44 @@ impl EmitterWriter {
21672163
buffer: &mut StyledBuffer,
21682164
row_num: &mut usize,
21692165
highlight_parts: &Vec<SubstitutionHighlight>,
2170-
line_pos: usize,
2171-
line: &str,
2172-
line_start: usize,
2166+
line_num: usize,
2167+
line_to_add: &str,
21732168
show_code_change: DisplaySuggestion,
21742169
max_line_num_len: usize,
21752170
file_lines: &FileLines,
21762171
is_multiline: bool,
21772172
) {
21782173
if let DisplaySuggestion::Diff = show_code_change {
2179-
// Add the line number for both addition and removal to drive the point home.
2180-
//
2181-
// N - fn foo<A: T>(bar: A) {
2182-
// N + fn foo(bar: impl T) {
2183-
let number_of_lines = file_lines.lines.len();
2184-
for (index, line_to_remove) in
2185-
file_lines.lines.iter().take(number_of_lines - 1).enumerate()
2186-
{
2174+
// We need to print more than one line if the span we need to remove is multiline.
2175+
// For more info: https://github.com/rust-lang/rust/issues/92741
2176+
let lines_to_remove = file_lines.lines.iter().take(file_lines.lines.len() - 1);
2177+
for (index, line_to_remove) in lines_to_remove.enumerate() {
21872178
buffer.puts(
21882179
*row_num - 1,
21892180
0,
2190-
&self.maybe_anonymized(line_start + line_pos + index),
2181+
&self.maybe_anonymized(line_num + index),
21912182
Style::LineNumber,
21922183
);
21932184
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
2194-
buffer.puts(
2195-
*row_num - 1,
2196-
max_line_num_len + 3,
2197-
&normalize_whitespace(
2198-
&file_lines.file.get_line(line_to_remove.line_index).unwrap(),
2199-
),
2200-
Style::NoStyle,
2185+
let line = normalize_whitespace(
2186+
&file_lines.file.get_line(line_to_remove.line_index).unwrap(),
22012187
);
2188+
buffer.puts(*row_num - 1, max_line_num_len + 3, &line, Style::NoStyle);
22022189
*row_num += 1;
22032190
}
2204-
let last_line = &file_lines
2205-
.file
2206-
.get_line(file_lines.lines[number_of_lines - 1].line_index)
2207-
.unwrap();
2208-
if last_line != line {
2191+
// If the last line is exactly equal to the line we need to add, we can skip both of them.
2192+
// This allows us to avoid output like the following:
2193+
// 2 - &
2194+
// 2 + if true { true } else { false }
2195+
// 3 - if true { true } else { false }
2196+
// If those lines aren't equal, we print their diff
2197+
let last_line_index = file_lines.lines[file_lines.lines.len() - 1].line_index;
2198+
let last_line = &file_lines.file.get_line(last_line_index).unwrap();
2199+
if last_line != line_to_add {
22092200
buffer.puts(
22102201
*row_num - 1,
22112202
0,
2212-
&self.maybe_anonymized(line_start + line_pos + number_of_lines - 1),
2203+
&self.maybe_anonymized(line_num + file_lines.lines.len() - 1),
22132204
Style::LineNumber,
22142205
);
22152206
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
@@ -2219,27 +2210,16 @@ impl EmitterWriter {
22192210
&normalize_whitespace(last_line),
22202211
Style::NoStyle,
22212212
);
2222-
buffer.puts(
2223-
*row_num,
2224-
0,
2225-
&self.maybe_anonymized(line_start + line_pos),
2226-
Style::LineNumber,
2227-
);
2213+
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
22282214
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
2229-
// print the suggestion
2230-
buffer.append(*row_num, &normalize_whitespace(line), Style::NoStyle);
2215+
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
22312216
} else {
22322217
*row_num -= 2;
22332218
}
22342219
} else if is_multiline {
2235-
buffer.puts(
2236-
*row_num,
2237-
0,
2238-
&self.maybe_anonymized(line_start + line_pos),
2239-
Style::LineNumber,
2240-
);
2220+
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
22412221
match &highlight_parts[..] {
2242-
[SubstitutionHighlight { start: 0, end }] if *end == line.len() => {
2222+
[SubstitutionHighlight { start: 0, end }] if *end == line_to_add.len() => {
22432223
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
22442224
}
22452225
[] => {
@@ -2249,24 +2229,17 @@ impl EmitterWriter {
22492229
buffer.puts(*row_num, max_line_num_len + 1, "~ ", Style::Addition);
22502230
}
22512231
}
2252-
// print the suggestion
2253-
buffer.append(*row_num, &normalize_whitespace(line), Style::NoStyle);
2232+
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
22542233
} else {
2255-
buffer.puts(
2256-
*row_num,
2257-
0,
2258-
&self.maybe_anonymized(line_start + line_pos),
2259-
Style::LineNumber,
2260-
);
2234+
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
22612235
draw_col_separator(buffer, *row_num, max_line_num_len + 1);
2262-
// print the suggestion
2263-
buffer.append(*row_num, &normalize_whitespace(line), Style::NoStyle);
2236+
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
22642237
}
22652238

22662239
// Colorize addition/replacements with green.
22672240
for &SubstitutionHighlight { start, end } in highlight_parts {
22682241
// Account for tabs when highlighting (#87972).
2269-
let tabs: usize = line
2242+
let tabs: usize = line_to_add
22702243
.chars()
22712244
.take(start)
22722245
.map(|ch| match ch {

0 commit comments

Comments
 (0)