Skip to content

Commit

Permalink
Record call_site parent for macros.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Sep 10, 2021
1 parent 5e026ea commit 2e37ed8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_expand/src/base.rs
Expand Up @@ -15,7 +15,7 @@ use rustc_lint_defs::builtin::PROC_MACRO_BACK_COMPAT;
use rustc_lint_defs::BuiltinLintDiagnostics;
use rustc_parse::{self, nt_to_tokenstream, parser, MACRO_ARGUMENTS};
use rustc_session::{parse::ParseSess, Limit, Session};
use rustc_span::def_id::{CrateNum, DefId};
use rustc_span::def_id::{CrateNum, DefId, LocalDefId};
use rustc_span::edition::Edition;
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId};
use rustc_span::source_map::SourceMap;
Expand Down Expand Up @@ -843,6 +843,7 @@ pub type DeriveResolutions = Vec<(ast::Path, Annotatable, Option<Lrc<SyntaxExten

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

fn resolve_dollar_crates(&mut self);
fn visit_ast_fragment_with_placeholders(
Expand Down
15 changes: 14 additions & 1 deletion compiler/rustc_expand/src/expand.rs
Expand Up @@ -588,7 +588,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
// Resolve `$crate`s in the fragment for pretty-printing.
self.cx.resolver.resolve_dollar_crates();

let invocations = {
let mut invocations = {
let mut collector = InvocationCollector {
// Non-derive macro invocations cannot see the results of cfg expansion - they
// will either be removed along with the item, or invoked before the cfg/cfg_attr
Expand All @@ -613,6 +613,19 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
self.cx
.resolver
.visit_ast_fragment_with_placeholders(self.cx.current_expansion.id, &fragment);

if self.cx.sess.opts.debugging_opts.incremental_relative_spans {
for (invoc, _) in invocations.iter_mut() {
let expn_id = invoc.expansion_data.id;
let parent_def = self.cx.resolver.invocation_parent(expn_id);
let span = match &mut invoc.kind {
InvocationKind::Bang { ref mut span, .. } => span,
InvocationKind::Attr { attr, .. } => &mut attr.span,
InvocationKind::Derive { path, .. } => &mut path.span,
};
*span = span.with_parent(Some(parent_def));
}
}
}

(fragment, invocations)
Expand Down
8 changes: 7 additions & 1 deletion compiler/rustc_resolve/src/def_collector.rs
Expand Up @@ -32,7 +32,13 @@ impl<'a, 'b> DefCollector<'a, 'b> {
fn create_def(&mut self, node_id: NodeId, data: DefPathData, span: Span) -> LocalDefId {
let parent_def = self.parent_def;
debug!("create_def(node_id={:?}, data={:?}, parent_def={:?})", node_id, data, parent_def);
self.resolver.create_def(parent_def, node_id, data, self.expansion.to_expn_id(), span)
self.resolver.create_def(
parent_def,
node_id,
data,
self.expansion.to_expn_id(),
span.with_parent(None),
)
}

fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_def: LocalDefId, f: F) {
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_resolve/src/macros.rs
Expand Up @@ -180,6 +180,10 @@ impl<'a> ResolverExpand for Resolver<'a> {
self.next_node_id()
}

fn invocation_parent(&self, id: LocalExpnId) -> LocalDefId {
self.invocation_parents[&id].0
}

fn resolve_dollar_crates(&mut self) {
hygiene::update_dollar_crate_names(|ctxt| {
let ident = Ident::new(kw::DollarCrate, DUMMY_SP.with_ctxt(ctxt));
Expand Down

0 comments on commit 2e37ed8

Please sign in to comment.