]> git.lizzy.rs Git - rust.git/commitdiff
Mention filename in suggestion when it differs from primary span
authorEsteban Küber <esteban@kuber.com.ar>
Tue, 31 May 2022 23:38:03 +0000 (16:38 -0700)
committerEsteban Küber <esteban@kuber.com.ar>
Wed, 1 Jun 2022 16:52:03 +0000 (09:52 -0700)
compiler/rustc_errors/src/emitter.rs
src/test/ui/codemap_tests/two_files.stderr
src/test/ui/const-generics/generic_const_exprs/issue-94287.stderr
src/test/ui/editions/edition-keywords-2015-2018-expansion.stderr
src/test/ui/editions/edition-keywords-2018-2018-expansion.stderr
src/test/ui/issues/issue-70724-add_type_neq_err_label-unwrap.stderr
src/test/ui/methods/method-on-ambiguous-numeric-type.stderr

index 3fdc8cf8ac25fb9cd30da027b104e59844f92164..e9e7065ec03cc744b90b8bda4a96c903926b8ec2 100644 (file)
@@ -1715,6 +1715,7 @@ fn emit_message_default(
 
     fn emit_suggestion_default(
         &mut self,
+        span: &MultiSpan,
         suggestion: &CodeSuggestion,
         args: &FluentArgs<'_>,
         level: &Level,
@@ -1766,6 +1767,30 @@ enum DisplaySuggestion {
                 None,
             }
 
+            if let Some(span) = span.primary_span() {
+                // Compare the primary span of the diagnostic with the span of the suggestion
+                // being emitted.  If they belong to the same file, we don't *need* to show the
+                // file name, saving in verbosity, but if it *isn't* we do need it, otherwise we're
+                // telling users to make a change but not clarifying *where*.
+                let loc = sm.lookup_char_pos(parts[0].span.lo());
+                if loc.file.name != sm.span_to_filename(span) && loc.file.name.is_real() {
+                    buffer.puts(row_num - 1, 0, "--> ", Style::LineNumber);
+                    buffer.append(
+                        row_num - 1,
+                        &format!(
+                            "{}:{}:{}",
+                            sm.filename_for_diagnostics(&loc.file.name),
+                            sm.doctest_offset_line(&loc.file.name, loc.line),
+                            loc.col.0 + 1,
+                        ),
+                        Style::LineAndColumn,
+                    );
+                    for _ in 0..max_line_num_len {
+                        buffer.prepend(row_num - 1, " ", Style::NoStyle);
+                    }
+                    row_num += 1;
+                }
+            }
             let show_code_change = if has_deletion && !is_multiline {
                 DisplaySuggestion::Diff
             } else if (parts.len() != 1 || parts[0].snippet.trim() != complete.trim())
@@ -1787,7 +1812,7 @@ enum DisplaySuggestion {
             assert!(!file_lines.lines.is_empty() || parts[0].span.is_dummy());
 
             let line_start = sm.lookup_char_pos(parts[0].span.lo()).line;
-            draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
+            draw_col_separator_no_space(&mut buffer, row_num - 1, max_line_num_len + 1);
             let mut lines = complete.lines();
             if lines.clone().next().is_none() {
                 // Account for a suggestion to completely remove a line(s) with whitespace (#94192).
@@ -2046,9 +2071,13 @@ fn emit_messages_default(
                             ) {
                                 panic!("failed to emit error: {}", e);
                             }
-                        } else if let Err(e) =
-                            self.emit_suggestion_default(sugg, args, &Level::Help, max_line_num_len)
-                        {
+                        } else if let Err(e) = self.emit_suggestion_default(
+                            span,
+                            sugg,
+                            args,
+                            &Level::Help,
+                            max_line_num_len,
+                        ) {
                             panic!("failed to emit error: {}", e);
                         };
                     }
index aff51ee9e2f541514975efbd1175be5e2b24b67a..2eb3fd56783147fd58047c9f214dadb9ae12fe54 100644 (file)
@@ -5,6 +5,7 @@ LL | impl Bar for Baz { }
    |      ^^^ type aliases cannot be used as traits
    |
 help: you might have meant to use `#![feature(trait_alias)]` instead of a `type` alias
+  --> $DIR/two_files_data.rs:5:1
    |
 LL | trait Bar = dyn Foo;
    |
index c918651ba62d986d603e3df5c19d22e3e74ecc38..7390a007742fcce629a9c166f1c6a79596c7570e 100644 (file)
@@ -6,6 +6,7 @@ LL |     If<{ FRAC <= 32 }>: True,
    |
    = note: the crate this constant originates from uses `#![feature(generic_const_exprs)]`
 help: consider enabling this feature
+  --> $DIR/issue-94287.rs:1:1
    |
 LL | #![feature(generic_const_exprs)]
    |
index 23dad2c16b22c95fa286b29ac19c1b6508e48c7d..570bbac2b21f649d6d1df5ae37c123e786a2ba93 100644 (file)
@@ -6,6 +6,7 @@ LL |     produces_async! {}
    |
    = note: this error originates in the macro `produces_async` (in Nightly builds, run with -Z macro-backtrace for more info)
 help: escape `async` to use it as an identifier
+  --> $DIR/auxiliary/edition-kw-macro-2018.rs:7:19
    |
 LL |     () => (pub fn r#async() {})
    |                   ++
index 67f9aa6041399b4ecf2a4afc33ecc487f849a171..69f275746bdc44be3db5b5552e5cd8e2c0759317 100644 (file)
@@ -6,6 +6,7 @@ LL |     produces_async! {}
    |
    = note: this error originates in the macro `produces_async` (in Nightly builds, run with -Z macro-backtrace for more info)
 help: escape `async` to use it as an identifier
+  --> $DIR/auxiliary/edition-kw-macro-2018.rs:7:19
    |
 LL |     () => (pub fn r#async() {})
    |                   ++
index 4c11f3544948e694becba10e8d13aa8c0c0699f1..c6e6ea1e096af76068f6be6627d27cd95e66cd24 100644 (file)
@@ -9,6 +9,7 @@ LL |     assert_eq!(a, 0);
    |
    = note: this error originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
 help: you might have forgotten to call this function
+  --> $SRC_DIR/core/src/macros/mod.rs:LL:COL
    |
 LL |                 if !(*left_val() == *right_val) {
    |                               ++
index 09978b35f7e8a10df591bb901aeef37ea8e9ff62..0af58bc61f49ea88416d71682d3c51229927efe8 100644 (file)
@@ -46,6 +46,7 @@ LL |     bar.pow(2);
    |         ^^^
    |
 help: you must specify a type for this binding, like `i32`
+  --> $DIR/auxiliary/macro-in-other-crate.rs:3:29
    |
 LL |     ($ident:ident) => { let $ident: i32 = 42; }
    |                             ~~~~~~~~~~~