about summary refs log tree commit diff
path: root/compiler/rustc_span/src
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_span/src')
-rw-r--r--compiler/rustc_span/src/lib.rs12
1 files changed, 10 insertions, 2 deletions
diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs
index d23e1825e6e..798e186a94b 100644
--- a/compiler/rustc_span/src/lib.rs
+++ b/compiler/rustc_span/src/lib.rs
@@ -1071,9 +1071,17 @@ impl Span {
     ///
     /// If "self" is the span of the outer_ident, and "within" is the span of the `($ident,)`
     /// expr, then this will return the span of the `$ident` macro variable.
-    pub fn within_macro(self, within: Span) -> Option<Span> {
+    pub fn within_macro(self, within: Span, sm: &SourceMap) -> Option<Span> {
         match Span::prepare_to_combine(self, within) {
-            Ok((self_, _, parent)) if self_.lo != self.lo() && self.hi() != self_.hi => {
+            // Only return something if it doesn't overlap with the original span,
+            // and the span isn't "imported" (i.e. from unavailable sources).
+            // FIXME: This does limit the usefulness of the error when the macro is
+            // from a foreign crate; we could also take into account `-Zmacro-backtrace`,
+            // which doesn't redact this span (but that would mean passing in even more
+            // args to this function, lol).
+            Ok((self_, _, parent))
+                if self_.hi < self.lo() || self.hi() < self_.lo && !sm.is_imported(within) =>
+            {
                 Some(Span::new(self_.lo, self_.hi, self_.ctxt, parent))
             }
             _ => None,