]> git.lizzy.rs Git - rust.git/commitdiff
syntax: Make def-site span mandatory in ExpnInfo/MacroBacktrace/DiagnosticSpanMacroEx...
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Sun, 30 Jun 2019 00:05:52 +0000 (03:05 +0300)
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>
Wed, 10 Jul 2019 21:12:07 +0000 (00:12 +0300)
We have to deal with dummy spans anyway

Remove def-site span from expander interfaces.
It's not used by the expansion infra, only by specific expanders, which can keep it themselves if they want it.

12 files changed:
src/librustc/hir/lowering.rs
src/librustc/lint/mod.rs
src/librustc/traits/error_reporting.rs
src/librustc_errors/emitter.rs
src/librustc_save_analysis/lib.rs
src/libsyntax/ext/base.rs
src/libsyntax/ext/expand.rs
src/libsyntax/ext/tt/macro_rules.rs
src/libsyntax/json.rs
src/libsyntax_pos/hygiene.rs
src/libsyntax_pos/lib.rs
src/test/run-pass-fulldeps/auxiliary/plugin-args.rs

index 549c34d961aa4799924fc256fd1944d0a434d1d1..c44fd30be850a991af358c45de4d1a8124969347 100644 (file)
@@ -877,7 +877,7 @@ fn mark_span_with_reason(
     ) -> Span {
         let mark = Mark::fresh(Mark::root());
         mark.set_expn_info(ExpnInfo {
-            def_site: Some(span),
+            def_site: span,
             allow_internal_unstable,
             ..ExpnInfo::default(ExpnKind::Desugaring(reason), span, self.sess.edition())
         });
index 2bf4f1d3cfbba5a39d0398cf181f8af6b728838d..9876d6c1fa59b2943a69f9821bc5011b72fa9e6f 100644 (file)
@@ -888,13 +888,11 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
         ExpnKind::Desugaring(DesugaringKind::ForLoop) => false,
         ExpnKind::Desugaring(_) => true, // well, it's "external"
         ExpnKind::MacroBang(..) => {
-            let def_site = match info.def_site {
-                Some(span) => span,
-                // no span for the def_site means it's an external macro
-                None => return true,
-            };
-
-            match sess.source_map().span_to_snippet(def_site) {
+            if info.def_site.is_dummy() {
+                // dummy span for the def_site means it's an external macro
+                return true;
+            }
+            match sess.source_map().span_to_snippet(info.def_site) {
                 Ok(code) => !code.starts_with("macro_rules"),
                 // no snippet = external macro or compiler-builtin expansion
                 Err(_) => true,
index fb7ed1474450287d5f5c7266da17cc1971607d48..a7dfbd688c14e037a12af4d201325930f7a9b2a6 100644 (file)
@@ -61,12 +61,9 @@ struct ErrorDescriptor<'tcx> {
             // We want to ignore desugarings here: spans are equivalent even
             // if one is the result of a desugaring and the other is not.
             let mut span = error.obligation.cause.span;
-            if let Some(ExpnInfo {
-                kind: ExpnKind::Desugaring(_),
-                def_site: Some(def_span),
-                ..
-            }) = span.ctxt().outer_expn_info() {
-                span = def_span;
+            if let Some(ExpnInfo { kind: ExpnKind::Desugaring(_), def_site, .. })
+                    = span.ctxt().outer_expn_info() {
+                span = def_site;
             }
 
             error_map.entry(span).or_default().push(
index 83a0fb486fd9a34c728910a571f74c00b5ea790d..361b5cd9357125731da3fa9c75b42713ad511054 100644 (file)
@@ -723,39 +723,37 @@ fn fix_multispan_in_std_macros(&mut self,
                 for (i, trace) in sp.macro_backtrace().iter().rev().enumerate() {
                     // Only show macro locations that are local
                     // and display them like a span_note
-                    if let Some(def_site) = trace.def_site_span {
-                        if def_site.is_dummy() {
-                            continue;
-                        }
-                        if always_backtrace {
-                            new_labels.push((def_site,
-                                             format!("in this expansion of `{}`{}",
-                                                     trace.macro_decl_name,
-                                                     if backtrace_len > 2 {
-                                                         // if backtrace_len == 1 it'll be pointed
-                                                         // at by "in this macro invocation"
-                                                         format!(" (#{})", i + 1)
-                                                     } else {
-                                                         String::new()
-                                                     })));
-                        }
-                        // Check to make sure we're not in any <*macros>
-                        if !sm.span_to_filename(def_site).is_macros() &&
-                           !trace.macro_decl_name.starts_with("desugaring of ") &&
-                           !trace.macro_decl_name.starts_with("#[") ||
-                           always_backtrace {
-                            new_labels.push((trace.call_site,
-                                             format!("in this macro invocation{}",
-                                                     if backtrace_len > 2 && always_backtrace {
-                                                         // only specify order when the macro
-                                                         // backtrace is multiple levels deep
-                                                         format!(" (#{})", i + 1)
-                                                     } else {
-                                                         String::new()
-                                                     })));
-                            if !always_backtrace {
-                                break;
-                            }
+                    if trace.def_site_span.is_dummy() {
+                        continue;
+                    }
+                    if always_backtrace {
+                        new_labels.push((trace.def_site_span,
+                                            format!("in this expansion of `{}`{}",
+                                                    trace.macro_decl_name,
+                                                    if backtrace_len > 2 {
+                                                        // if backtrace_len == 1 it'll be pointed
+                                                        // at by "in this macro invocation"
+                                                        format!(" (#{})", i + 1)
+                                                    } else {
+                                                        String::new()
+                                                    })));
+                    }
+                    // Check to make sure we're not in any <*macros>
+                    if !sm.span_to_filename(trace.def_site_span).is_macros() &&
+                        !trace.macro_decl_name.starts_with("desugaring of ") &&
+                        !trace.macro_decl_name.starts_with("#[") ||
+                        always_backtrace {
+                        new_labels.push((trace.call_site,
+                                            format!("in this macro invocation{}",
+                                                    if backtrace_len > 2 && always_backtrace {
+                                                        // only specify order when the macro
+                                                        // backtrace is multiple levels deep
+                                                        format!(" (#{})", i + 1)
+                                                    } else {
+                                                        String::new()
+                                                    })));
+                        if !always_backtrace {
+                            break;
                         }
                     }
                 }
index fab2537f9d8f181bbdf4bfdecff7a63a7c8c07ed..aeaee1887b95b5b2192c6f5cafce8883a49216b7 100644 (file)
@@ -841,7 +841,6 @@ pub fn get_macro_use_data(&self, span: Span) -> Option<MacroRef> {
         let callsite = span.source_callsite();
         let callsite_span = self.span_from_span(callsite);
         let callee = span.source_callee()?;
-        let callee_span = callee.def_site?;
 
         // Ignore attribute macros, their spans are usually mangled
         if let ExpnKind::MacroAttribute(_) = callee.kind {
@@ -855,7 +854,7 @@ pub fn get_macro_use_data(&self, span: Span) -> Option<MacroRef> {
             .sess
             .imported_macro_spans
             .borrow()
-            .get(&callee_span)
+            .get(&callee.def_site)
         {
             let &(ref mac_name, mac_span) = mac;
             let mac_span = self.span_from_span(mac_span);
@@ -866,7 +865,7 @@ pub fn get_macro_use_data(&self, span: Span) -> Option<MacroRef> {
             });
         }
 
-        let callee_span = self.span_from_span(callee_span);
+        let callee_span = self.span_from_span(callee.def_site);
         Some(MacroRef {
             span: callsite_span,
             qualname: callee.kind.descr().to_string(), // FIXME: generate the real qualname
index 267046655ffdc4a2301eca844bc5790f13f5ddda..2f8d6f00ba79262487b411c1e248f1932d2c49d1 100644 (file)
@@ -219,7 +219,6 @@ fn expand<'cx>(
         ecx: &'cx mut ExtCtxt<'_>,
         span: Span,
         input: TokenStream,
-        def_span: Option<Span>,
     ) -> Box<dyn MacResult+'cx>;
 }
 
@@ -236,7 +235,6 @@ fn expand<'cx>(
         ecx: &'cx mut ExtCtxt<'_>,
         span: Span,
         input: TokenStream,
-        _def_span: Option<Span>,
     ) -> Box<dyn MacResult+'cx> {
         struct AvoidInterpolatedIdents;
 
@@ -654,7 +652,7 @@ pub fn expn_info(&self, call_site: Span, descr: &str) -> ExpnInfo {
         ExpnInfo {
             call_site,
             kind: self.expn_kind(Symbol::intern(descr)),
-            def_site: Some(self.span),
+            def_site: self.span,
             default_transparency: self.default_transparency,
             allow_internal_unstable: self.allow_internal_unstable.clone(),
             allow_internal_unsafe: self.allow_internal_unsafe,
index bb7d7352e055af821e7fedac52620e641e6a1e86..06ff2bc655cf5ea5d9a2a2471e50b3d41e073990 100644 (file)
@@ -673,7 +673,7 @@ fn expand_bang_invoc(&mut self,
                 result
             }
             SyntaxExtensionKind::LegacyBang(expander) => {
-                let tok_result = expander.expand(self.cx, span, mac.node.stream(), Some(ext.span));
+                let tok_result = expander.expand(self.cx, span, mac.node.stream());
                 kind.make_from(tok_result)
             }
 
index 665c794422d49cc51433835a3d163d47fba09135..5c6438a7ef5343e0e8ff91cbb51ea8b926ffd1cf 100644 (file)
@@ -88,6 +88,7 @@ pub fn make(mut self: Box<ParserAnyMacro<'a>>, kind: AstFragmentKind) -> AstFrag
 
 struct MacroRulesMacroExpander {
     name: ast::Ident,
+    span: Span,
     lhses: Vec<quoted::TokenTree>,
     rhses: Vec<quoted::TokenTree>,
     valid: bool,
@@ -99,12 +100,11 @@ fn expand<'cx>(
         cx: &'cx mut ExtCtxt<'_>,
         sp: Span,
         input: TokenStream,
-        def_span: Option<Span>,
     ) -> Box<dyn MacResult + 'cx> {
         if !self.valid {
             return DummyResult::any(sp);
         }
-        generic_extension(cx, sp, def_span, self.name, input, &self.lhses, &self.rhses)
+        generic_extension(cx, sp, self.span, self.name, input, &self.lhses, &self.rhses)
     }
 }
 
@@ -117,7 +117,7 @@ fn trace_macros_note(cx: &mut ExtCtxt<'_>, sp: Span, message: String) {
 fn generic_extension<'cx>(
     cx: &'cx mut ExtCtxt<'_>,
     sp: Span,
-    def_span: Option<Span>,
+    def_span: Span,
     name: ast::Ident,
     arg: TokenStream,
     lhses: &[quoted::TokenTree],
@@ -199,10 +199,8 @@ fn generic_extension<'cx>(
     let span = token.span.substitute_dummy(sp);
     let mut err = cx.struct_span_err(span, &parse_failure_msg(&token));
     err.span_label(span, label);
-    if let Some(sp) = def_span {
-        if cx.source_map().span_to_filename(sp).is_real() && !sp.is_dummy() {
-            err.span_label(cx.source_map().def_span(sp), "when calling this macro");
-        }
+    if !def_span.is_dummy() && cx.source_map().span_to_filename(def_span).is_real() {
+        err.span_label(cx.source_map().def_span(def_span), "when calling this macro");
     }
 
     // Check whether there's a missing comma in this macro call, like `println!("{}" a);`
@@ -377,7 +375,7 @@ pub fn compile(
     }
 
     let expander: Box<_> =
-        Box::new(MacroRulesMacroExpander { name: def.ident, lhses, rhses, valid });
+        Box::new(MacroRulesMacroExpander { name: def.ident, span: def.span, lhses, rhses, valid });
 
     let (default_transparency, transparency_error) =
         attr::find_transparency(&def.attrs, body.legacy);
index 767ab74355e66213ff8c27fbec2ba22be41f7302..ec0222d90eb7a2430cc71fdd284b6f0f88806f0e 100644 (file)
@@ -170,7 +170,7 @@ struct DiagnosticSpanMacroExpansion {
     macro_decl_name: String,
 
     /// span where macro was defined (if known)
-    def_site_span: Option<DiagnosticSpan>,
+    def_site_span: DiagnosticSpan,
 }
 
 #[derive(RustcEncodable)]
@@ -300,14 +300,13 @@ fn from_span_full(span: Span,
                                      None,
                                      backtrace,
                                      je);
-            let def_site_span = bt.def_site_span.map(|sp| {
-                Self::from_span_full(sp,
+            let def_site_span =
+                Self::from_span_full(bt.def_site_span,
                                      false,
                                      None,
                                      None,
                                      vec![].into_iter(),
-                                     je)
-            });
+                                     je);
             Box::new(DiagnosticSpanMacroExpansion {
                 span: call_site,
                 macro_decl_name: bt.macro_decl_name,
index f8318f54320e30ca7b70c6e1b00d6babd5c72014..63b692ccdcb933e469284021ede52636d37ad0c6 100644 (file)
@@ -26,7 +26,7 @@
 // trigger runtime aborts. (Fortunately these are obvious and easy to fix.)
 
 use crate::GLOBALS;
-use crate::Span;
+use crate::{Span, DUMMY_SP};
 use crate::edition::Edition;
 use crate::symbol::{kw, Symbol};
 
@@ -632,11 +632,9 @@ pub struct ExpnInfo {
 
     // --- The part specific to the macro/desugaring definition.
     // --- FIXME: Share it between expansions with the same definition.
-    /// The span of the macro definition itself. The macro may not
-    /// have a sensible definition span (e.g., something defined
-    /// completely inside libsyntax) in which case this is None.
+    /// The span of the macro definition (possibly dummy).
     /// This span serves only informational purpose and is not used for resolution.
-    pub def_site: Option<Span>,
+    pub def_site: Span,
     /// Transparency used by `apply_mark` for mark with this expansion info by default.
     pub default_transparency: Transparency,
     /// List of #[unstable]/feature-gated features that the macro is allowed to use
@@ -659,7 +657,7 @@ pub fn default(kind: ExpnKind, call_site: Span, edition: Edition) -> ExpnInfo {
         ExpnInfo {
             call_site,
             kind,
-            def_site: None,
+            def_site: DUMMY_SP,
             default_transparency: Transparency::SemiTransparent,
             allow_internal_unstable: None,
             allow_internal_unsafe: false,
index d4c1958f7e265098f19ffe30849b75a2ffd2753d..1369fca3b4a5e002cc15b75aa1ad489b3a541ed7 100644 (file)
@@ -1363,8 +1363,8 @@ pub struct MacroBacktrace {
     /// name of macro that was applied (e.g., "foo!" or "#[derive(Eq)]")
     pub macro_decl_name: String,
 
-    /// span where macro was defined (if known)
-    pub def_site_span: Option<Span>,
+    /// span where macro was defined (possibly dummy)
+    pub def_site_span: Span,
 }
 
 // _____________________________________________________________________________
index 7cbfef52b4aad4de7f4e0671f2229e508cd501c8..36cee82893a06066e2440f0eddad40eb8f5fdef6 100644 (file)
@@ -28,8 +28,7 @@ impl TTMacroExpander for Expander {
     fn expand<'cx>(&self,
                    ecx: &'cx mut ExtCtxt,
                    sp: Span,
-                   _: TokenStream,
-                   _: Option<Span>) -> Box<dyn MacResult+'cx> {
+                   _: TokenStream) -> Box<dyn MacResult+'cx> {
         let args = self.args.iter().map(|i| pprust::meta_list_item_to_string(i))
             .collect::<Vec<_>>().join(", ");
         MacEager::expr(ecx.expr_str(sp, Symbol::intern(&args)))