Skip to content

Commit 2e37ed8

Browse files
committed
Record call_site parent for macros.
1 parent 5e026ea commit 2e37ed8

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

compiler/rustc_expand/src/base.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_lint_defs::builtin::PROC_MACRO_BACK_COMPAT;
1515
use rustc_lint_defs::BuiltinLintDiagnostics;
1616
use rustc_parse::{self, nt_to_tokenstream, parser, MACRO_ARGUMENTS};
1717
use rustc_session::{parse::ParseSess, Limit, Session};
18-
use rustc_span::def_id::{CrateNum, DefId};
18+
use rustc_span::def_id::{CrateNum, DefId, LocalDefId};
1919
use rustc_span::edition::Edition;
2020
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId};
2121
use rustc_span::source_map::SourceMap;
@@ -843,6 +843,7 @@ pub type DeriveResolutions = Vec<(ast::Path, Annotatable, Option<Lrc<SyntaxExten
843843

844844
pub trait ResolverExpand {
845845
fn next_node_id(&mut self) -> NodeId;
846+
fn invocation_parent(&self, id: LocalExpnId) -> LocalDefId;
846847

847848
fn resolve_dollar_crates(&mut self);
848849
fn visit_ast_fragment_with_placeholders(

compiler/rustc_expand/src/expand.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
588588
// Resolve `$crate`s in the fragment for pretty-printing.
589589
self.cx.resolver.resolve_dollar_crates();
590590

591-
let invocations = {
591+
let mut invocations = {
592592
let mut collector = InvocationCollector {
593593
// Non-derive macro invocations cannot see the results of cfg expansion - they
594594
// will either be removed along with the item, or invoked before the cfg/cfg_attr
@@ -613,6 +613,19 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
613613
self.cx
614614
.resolver
615615
.visit_ast_fragment_with_placeholders(self.cx.current_expansion.id, &fragment);
616+
617+
if self.cx.sess.opts.debugging_opts.incremental_relative_spans {
618+
for (invoc, _) in invocations.iter_mut() {
619+
let expn_id = invoc.expansion_data.id;
620+
let parent_def = self.cx.resolver.invocation_parent(expn_id);
621+
let span = match &mut invoc.kind {
622+
InvocationKind::Bang { ref mut span, .. } => span,
623+
InvocationKind::Attr { attr, .. } => &mut attr.span,
624+
InvocationKind::Derive { path, .. } => &mut path.span,
625+
};
626+
*span = span.with_parent(Some(parent_def));
627+
}
628+
}
616629
}
617630

618631
(fragment, invocations)

compiler/rustc_resolve/src/def_collector.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ impl<'a, 'b> DefCollector<'a, 'b> {
3232
fn create_def(&mut self, node_id: NodeId, data: DefPathData, span: Span) -> LocalDefId {
3333
let parent_def = self.parent_def;
3434
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
35-
self.resolver.create_def(parent_def, node_id, data, self.expansion.to_expn_id(), span)
35+
self.resolver.create_def(
36+
parent_def,
37+
node_id,
38+
data,
39+
self.expansion.to_expn_id(),
40+
span.with_parent(None),
41+
)
3642
}
3743

3844
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: LocalDefId, f: F) {

compiler/rustc_resolve/src/macros.rs

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ impl<'a> ResolverExpand for Resolver<'a> {
180180
self.next_node_id()
181181
}
182182

183+
fn invocation_parent(&self, id: LocalExpnId) -> LocalDefId {
184+
self.invocation_parents[&id].0
185+
}
186+
183187
fn resolve_dollar_crates(&mut self) {
184188
hygiene::update_dollar_crate_names(|ctxt| {
185189
let ident = Ident::new(kw::DollarCrate, DUMMY_SP.with_ctxt(ctxt));

0 commit comments

Comments
 (0)