) -> 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())
});
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,
// 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(
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;
}
}
}
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 {
.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);
});
}
- 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
ecx: &'cx mut ExtCtxt<'_>,
span: Span,
input: TokenStream,
- def_span: Option<Span>,
) -> Box<dyn MacResult+'cx>;
}
ecx: &'cx mut ExtCtxt<'_>,
span: Span,
input: TokenStream,
- _def_span: Option<Span>,
) -> Box<dyn MacResult+'cx> {
struct AvoidInterpolatedIdents;
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,
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)
}
struct MacroRulesMacroExpander {
name: ast::Ident,
+ span: Span,
lhses: Vec<quoted::TokenTree>,
rhses: Vec<quoted::TokenTree>,
valid: bool,
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)
}
}
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],
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);`
}
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);
macro_decl_name: String,
/// span where macro was defined (if known)
- def_site_span: Option<DiagnosticSpan>,
+ def_site_span: DiagnosticSpan,
}
#[derive(RustcEncodable)]
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,
// 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};
// --- 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
ExpnInfo {
call_site,
kind,
- def_site: None,
+ def_site: DUMMY_SP,
default_transparency: Transparency::SemiTransparent,
allow_internal_unstable: None,
allow_internal_unsafe: false,
/// 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,
}
// _____________________________________________________________________________
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)))