Skip to content

Commit 50f9807

Browse files
committed
fix: Use untrimmed line numbers for trimmed suggestions
1 parent 779e19d commit 50f9807

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

compiler/rustc_errors/src/diagnostic.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
936936

937937
let parts = suggestion
938938
.into_iter()
939-
.map(|(span, snippet)| SubstitutionPart { snippet, span })
939+
.map(|(span, snippet)| SubstitutionPart { snippet, span, original_span: span })
940940
.collect::<Vec<_>>();
941941

942942
assert!(!parts.is_empty());
@@ -1028,7 +1028,11 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
10281028
);
10291029
self.push_suggestion(CodeSuggestion {
10301030
substitutions: vec![Substitution {
1031-
parts: vec![SubstitutionPart { snippet: suggestion.to_string(), span: sp }],
1031+
parts: vec![SubstitutionPart {
1032+
snippet: suggestion.to_string(),
1033+
span: sp,
1034+
original_span: sp,
1035+
}],
10321036
}],
10331037
msg: self.subdiagnostic_message_to_diagnostic_message(msg),
10341038
style,
@@ -1093,7 +1097,9 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
10931097
!(sp.is_empty() && snippet.is_empty()),
10941098
"Span `{sp:?}` must not be empty and have no suggestion"
10951099
);
1096-
Substitution { parts: vec![SubstitutionPart { snippet, span: sp }] }
1100+
Substitution {
1101+
parts: vec![SubstitutionPart { snippet, span: sp, original_span: sp }],
1102+
}
10971103
})
10981104
.collect();
10991105
self.push_suggestion(CodeSuggestion {
@@ -1120,7 +1126,7 @@ impl<'a, G: EmissionGuarantee> Diag<'a, G> {
11201126
.map(|sugg| {
11211127
let mut parts = sugg
11221128
.into_iter()
1123-
.map(|(span, snippet)| SubstitutionPart { snippet, span })
1129+
.map(|(span, snippet)| SubstitutionPart { snippet, span, original_span: span })
11241130
.collect::<Vec<_>>();
11251131

11261132
parts.sort_unstable_by_key(|part| part.span);

compiler/rustc_errors/src/emitter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,11 +2154,11 @@ impl HumanEmitter {
21542154

21552155
assert!(!file_lines.lines.is_empty() || parts[0].span.is_dummy());
21562156

2157-
let line_start = sm.lookup_char_pos(parts[0].span.lo()).line;
2157+
let line_start = sm.lookup_char_pos(parts[0].original_span.lo()).line;
21582158
let mut lines = complete.lines();
21592159
if lines.clone().next().is_none() {
21602160
// Account for a suggestion to completely remove a line(s) with whitespace (#94192).
2161-
let line_end = sm.lookup_char_pos(parts[0].span.hi()).line;
2161+
let line_end = sm.lookup_char_pos(parts[0].original_span.hi()).line;
21622162
for line in line_start..=line_end {
21632163
self.draw_line_num(
21642164
&mut buffer,

compiler/rustc_errors/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ pub struct Substitution {
222222
pub struct SubstitutionPart {
223223
pub span: Span,
224224
pub snippet: String,
225+
pub original_span: Span,
225226
}
226227

227228
/// Used to translate between `Span`s and byte positions within a single output line in highlighted
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ compile-flags: -Z ui-testing=no
2+
fn function_with_lots_of_arguments(a: i32, b: char, c: i32, d: i32, e: i32, f: i32) {}
3+
4+
fn main() {
5+
let variable_name = 42;
6+
function_with_lots_of_arguments(
7+
variable_name,
8+
variable_name,
9+
variable_name,
10+
variable_name,
11+
variable_name,
12+
);
13+
//~^^^^^^^ ERROR this function takes 6 arguments but 5 arguments were supplied [E0061]
14+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
error[E0061]: this function takes 6 arguments but 5 arguments were supplied
2+
--> $DIR/trimmed_multiline_suggestion.rs:6:5
3+
|
4+
6 | function_with_lots_of_arguments(
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7 | variable_name,
7+
8 | variable_name,
8+
| ------------- argument #2 of type `char` is missing
9+
|
10+
note: function defined here
11+
--> $DIR/trimmed_multiline_suggestion.rs:2:4
12+
|
13+
2 | fn function_with_lots_of_arguments(a: i32, b: char, c: i32, d: i32, e: i32, f: i32) {}
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -------
15+
help: provide the argument
16+
|
17+
6 | function_with_lots_of_arguments(
18+
7 | variable_name,
19+
8 ~ /* char */,
20+
9 ~ variable_name,
21+
|
22+
23+
error: aborting due to 1 previous error
24+
25+
For more information about this error, try `rustc --explain E0061`.

0 commit comments

Comments
 (0)