Skip to content

Commit 3eafaae

Browse files
committed
syntax: Make def-site span mandatory in ExpnInfo/MacroBacktrace/DiagnosticSpanMacroExpansion
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.
1 parent a138e9d commit 3eafaae

File tree

12 files changed

+61
-77
lines changed

12 files changed

+61
-77
lines changed

src/librustc/hir/lowering.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ impl<'a> LoweringContext<'a> {
877877
) -> Span {
878878
let mark = Mark::fresh(Mark::root());
879879
mark.set_expn_info(ExpnInfo {
880-
def_site: Some(span),
880+
def_site: span,
881881
allow_internal_unstable,
882882
..ExpnInfo::default(ExpnKind::Desugaring(reason), span, self.sess.edition())
883883
});

src/librustc/lint/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -888,13 +888,11 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool {
888888
ExpnKind::Desugaring(DesugaringKind::ForLoop) => false,
889889
ExpnKind::Desugaring(_) => true, // well, it's "external"
890890
ExpnKind::MacroBang(..) => {
891-
let def_site = match info.def_site {
892-
Some(span) => span,
893-
// no span for the def_site means it's an external macro
894-
None => return true,
895-
};
896-
897-
match sess.source_map().span_to_snippet(def_site) {
891+
if info.def_site.is_dummy() {
892+
// dummy span for the def_site means it's an external macro
893+
return true;
894+
}
895+
match sess.source_map().span_to_snippet(info.def_site) {
898896
Ok(code) => !code.starts_with("macro_rules"),
899897
// no snippet = external macro or compiler-builtin expansion
900898
Err(_) => true,

src/librustc/traits/error_reporting.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,9 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
6161
// We want to ignore desugarings here: spans are equivalent even
6262
// if one is the result of a desugaring and the other is not.
6363
let mut span = error.obligation.cause.span;
64-
if let Some(ExpnInfo {
65-
kind: ExpnKind::Desugaring(_),
66-
def_site: Some(def_span),
67-
..
68-
}) = span.ctxt().outer_expn_info() {
69-
span = def_span;
64+
if let Some(ExpnInfo { kind: ExpnKind::Desugaring(_), def_site, .. })
65+
= span.ctxt().outer_expn_info() {
66+
span = def_site;
7067
}
7168

7269
error_map.entry(span).or_default().push(

src/librustc_errors/emitter.rs

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -723,39 +723,37 @@ impl EmitterWriter {
723723
for (i, trace) in sp.macro_backtrace().iter().rev().enumerate() {
724724
// Only show macro locations that are local
725725
// and display them like a span_note
726-
if let Some(def_site) = trace.def_site_span {
727-
if def_site.is_dummy() {
728-
continue;
729-
}
730-
if always_backtrace {
731-
new_labels.push((def_site,
732-
format!("in this expansion of `{}`{}",
733-
trace.macro_decl_name,
734-
if backtrace_len > 2 {
735-
// if backtrace_len == 1 it'll be pointed
736-
// at by "in this macro invocation"
737-
format!(" (#{})", i + 1)
738-
} else {
739-
String::new()
740-
})));
741-
}
742-
// Check to make sure we're not in any <*macros>
743-
if !sm.span_to_filename(def_site).is_macros() &&
744-
!trace.macro_decl_name.starts_with("desugaring of ") &&
745-
!trace.macro_decl_name.starts_with("#[") ||
746-
always_backtrace {
747-
new_labels.push((trace.call_site,
748-
format!("in this macro invocation{}",
749-
if backtrace_len > 2 && always_backtrace {
750-
// only specify order when the macro
751-
// backtrace is multiple levels deep
752-
format!(" (#{})", i + 1)
753-
} else {
754-
String::new()
755-
})));
756-
if !always_backtrace {
757-
break;
758-
}
726+
if trace.def_site_span.is_dummy() {
727+
continue;
728+
}
729+
if always_backtrace {
730+
new_labels.push((trace.def_site_span,
731+
format!("in this expansion of `{}`{}",
732+
trace.macro_decl_name,
733+
if backtrace_len > 2 {
734+
// if backtrace_len == 1 it'll be pointed
735+
// at by "in this macro invocation"
736+
format!(" (#{})", i + 1)
737+
} else {
738+
String::new()
739+
})));
740+
}
741+
// Check to make sure we're not in any <*macros>
742+
if !sm.span_to_filename(trace.def_site_span).is_macros() &&
743+
!trace.macro_decl_name.starts_with("desugaring of ") &&
744+
!trace.macro_decl_name.starts_with("#[") ||
745+
always_backtrace {
746+
new_labels.push((trace.call_site,
747+
format!("in this macro invocation{}",
748+
if backtrace_len > 2 && always_backtrace {
749+
// only specify order when the macro
750+
// backtrace is multiple levels deep
751+
format!(" (#{})", i + 1)
752+
} else {
753+
String::new()
754+
})));
755+
if !always_backtrace {
756+
break;
759757
}
760758
}
761759
}

src/librustc_save_analysis/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,6 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
841841
let callsite = span.source_callsite();
842842
let callsite_span = self.span_from_span(callsite);
843843
let callee = span.source_callee()?;
844-
let callee_span = callee.def_site?;
845844

846845
// Ignore attribute macros, their spans are usually mangled
847846
if let ExpnKind::MacroAttribute(_) = callee.kind {
@@ -855,7 +854,7 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
855854
.sess
856855
.imported_macro_spans
857856
.borrow()
858-
.get(&callee_span)
857+
.get(&callee.def_site)
859858
{
860859
let &(ref mac_name, mac_span) = mac;
861860
let mac_span = self.span_from_span(mac_span);
@@ -866,7 +865,7 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
866865
});
867866
}
868867

869-
let callee_span = self.span_from_span(callee_span);
868+
let callee_span = self.span_from_span(callee.def_site);
870869
Some(MacroRef {
871870
span: callsite_span,
872871
qualname: callee.kind.descr().to_string(), // FIXME: generate the real qualname

src/libsyntax/ext/base.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,6 @@ pub trait TTMacroExpander {
219219
ecx: &'cx mut ExtCtxt<'_>,
220220
span: Span,
221221
input: TokenStream,
222-
def_span: Option<Span>,
223222
) -> Box<dyn MacResult+'cx>;
224223
}
225224

@@ -236,7 +235,6 @@ impl<F> TTMacroExpander for F
236235
ecx: &'cx mut ExtCtxt<'_>,
237236
span: Span,
238237
input: TokenStream,
239-
_def_span: Option<Span>,
240238
) -> Box<dyn MacResult+'cx> {
241239
struct AvoidInterpolatedIdents;
242240

@@ -654,7 +652,7 @@ impl SyntaxExtension {
654652
ExpnInfo {
655653
call_site,
656654
kind: self.expn_kind(Symbol::intern(descr)),
657-
def_site: Some(self.span),
655+
def_site: self.span,
658656
default_transparency: self.default_transparency,
659657
allow_internal_unstable: self.allow_internal_unstable.clone(),
660658
allow_internal_unsafe: self.allow_internal_unsafe,

src/libsyntax/ext/expand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
673673
result
674674
}
675675
SyntaxExtensionKind::LegacyBang(expander) => {
676-
let tok_result = expander.expand(self.cx, span, mac.node.stream(), Some(ext.span));
676+
let tok_result = expander.expand(self.cx, span, mac.node.stream());
677677
kind.make_from(tok_result)
678678
}
679679

src/libsyntax/ext/tt/macro_rules.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl<'a> ParserAnyMacro<'a> {
8888

8989
struct MacroRulesMacroExpander {
9090
name: ast::Ident,
91+
span: Span,
9192
lhses: Vec<quoted::TokenTree>,
9293
rhses: Vec<quoted::TokenTree>,
9394
valid: bool,
@@ -99,12 +100,11 @@ impl TTMacroExpander for MacroRulesMacroExpander {
99100
cx: &'cx mut ExtCtxt<'_>,
100101
sp: Span,
101102
input: TokenStream,
102-
def_span: Option<Span>,
103103
) -> Box<dyn MacResult + 'cx> {
104104
if !self.valid {
105105
return DummyResult::any(sp);
106106
}
107-
generic_extension(cx, sp, def_span, self.name, input, &self.lhses, &self.rhses)
107+
generic_extension(cx, sp, self.span, self.name, input, &self.lhses, &self.rhses)
108108
}
109109
}
110110

@@ -117,7 +117,7 @@ fn trace_macros_note(cx: &mut ExtCtxt<'_>, sp: Span, message: String) {
117117
fn generic_extension<'cx>(
118118
cx: &'cx mut ExtCtxt<'_>,
119119
sp: Span,
120-
def_span: Option<Span>,
120+
def_span: Span,
121121
name: ast::Ident,
122122
arg: TokenStream,
123123
lhses: &[quoted::TokenTree],
@@ -199,10 +199,8 @@ fn generic_extension<'cx>(
199199
let span = token.span.substitute_dummy(sp);
200200
let mut err = cx.struct_span_err(span, &parse_failure_msg(&token));
201201
err.span_label(span, label);
202-
if let Some(sp) = def_span {
203-
if cx.source_map().span_to_filename(sp).is_real() && !sp.is_dummy() {
204-
err.span_label(cx.source_map().def_span(sp), "when calling this macro");
205-
}
202+
if !def_span.is_dummy() && cx.source_map().span_to_filename(def_span).is_real() {
203+
err.span_label(cx.source_map().def_span(def_span), "when calling this macro");
206204
}
207205

208206
// Check whether there's a missing comma in this macro call, like `println!("{}" a);`
@@ -377,7 +375,7 @@ pub fn compile(
377375
}
378376

379377
let expander: Box<_> =
380-
Box::new(MacroRulesMacroExpander { name: def.ident, lhses, rhses, valid });
378+
Box::new(MacroRulesMacroExpander { name: def.ident, span: def.span, lhses, rhses, valid });
381379

382380
let (default_transparency, transparency_error) =
383381
attr::find_transparency(&def.attrs, body.legacy);

src/libsyntax/json.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ struct DiagnosticSpanMacroExpansion {
170170
macro_decl_name: String,
171171

172172
/// span where macro was defined (if known)
173-
def_site_span: Option<DiagnosticSpan>,
173+
def_site_span: DiagnosticSpan,
174174
}
175175

176176
#[derive(RustcEncodable)]
@@ -300,14 +300,13 @@ impl DiagnosticSpan {
300300
None,
301301
backtrace,
302302
je);
303-
let def_site_span = bt.def_site_span.map(|sp| {
304-
Self::from_span_full(sp,
303+
let def_site_span =
304+
Self::from_span_full(bt.def_site_span,
305305
false,
306306
None,
307307
None,
308308
vec![].into_iter(),
309-
je)
310-
});
309+
je);
311310
Box::new(DiagnosticSpanMacroExpansion {
312311
span: call_site,
313312
macro_decl_name: bt.macro_decl_name,

src/libsyntax_pos/hygiene.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
// trigger runtime aborts. (Fortunately these are obvious and easy to fix.)
2727

2828
use crate::GLOBALS;
29-
use crate::Span;
29+
use crate::{Span, DUMMY_SP};
3030
use crate::edition::Edition;
3131
use crate::symbol::{kw, Symbol};
3232

@@ -632,11 +632,9 @@ pub struct ExpnInfo {
632632

633633
// --- The part specific to the macro/desugaring definition.
634634
// --- FIXME: Share it between expansions with the same definition.
635-
/// The span of the macro definition itself. The macro may not
636-
/// have a sensible definition span (e.g., something defined
637-
/// completely inside libsyntax) in which case this is None.
635+
/// The span of the macro definition (possibly dummy).
638636
/// This span serves only informational purpose and is not used for resolution.
639-
pub def_site: Option<Span>,
637+
pub def_site: Span,
640638
/// Transparency used by `apply_mark` for mark with this expansion info by default.
641639
pub default_transparency: Transparency,
642640
/// List of #[unstable]/feature-gated features that the macro is allowed to use
@@ -659,7 +657,7 @@ impl ExpnInfo {
659657
ExpnInfo {
660658
call_site,
661659
kind,
662-
def_site: None,
660+
def_site: DUMMY_SP,
663661
default_transparency: Transparency::SemiTransparent,
664662
allow_internal_unstable: None,
665663
allow_internal_unsafe: false,

0 commit comments

Comments
 (0)