Skip to content

Commit 5dd0e1b

Browse files
committed
Auto merge of rust-lang#107671 - CastilloDel:master, r=estebank
Fix suggestions rendering when the diff span is multiline Fixes rust-lang#92741 cc `@estebank` I think, I finally fixed. I still want to go back and try to clean up the code a bit. I'm open to suggestions. Some examples of the new suggestions: ``` help: consider removing the borrow | 2 - & | ``` ``` help: consider removing the borrow | 2 - & 3 - mut | ``` ``` help: consider removing the borrow | 2 - & 3 - mut if true { true } else { false } 2 + if true { true } else { false } | ``` Should we add a test to ensure this behavior doesn't disappear in the future?
2 parents e4dd9ed + f0830c0 commit 5dd0e1b

File tree

5 files changed

+137
-39
lines changed

5 files changed

+137
-39
lines changed

compiler/rustc_errors/src/emitter.rs

+57-38
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,40 +2163,63 @@ 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
) {
2178-
// Print the span column to avoid confusion
2179-
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_start + line_pos), Style::LineNumber);
21802173
if let DisplaySuggestion::Diff = show_code_change {
2181-
// Add the line number for both addition and removal to drive the point home.
2182-
//
2183-
// N - fn foo<A: T>(bar: A) {
2184-
// N + fn foo(bar: impl T) {
2185-
buffer.puts(
2186-
*row_num - 1,
2187-
0,
2188-
&self.maybe_anonymized(line_start + line_pos),
2189-
Style::LineNumber,
2190-
);
2191-
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
2192-
buffer.puts(
2193-
*row_num - 1,
2194-
max_line_num_len + 3,
2195-
&normalize_whitespace(
2196-
&file_lines.file.get_line(file_lines.lines[line_pos].line_index).unwrap(),
2197-
),
2198-
Style::NoStyle,
2199-
);
2200-
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
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() {
2178+
buffer.puts(
2179+
*row_num - 1,
2180+
0,
2181+
&self.maybe_anonymized(line_num + index),
2182+
Style::LineNumber,
2183+
);
2184+
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
2185+
let line = normalize_whitespace(
2186+
&file_lines.file.get_line(line_to_remove.line_index).unwrap(),
2187+
);
2188+
buffer.puts(*row_num - 1, max_line_num_len + 3, &line, Style::NoStyle);
2189+
*row_num += 1;
2190+
}
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 {
2200+
buffer.puts(
2201+
*row_num - 1,
2202+
0,
2203+
&self.maybe_anonymized(line_num + file_lines.lines.len() - 1),
2204+
Style::LineNumber,
2205+
);
2206+
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
2207+
buffer.puts(
2208+
*row_num - 1,
2209+
max_line_num_len + 3,
2210+
&normalize_whitespace(last_line),
2211+
Style::NoStyle,
2212+
);
2213+
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
2214+
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
2215+
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
2216+
} else {
2217+
*row_num -= 2;
2218+
}
22012219
} else if is_multiline {
2220+
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
22022221
match &highlight_parts[..] {
2203-
[SubstitutionHighlight { start: 0, end }] if *end == line.len() => {
2222+
[SubstitutionHighlight { start: 0, end }] if *end == line_to_add.len() => {
22042223
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
22052224
}
22062225
[] => {
@@ -2210,17 +2229,17 @@ impl EmitterWriter {
22102229
buffer.puts(*row_num, max_line_num_len + 1, "~ ", Style::Addition);
22112230
}
22122231
}
2232+
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
22132233
} else {
2234+
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
22142235
draw_col_separator(buffer, *row_num, max_line_num_len + 1);
2236+
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
22152237
}
22162238

2217-
// print the suggestion
2218-
buffer.append(*row_num, &normalize_whitespace(line), Style::NoStyle);
2219-
22202239
// Colorize addition/replacements with green.
22212240
for &SubstitutionHighlight { start, end } in highlight_parts {
22222241
// Account for tabs when highlighting (#87972).
2223-
let tabs: usize = line
2242+
let tabs: usize = line_to_add
22242243
.chars()
22252244
.take(start)
22262245
.map(|ch| match ch {

src/tools/tidy/src/ui_tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::path::Path;
1010
const ENTRY_LIMIT: usize = 1000;
1111
// FIXME: The following limits should be reduced eventually.
1212
const ROOT_ENTRY_LIMIT: usize = 939;
13-
const ISSUES_ENTRY_LIMIT: usize = 1998;
13+
const ISSUES_ENTRY_LIMIT: usize = 2001;
1414

1515
fn check_entries(path: &Path, bad: &mut bool) {
1616
for dir in Walk::new(&path.join("ui")) {

tests/ui/issues/issue-92741.fixed

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// run-rustfix
2+
fn main() {}
3+
fn _foo() -> bool {
4+
if true { true } else { false }
5+
}
6+
7+
fn _bar() -> bool {
8+
if true { true } else { false }
9+
}
10+
11+
fn _baz() -> bool {
12+
if true { true } else { false }
13+
}

tests/ui/issues/issue-92741.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// run-rustfix
2+
fn main() {}
3+
fn _foo() -> bool {
4+
& //~ ERROR 4:5: 6:36: mismatched types [E0308]
5+
mut
6+
if true { true } else { false }
7+
}
8+
9+
fn _bar() -> bool {
10+
& //~ ERROR 10:5: 11:40: mismatched types [E0308]
11+
mut if true { true } else { false }
12+
}
13+
14+
fn _baz() -> bool {
15+
& mut //~ ERROR 15:5: 16:36: mismatched types [E0308]
16+
if true { true } else { false }
17+
}

tests/ui/issues/issue-92741.stderr

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-92741.rs:4:5
3+
|
4+
LL | fn _foo() -> bool {
5+
| ---- expected `bool` because of return type
6+
LL | / &
7+
LL | | mut
8+
LL | | if true { true } else { false }
9+
| |___________________________________^ expected `bool`, found `&mut bool`
10+
|
11+
help: consider removing the borrow
12+
|
13+
LL - &
14+
LL - mut
15+
|
16+
17+
error[E0308]: mismatched types
18+
--> $DIR/issue-92741.rs:10:5
19+
|
20+
LL | fn _bar() -> bool {
21+
| ---- expected `bool` because of return type
22+
LL | / &
23+
LL | | mut if true { true } else { false }
24+
| |_______________________________________^ expected `bool`, found `&mut bool`
25+
|
26+
help: consider removing the borrow
27+
|
28+
LL - &
29+
LL - mut if true { true } else { false }
30+
LL + if true { true } else { false }
31+
|
32+
33+
error[E0308]: mismatched types
34+
--> $DIR/issue-92741.rs:15:5
35+
|
36+
LL | fn _baz() -> bool {
37+
| ---- expected `bool` because of return type
38+
LL | / & mut
39+
LL | | if true { true } else { false }
40+
| |___________________________________^ expected `bool`, found `&mut bool`
41+
|
42+
help: consider removing the borrow
43+
|
44+
LL - & mut
45+
|
46+
47+
error: aborting due to 3 previous errors
48+
49+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)