Auto merge of #107671 - CastilloDel:master, r=estebank
Fix suggestions rendering when the diff span is multiline Fixes #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?
This commit is contained in:
commit
5dd0e1b7ae
5 changed files with 137 additions and 39 deletions
|
@ -1882,9 +1882,8 @@ impl EmitterWriter {
|
||||||
&mut buffer,
|
&mut buffer,
|
||||||
&mut row_num,
|
&mut row_num,
|
||||||
&Vec::new(),
|
&Vec::new(),
|
||||||
p,
|
p + line_start,
|
||||||
l,
|
l,
|
||||||
line_start,
|
|
||||||
show_code_change,
|
show_code_change,
|
||||||
max_line_num_len,
|
max_line_num_len,
|
||||||
&file_lines,
|
&file_lines,
|
||||||
|
@ -1907,9 +1906,8 @@ impl EmitterWriter {
|
||||||
&mut buffer,
|
&mut buffer,
|
||||||
&mut row_num,
|
&mut row_num,
|
||||||
&Vec::new(),
|
&Vec::new(),
|
||||||
p,
|
p + line_start,
|
||||||
l,
|
l,
|
||||||
line_start,
|
|
||||||
show_code_change,
|
show_code_change,
|
||||||
max_line_num_len,
|
max_line_num_len,
|
||||||
&file_lines,
|
&file_lines,
|
||||||
|
@ -1925,9 +1923,8 @@ impl EmitterWriter {
|
||||||
&mut buffer,
|
&mut buffer,
|
||||||
&mut row_num,
|
&mut row_num,
|
||||||
&Vec::new(),
|
&Vec::new(),
|
||||||
p,
|
p + line_start,
|
||||||
l,
|
l,
|
||||||
line_start,
|
|
||||||
show_code_change,
|
show_code_change,
|
||||||
max_line_num_len,
|
max_line_num_len,
|
||||||
&file_lines,
|
&file_lines,
|
||||||
|
@ -1941,9 +1938,8 @@ impl EmitterWriter {
|
||||||
&mut buffer,
|
&mut buffer,
|
||||||
&mut row_num,
|
&mut row_num,
|
||||||
highlight_parts,
|
highlight_parts,
|
||||||
line_pos,
|
line_pos + line_start,
|
||||||
line,
|
line,
|
||||||
line_start,
|
|
||||||
show_code_change,
|
show_code_change,
|
||||||
max_line_num_len,
|
max_line_num_len,
|
||||||
&file_lines,
|
&file_lines,
|
||||||
|
@ -2167,40 +2163,63 @@ impl EmitterWriter {
|
||||||
buffer: &mut StyledBuffer,
|
buffer: &mut StyledBuffer,
|
||||||
row_num: &mut usize,
|
row_num: &mut usize,
|
||||||
highlight_parts: &Vec<SubstitutionHighlight>,
|
highlight_parts: &Vec<SubstitutionHighlight>,
|
||||||
line_pos: usize,
|
line_num: usize,
|
||||||
line: &str,
|
line_to_add: &str,
|
||||||
line_start: usize,
|
|
||||||
show_code_change: DisplaySuggestion,
|
show_code_change: DisplaySuggestion,
|
||||||
max_line_num_len: usize,
|
max_line_num_len: usize,
|
||||||
file_lines: &FileLines,
|
file_lines: &FileLines,
|
||||||
is_multiline: bool,
|
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 {
|
if let DisplaySuggestion::Diff = show_code_change {
|
||||||
// Add the line number for both addition and removal to drive the point home.
|
// We need to print more than one line if the span we need to remove is multiline.
|
||||||
//
|
// For more info: https://github.com/rust-lang/rust/issues/92741
|
||||||
// N - fn foo<A: T>(bar: A) {
|
let lines_to_remove = file_lines.lines.iter().take(file_lines.lines.len() - 1);
|
||||||
// N + fn foo(bar: impl T) {
|
for (index, line_to_remove) in lines_to_remove.enumerate() {
|
||||||
buffer.puts(
|
buffer.puts(
|
||||||
*row_num - 1,
|
*row_num - 1,
|
||||||
0,
|
0,
|
||||||
&self.maybe_anonymized(line_start + line_pos),
|
&self.maybe_anonymized(line_num + index),
|
||||||
Style::LineNumber,
|
Style::LineNumber,
|
||||||
);
|
);
|
||||||
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
|
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
|
||||||
buffer.puts(
|
let line = normalize_whitespace(
|
||||||
*row_num - 1,
|
&file_lines.file.get_line(line_to_remove.line_index).unwrap(),
|
||||||
max_line_num_len + 3,
|
);
|
||||||
&normalize_whitespace(
|
buffer.puts(*row_num - 1, max_line_num_len + 3, &line, Style::NoStyle);
|
||||||
&file_lines.file.get_line(file_lines.lines[line_pos].line_index).unwrap(),
|
*row_num += 1;
|
||||||
),
|
}
|
||||||
Style::NoStyle,
|
// If the last line is exactly equal to the line we need to add, we can skip both of them.
|
||||||
);
|
// This allows us to avoid output like the following:
|
||||||
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
|
// 2 - &
|
||||||
|
// 2 + if true { true } else { false }
|
||||||
|
// 3 - if true { true } else { false }
|
||||||
|
// If those lines aren't equal, we print their diff
|
||||||
|
let last_line_index = file_lines.lines[file_lines.lines.len() - 1].line_index;
|
||||||
|
let last_line = &file_lines.file.get_line(last_line_index).unwrap();
|
||||||
|
if last_line != line_to_add {
|
||||||
|
buffer.puts(
|
||||||
|
*row_num - 1,
|
||||||
|
0,
|
||||||
|
&self.maybe_anonymized(line_num + file_lines.lines.len() - 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_num), Style::LineNumber);
|
||||||
|
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
|
||||||
|
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
|
||||||
|
} else {
|
||||||
|
*row_num -= 2;
|
||||||
|
}
|
||||||
} else if is_multiline {
|
} else if is_multiline {
|
||||||
|
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
|
||||||
match &highlight_parts[..] {
|
match &highlight_parts[..] {
|
||||||
[SubstitutionHighlight { start: 0, end }] if *end == line.len() => {
|
[SubstitutionHighlight { start: 0, end }] if *end == line_to_add.len() => {
|
||||||
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
|
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
|
||||||
}
|
}
|
||||||
[] => {
|
[] => {
|
||||||
|
@ -2210,17 +2229,17 @@ impl EmitterWriter {
|
||||||
buffer.puts(*row_num, max_line_num_len + 1, "~ ", Style::Addition);
|
buffer.puts(*row_num, max_line_num_len + 1, "~ ", Style::Addition);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
|
||||||
} else {
|
} else {
|
||||||
|
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
|
||||||
draw_col_separator(buffer, *row_num, max_line_num_len + 1);
|
draw_col_separator(buffer, *row_num, max_line_num_len + 1);
|
||||||
|
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
|
||||||
}
|
}
|
||||||
|
|
||||||
// print the suggestion
|
|
||||||
buffer.append(*row_num, &normalize_whitespace(line), Style::NoStyle);
|
|
||||||
|
|
||||||
// Colorize addition/replacements with green.
|
// Colorize addition/replacements with green.
|
||||||
for &SubstitutionHighlight { start, end } in highlight_parts {
|
for &SubstitutionHighlight { start, end } in highlight_parts {
|
||||||
// Account for tabs when highlighting (#87972).
|
// Account for tabs when highlighting (#87972).
|
||||||
let tabs: usize = line
|
let tabs: usize = line_to_add
|
||||||
.chars()
|
.chars()
|
||||||
.take(start)
|
.take(start)
|
||||||
.map(|ch| match ch {
|
.map(|ch| match ch {
|
||||||
|
|
|
@ -10,7 +10,7 @@ use std::path::Path;
|
||||||
const ENTRY_LIMIT: usize = 1000;
|
const ENTRY_LIMIT: usize = 1000;
|
||||||
// FIXME: The following limits should be reduced eventually.
|
// FIXME: The following limits should be reduced eventually.
|
||||||
const ROOT_ENTRY_LIMIT: usize = 939;
|
const ROOT_ENTRY_LIMIT: usize = 939;
|
||||||
const ISSUES_ENTRY_LIMIT: usize = 1998;
|
const ISSUES_ENTRY_LIMIT: usize = 2001;
|
||||||
|
|
||||||
fn check_entries(path: &Path, bad: &mut bool) {
|
fn check_entries(path: &Path, bad: &mut bool) {
|
||||||
for dir in Walk::new(&path.join("ui")) {
|
for dir in Walk::new(&path.join("ui")) {
|
||||||
|
|
13
tests/ui/issues/issue-92741.fixed
Normal file
13
tests/ui/issues/issue-92741.fixed
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
// run-rustfix
|
||||||
|
fn main() {}
|
||||||
|
fn _foo() -> bool {
|
||||||
|
if true { true } else { false }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _bar() -> bool {
|
||||||
|
if true { true } else { false }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _baz() -> bool {
|
||||||
|
if true { true } else { false }
|
||||||
|
}
|
17
tests/ui/issues/issue-92741.rs
Normal file
17
tests/ui/issues/issue-92741.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
// run-rustfix
|
||||||
|
fn main() {}
|
||||||
|
fn _foo() -> bool {
|
||||||
|
& //~ ERROR 4:5: 6:36: mismatched types [E0308]
|
||||||
|
mut
|
||||||
|
if true { true } else { false }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _bar() -> bool {
|
||||||
|
& //~ ERROR 10:5: 11:40: mismatched types [E0308]
|
||||||
|
mut if true { true } else { false }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _baz() -> bool {
|
||||||
|
& mut //~ ERROR 15:5: 16:36: mismatched types [E0308]
|
||||||
|
if true { true } else { false }
|
||||||
|
}
|
49
tests/ui/issues/issue-92741.stderr
Normal file
49
tests/ui/issues/issue-92741.stderr
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
error[E0308]: mismatched types
|
||||||
|
--> $DIR/issue-92741.rs:4:5
|
||||||
|
|
|
||||||
|
LL | fn _foo() -> bool {
|
||||||
|
| ---- expected `bool` because of return type
|
||||||
|
LL | / &
|
||||||
|
LL | | mut
|
||||||
|
LL | | if true { true } else { false }
|
||||||
|
| |___________________________________^ expected `bool`, found `&mut bool`
|
||||||
|
|
|
||||||
|
help: consider removing the borrow
|
||||||
|
|
|
||||||
|
LL - &
|
||||||
|
LL - mut
|
||||||
|
|
|
||||||
|
|
||||||
|
error[E0308]: mismatched types
|
||||||
|
--> $DIR/issue-92741.rs:10:5
|
||||||
|
|
|
||||||
|
LL | fn _bar() -> bool {
|
||||||
|
| ---- expected `bool` because of return type
|
||||||
|
LL | / &
|
||||||
|
LL | | mut if true { true } else { false }
|
||||||
|
| |_______________________________________^ expected `bool`, found `&mut bool`
|
||||||
|
|
|
||||||
|
help: consider removing the borrow
|
||||||
|
|
|
||||||
|
LL - &
|
||||||
|
LL - mut if true { true } else { false }
|
||||||
|
LL + if true { true } else { false }
|
||||||
|
|
|
||||||
|
|
||||||
|
error[E0308]: mismatched types
|
||||||
|
--> $DIR/issue-92741.rs:15:5
|
||||||
|
|
|
||||||
|
LL | fn _baz() -> bool {
|
||||||
|
| ---- expected `bool` because of return type
|
||||||
|
LL | / & mut
|
||||||
|
LL | | if true { true } else { false }
|
||||||
|
| |___________________________________^ expected `bool`, found `&mut bool`
|
||||||
|
|
|
||||||
|
help: consider removing the borrow
|
||||||
|
|
|
||||||
|
LL - & mut
|
||||||
|
|
|
||||||
|
|
||||||
|
error: aborting due to 3 previous errors
|
||||||
|
|
||||||
|
For more information about this error, try `rustc --explain E0308`.
|
Loading…
Add table
Reference in a new issue