Skip to content

Commit 2c1990d

Browse files
authoredJun 1, 2022
Rollup merge of #97605 - estebank:suggestion-filename, r=oli-obk
Mention filename in suggestion when it differs from primary span
2 parents d126de1 + 310b1a9 commit 2c1990d

File tree

7 files changed

+39
-4
lines changed

7 files changed

+39
-4
lines changed
 

‎compiler/rustc_errors/src/emitter.rs

+33-4
Original file line numberDiff line numberDiff line change
@@ -1715,6 +1715,7 @@ impl EmitterWriter {
17151715

17161716
fn emit_suggestion_default(
17171717
&mut self,
1718+
span: &MultiSpan,
17181719
suggestion: &CodeSuggestion,
17191720
args: &FluentArgs<'_>,
17201721
level: &Level,
@@ -1766,6 +1767,30 @@ impl EmitterWriter {
17661767
None,
17671768
}
17681769

1770+
if let Some(span) = span.primary_span() {
1771+
// Compare the primary span of the diagnostic with the span of the suggestion
1772+
// being emitted. If they belong to the same file, we don't *need* to show the
1773+
// file name, saving in verbosity, but if it *isn't* we do need it, otherwise we're
1774+
// telling users to make a change but not clarifying *where*.
1775+
let loc = sm.lookup_char_pos(parts[0].span.lo());
1776+
if loc.file.name != sm.span_to_filename(span) && loc.file.name.is_real() {
1777+
buffer.puts(row_num - 1, 0, "--> ", Style::LineNumber);
1778+
buffer.append(
1779+
row_num - 1,
1780+
&format!(
1781+
"{}:{}:{}",
1782+
sm.filename_for_diagnostics(&loc.file.name),
1783+
sm.doctest_offset_line(&loc.file.name, loc.line),
1784+
loc.col.0 + 1,
1785+
),
1786+
Style::LineAndColumn,
1787+
);
1788+
for _ in 0..max_line_num_len {
1789+
buffer.prepend(row_num - 1, " ", Style::NoStyle);
1790+
}
1791+
row_num += 1;
1792+
}
1793+
}
17691794
let show_code_change = if has_deletion && !is_multiline {
17701795
DisplaySuggestion::Diff
17711796
} else if (parts.len() != 1 || parts[0].snippet.trim() != complete.trim())
@@ -1787,7 +1812,7 @@ impl EmitterWriter {
17871812
assert!(!file_lines.lines.is_empty() || parts[0].span.is_dummy());
17881813

17891814
let line_start = sm.lookup_char_pos(parts[0].span.lo()).line;
1790-
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
1815+
draw_col_separator_no_space(&mut buffer, row_num - 1, max_line_num_len + 1);
17911816
let mut lines = complete.lines();
17921817
if lines.clone().next().is_none() {
17931818
// Account for a suggestion to completely remove a line(s) with whitespace (#94192).
@@ -2046,9 +2071,13 @@ impl EmitterWriter {
20462071
) {
20472072
panic!("failed to emit error: {}", e);
20482073
}
2049-
} else if let Err(e) =
2050-
self.emit_suggestion_default(sugg, args, &Level::Help, max_line_num_len)
2051-
{
2074+
} else if let Err(e) = self.emit_suggestion_default(
2075+
span,
2076+
sugg,
2077+
args,
2078+
&Level::Help,
2079+
max_line_num_len,
2080+
) {
20522081
panic!("failed to emit error: {}", e);
20532082
};
20542083
}

‎src/test/ui/codemap_tests/two_files.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | impl Bar for Baz { }
55
| ^^^ type aliases cannot be used as traits
66
|
77
help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
8+
--> $DIR/two_files_data.rs:5:1
89
|
910
LL | trait Bar = dyn Foo;
1011
|

‎src/test/ui/const-generics/generic_const_exprs/issue-94287.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | If<{ FRAC <= 32 }>: True,
66
|
77
= note: the crate this constant originates from uses `#![feature(generic_const_exprs)]`
88
help: consider enabling this feature
9+
--> $DIR/issue-94287.rs:1:1
910
|
1011
LL | #![feature(generic_const_exprs)]
1112
|

‎src/test/ui/editions/edition-keywords-2015-2018-expansion.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | produces_async! {}
66
|
77
= note: this error originates in the macro `produces_async` (in Nightly builds, run with -Z macro-backtrace for more info)
88
help: escape `async` to use it as an identifier
9+
--> $DIR/auxiliary/edition-kw-macro-2018.rs:7:19
910
|
1011
LL | () => (pub fn r#async() {})
1112
| ++

‎src/test/ui/editions/edition-keywords-2018-2018-expansion.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ LL | produces_async! {}
66
|
77
= note: this error originates in the macro `produces_async` (in Nightly builds, run with -Z macro-backtrace for more info)
88
help: escape `async` to use it as an identifier
9+
--> $DIR/auxiliary/edition-kw-macro-2018.rs:7:19
910
|
1011
LL | () => (pub fn r#async() {})
1112
| ++

‎src/test/ui/issues/issue-70724-add_type_neq_err_label-unwrap.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ LL | assert_eq!(a, 0);
99
|
1010
= note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
1111
help: you might have forgotten to call this function
12+
--> $SRC_DIR/core/src/macros/mod.rs:LL:COL
1213
|
1314
LL | if !(*left_val() == *right_val) {
1415
| ++

‎src/test/ui/methods/method-on-ambiguous-numeric-type.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ LL | bar.pow(2);
4646
| ^^^
4747
|
4848
help: you must specify a type for this binding, like `i32`
49+
--> $DIR/auxiliary/macro-in-other-crate.rs:3:29
4950
|
5051
LL | ($ident:ident) => { let $ident: i32 = 42; }
5152
| ~~~~~~~~~~~

0 commit comments

Comments
 (0)
Please sign in to comment.