about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEduard-Mihai Burtescu <edy.burt@gmail.com>2019-12-15 17:47:51 +0200
committerEduard-Mihai Burtescu <edy.burt@gmail.com>2020-02-06 21:32:07 +0200
commitab080973cb3bfa96f71b7d29fc5c3e34893cf896 (patch)
tree12425bd6a346cea10960873edcab64816349bedb
parent5eaa9a150d470019ac0924ece7a23a0d1cefe5f4 (diff)
downloadrust-ab080973cb3bfa96f71b7d29fc5c3e34893cf896.tar.gz
rust-ab080973cb3bfa96f71b7d29fc5c3e34893cf896.zip
rustc_errors: hide "in this macro invocation" when redundant, more explicitly.
-rw-r--r--src/librustc_errors/annotate_snippet_emitter_writer.rs2
-rw-r--r--src/librustc_errors/emitter.rs63
-rw-r--r--src/test/ui/macros/same-sequence-span.stderr5
-rw-r--r--src/test/ui/proc-macro/generate-mod.stderr10
-rw-r--r--src/test/ui/proc-macro/invalid-punct-ident-4.stderr5
-rw-r--r--src/test/ui/proc-macro/lints_in_proc_macros.stderr5
-rw-r--r--src/test/ui/proc-macro/mixed-site-span.stderr10
-rw-r--r--src/test/ui/proc-macro/subspan.stderr16
-rw-r--r--src/test/ui/suggestions/dont-suggest-deref-inside-macro-issue-58298.stderr5
9 files changed, 53 insertions, 68 deletions
diff --git a/src/librustc_errors/annotate_snippet_emitter_writer.rs b/src/librustc_errors/annotate_snippet_emitter_writer.rs
index 1eda1c9200f..d83175694f4 100644
--- a/src/librustc_errors/annotate_snippet_emitter_writer.rs
+++ b/src/librustc_errors/annotate_snippet_emitter_writer.rs
@@ -32,7 +32,7 @@ impl Emitter for AnnotateSnippetEmitterWriter {
         let mut children = diag.children.clone();
         let (mut primary_span, suggestions) = self.primary_span_formatted(&diag);
 
-        self.render_multispans_macro_backtrace_and_fix_extern_macros(
+        self.fix_multispans_in_extern_macros_and_render_macro_backtrace(
             &self.source_map,
             &mut primary_span,
             &mut children,
diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs
index a57b59a1962..4857ff47462 100644
--- a/src/librustc_errors/emitter.rs
+++ b/src/librustc_errors/emitter.rs
@@ -271,7 +271,7 @@ pub trait Emitter {
         }
     }
 
-    fn render_multispans_macro_backtrace_and_fix_extern_macros(
+    fn fix_multispans_in_extern_macros_and_render_macro_backtrace(
         &self,
         source_map: &Option<Lrc<SourceMap>>,
         span: &mut MultiSpan,
@@ -279,10 +279,16 @@ pub trait Emitter {
         level: &Level,
         backtrace: bool,
     ) {
-        self.render_multispans_macro_backtrace(source_map, span, children, backtrace);
+        let mut external_spans_updated = false;
+        if !backtrace {
+            external_spans_updated =
+                self.fix_multispans_in_extern_macros(source_map, span, children);
+        }
+
+        self.render_multispans_macro_backtrace(span, children, backtrace);
 
         if !backtrace {
-            if self.fix_multispans_in_extern_macros(source_map, span, children) {
+            if external_spans_updated {
                 let msg = format!(
                     "this {} originates in a macro outside of the current crate \
                     (in Nightly builds, run with -Z macro-backtrace for more info)",
@@ -301,42 +307,33 @@ pub trait Emitter {
 
     fn render_multispans_macro_backtrace(
         &self,
-        source_map: &Option<Lrc<SourceMap>>,
         span: &mut MultiSpan,
         children: &mut Vec<SubDiagnostic>,
         backtrace: bool,
     ) {
-        self.render_multispan_macro_backtrace(source_map, span, backtrace);
+        self.render_multispan_macro_backtrace(span, backtrace);
         for child in children.iter_mut() {
-            self.render_multispan_macro_backtrace(source_map, &mut child.span, backtrace);
+            self.render_multispan_macro_backtrace(&mut child.span, backtrace);
         }
     }
 
-    fn render_multispan_macro_backtrace(
-        &self,
-        source_map: &Option<Lrc<SourceMap>>,
-        span: &mut MultiSpan,
-        always_backtrace: bool,
-    ) {
-        let sm = match source_map {
-            Some(ref sm) => sm,
-            None => return,
-        };
-
+    fn render_multispan_macro_backtrace(&self, span: &mut MultiSpan, always_backtrace: bool) {
         let mut new_labels: Vec<(Span, String)> = vec![];
 
-        // First, find all the spans in <*macros> and point instead at their use site
         for &sp in span.primary_spans() {
             if sp.is_dummy() {
                 continue;
             }
+
+            // FIXME(eddyb) use `retain` on `macro_backtrace` to remove all the
+            // entries we don't want to print, to make sure the indices being
+            // printed are contiguous (or omitted if there's only one entry).
             let macro_backtrace: Vec<_> = sp.macro_backtrace().collect();
             for (i, trace) in macro_backtrace.iter().rev().enumerate() {
-                // Only show macro locations that are local
-                // and display them like a span_note
                 if trace.def_site.is_dummy() {
                     continue;
                 }
+
                 if always_backtrace {
                     new_labels.push((
                         trace.def_site,
@@ -353,9 +350,21 @@ pub trait Emitter {
                         ),
                     ));
                 }
-                // Check to make sure we're not in any <*macros>
-                if !sm.span_to_filename(trace.def_site).is_macros()
-                    && matches!(trace.kind, ExpnKind::Macro(MacroKind::Bang, _))
+
+                // Don't add a label on the call site if the diagnostic itself
+                // already points to (a part of) that call site, as the label
+                // is meant for showing the relevant invocation when the actual
+                // diagnostic is pointing to some part of macro definition.
+                //
+                // This also handles the case where an external span got replaced
+                // with the call site span by `fix_multispans_in_extern_macros`.
+                //
+                // NB: `-Zmacro-backtrace` overrides this, for uniformity, as the
+                // "in this expansion of" label above is always added in that mode,
+                // and it needs an "in this macro invocation" label to match that.
+                let redundant_span = trace.call_site.contains(sp);
+
+                if !redundant_span && matches!(trace.kind, ExpnKind::Macro(MacroKind::Bang, _))
                     || always_backtrace
                 {
                     new_labels.push((
@@ -371,9 +380,9 @@ pub trait Emitter {
                             },
                         ),
                     ));
-                    if !always_backtrace {
-                        break;
-                    }
+                }
+                if !always_backtrace {
+                    break;
                 }
             }
         }
@@ -447,7 +456,7 @@ impl Emitter for EmitterWriter {
         let mut children = diag.children.clone();
         let (mut primary_span, suggestions) = self.primary_span_formatted(&diag);
 
-        self.render_multispans_macro_backtrace_and_fix_extern_macros(
+        self.fix_multispans_in_extern_macros_and_render_macro_backtrace(
             &self.sm,
             &mut primary_span,
             &mut children,
diff --git a/src/test/ui/macros/same-sequence-span.stderr b/src/test/ui/macros/same-sequence-span.stderr
index 896f579765f..0a7e019e59f 100644
--- a/src/test/ui/macros/same-sequence-span.stderr
+++ b/src/test/ui/macros/same-sequence-span.stderr
@@ -33,10 +33,7 @@ error: `$x:expr` may be followed by `=`, which is not allowed for `expr` fragmen
   --> $DIR/same-sequence-span.rs:19:1
    |
 LL | proc_macro_sequence::make_foo!();
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   | |
-   | not allowed after `expr` fragments
-   | in this macro invocation
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not allowed after `expr` fragments
    |
    = note: allowed there are: `=>`, `,` or `;`
 
diff --git a/src/test/ui/proc-macro/generate-mod.stderr b/src/test/ui/proc-macro/generate-mod.stderr
index fe53fb242f4..496bd86e988 100644
--- a/src/test/ui/proc-macro/generate-mod.stderr
+++ b/src/test/ui/proc-macro/generate-mod.stderr
@@ -2,10 +2,7 @@ error[E0412]: cannot find type `FromOutside` in this scope
   --> $DIR/generate-mod.rs:9:1
    |
 LL | generate_mod::check!();
-   | ^^^^^^^^^^^^^^^^^^^^^^^
-   | |
-   | not found in this scope
-   | in this macro invocation
+   | ^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
    |
    = note: possible candidate is found in another module, you can import it into scope:
            FromOutside
@@ -14,10 +11,7 @@ error[E0412]: cannot find type `Outer` in this scope
   --> $DIR/generate-mod.rs:9:1
    |
 LL | generate_mod::check!();
-   | ^^^^^^^^^^^^^^^^^^^^^^^
-   | |
-   | not found in this scope
-   | in this macro invocation
+   | ^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
    |
    = note: possible candidate is found in another module, you can import it into scope:
            Outer
diff --git a/src/test/ui/proc-macro/invalid-punct-ident-4.stderr b/src/test/ui/proc-macro/invalid-punct-ident-4.stderr
index e7764004e8d..cf85974316a 100644
--- a/src/test/ui/proc-macro/invalid-punct-ident-4.stderr
+++ b/src/test/ui/proc-macro/invalid-punct-ident-4.stderr
@@ -2,10 +2,7 @@ error: unexpected closing delimiter: `)`
   --> $DIR/invalid-punct-ident-4.rs:6:1
    |
 LL | lexer_failure!();
-   | ^^^^^^^^^^^^^^^^^
-   | |
-   | unexpected closing delimiter
-   | in this macro invocation
+   | ^^^^^^^^^^^^^^^^^ unexpected closing delimiter
 
 error: proc macro panicked
   --> $DIR/invalid-punct-ident-4.rs:6:1
diff --git a/src/test/ui/proc-macro/lints_in_proc_macros.stderr b/src/test/ui/proc-macro/lints_in_proc_macros.stderr
index f28b8c9fb73..2d97cd700be 100644
--- a/src/test/ui/proc-macro/lints_in_proc_macros.stderr
+++ b/src/test/ui/proc-macro/lints_in_proc_macros.stderr
@@ -2,10 +2,7 @@ error[E0425]: cannot find value `foobar2` in this scope
   --> $DIR/lints_in_proc_macros.rs:12:5
    |
 LL |     bang_proc_macro2!();
-   |     ^^^^^^^^^^^^^^^^^^^^
-   |     |
-   |     help: a local variable with a similar name exists: `foobar`
-   |     in this macro invocation
+   |     ^^^^^^^^^^^^^^^^^^^^ help: a local variable with a similar name exists: `foobar`
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/proc-macro/mixed-site-span.stderr b/src/test/ui/proc-macro/mixed-site-span.stderr
index 54d10fe0d90..b5ca6cb9b29 100644
--- a/src/test/ui/proc-macro/mixed-site-span.stderr
+++ b/src/test/ui/proc-macro/mixed-site-span.stderr
@@ -2,19 +2,13 @@ error[E0426]: use of undeclared label `'label_use`
   --> $DIR/mixed-site-span.rs:15:9
    |
 LL |         proc_macro_rules!();
-   |         ^^^^^^^^^^^^^^^^^^^^
-   |         |
-   |         undeclared label `'label_use`
-   |         in this macro invocation
+   |         ^^^^^^^^^^^^^^^^^^^^ undeclared label `'label_use`
 
 error[E0425]: cannot find value `local_use` in this scope
   --> $DIR/mixed-site-span.rs:15:9
    |
 LL |         proc_macro_rules!();
-   |         ^^^^^^^^^^^^^^^^^^^^
-   |         |
-   |         not found in this scope
-   |         in this macro invocation
+   |         ^^^^^^^^^^^^^^^^^^^^ not found in this scope
 
 error[E0425]: cannot find value `local_def` in this scope
   --> $DIR/mixed-site-span.rs:19:9
diff --git a/src/test/ui/proc-macro/subspan.stderr b/src/test/ui/proc-macro/subspan.stderr
index 06715c197bc..5117dd6d32d 100644
--- a/src/test/ui/proc-macro/subspan.stderr
+++ b/src/test/ui/proc-macro/subspan.stderr
@@ -2,7 +2,7 @@ error: found 'hi's
   --> $DIR/subspan.rs:11:1
    |
 LL | subspan!("hi");
-   | ^^^^^^^^^^^^^^^ in this macro invocation
+   | ^^^^^^^^^^^^^^^
    |
 note: here
   --> $DIR/subspan.rs:11:11
@@ -14,7 +14,7 @@ error: found 'hi's
   --> $DIR/subspan.rs:14:1
    |
 LL | subspan!("hihi");
-   | ^^^^^^^^^^^^^^^^^ in this macro invocation
+   | ^^^^^^^^^^^^^^^^^
    |
 note: here
   --> $DIR/subspan.rs:14:11
@@ -26,7 +26,7 @@ error: found 'hi's
   --> $DIR/subspan.rs:17:1
    |
 LL | subspan!("hihihi");
-   | ^^^^^^^^^^^^^^^^^^^ in this macro invocation
+   | ^^^^^^^^^^^^^^^^^^^
    |
 note: here
   --> $DIR/subspan.rs:17:11
@@ -38,7 +38,7 @@ error: found 'hi's
   --> $DIR/subspan.rs:20:1
    |
 LL | subspan!("why I hide? hi!");
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
 note: here
   --> $DIR/subspan.rs:20:17
@@ -50,7 +50,7 @@ error: found 'hi's
   --> $DIR/subspan.rs:21:1
    |
 LL | subspan!("hey, hi, hidy, hidy, hi hi");
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
 note: here
   --> $DIR/subspan.rs:21:16
@@ -62,7 +62,7 @@ error: found 'hi's
   --> $DIR/subspan.rs:22:1
    |
 LL | subspan!("this is a hi, and this is another hi");
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
 note: here
   --> $DIR/subspan.rs:22:12
@@ -74,7 +74,7 @@ error: found 'hi's
   --> $DIR/subspan.rs:23:1
    |
 LL | subspan!("how are you this evening");
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
 note: here
   --> $DIR/subspan.rs:23:24
@@ -86,7 +86,7 @@ error: found 'hi's
   --> $DIR/subspan.rs:24:1
    |
 LL | subspan!("this is highly eradic");
-   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
 note: here
   --> $DIR/subspan.rs:24:12
diff --git a/src/test/ui/suggestions/dont-suggest-deref-inside-macro-issue-58298.stderr b/src/test/ui/suggestions/dont-suggest-deref-inside-macro-issue-58298.stderr
index fe2583eafe5..f928510454a 100644
--- a/src/test/ui/suggestions/dont-suggest-deref-inside-macro-issue-58298.stderr
+++ b/src/test/ui/suggestions/dont-suggest-deref-inside-macro-issue-58298.stderr
@@ -4,10 +4,7 @@ error[E0308]: mismatched types
 LL | /     intrinsic_match! {
 LL | |         "abc"
 LL | |     };
-   | |      ^
-   | |      |
-   | |______expected `&str`, found struct `std::string::String`
-   |        in this macro invocation
+   | |______^ expected `&str`, found struct `std::string::String`
    |
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z macro-backtrace for more info)