Cache expansion hash.

This commit is contained in:
Camille GILLOT 2021-06-22 19:20:25 +02:00
parent cff0ea5f88
commit 616ce3c5c0
12 changed files with 263 additions and 217 deletions

View file

@ -39,7 +39,7 @@ use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind};
use rustc_hir::def::Namespace::*;
use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefPathHash, LocalDefId, CRATE_DEF_INDEX};
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
use rustc_hir::TraitCandidate;
use rustc_index::vec::IndexVec;
@ -54,7 +54,7 @@ use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
use rustc_session::Session;
use rustc_span::edition::Edition;
use rustc_span::hygiene::{ExpnId, ExpnKind, MacroKind, SyntaxContext, Transparency};
use rustc_span::source_map::Spanned;
use rustc_span::source_map::{CachingSourceMapView, Spanned};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};
@ -1149,6 +1149,13 @@ impl ResolverAstLowering for Resolver<'_> {
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node))
}
fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
match def_id.as_local() {
Some(def_id) => self.definitions.def_path_hash(def_id),
None => self.cstore().def_path_hash(def_id),
}
}
/// Adds a definition with a parent definition.
fn create_def(
&mut self,
@ -1192,6 +1199,32 @@ impl ResolverAstLowering for Resolver<'_> {
}
}
struct ExpandHasher<'a, 'b> {
source_map: CachingSourceMapView<'a>,
resolver: &'a Resolver<'b>,
}
impl<'a, 'b> rustc_span::HashStableContext for ExpandHasher<'a, 'b> {
#[inline]
fn hash_spans(&self) -> bool {
true
}
#[inline]
fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
self.resolver.def_path_hash(def_id)
}
#[inline]
fn span_data_to_lines_and_cols(
&mut self,
span: &rustc_span::SpanData,
) -> Option<(Lrc<rustc_span::SourceFile>, usize, rustc_span::BytePos, usize, rustc_span::BytePos)>
{
self.source_map.span_data_to_lines_and_cols(span)
}
}
impl<'a> Resolver<'a> {
pub fn new(
session: &'a Session,
@ -1364,6 +1397,13 @@ impl<'a> Resolver<'a> {
resolver
}
fn create_stable_hashing_context(&self) -> ExpandHasher<'_, 'a> {
ExpandHasher {
source_map: CachingSourceMapView::new(self.session.source_map()),
resolver: self,
}
}
pub fn next_node_id(&mut self) -> NodeId {
let next = self
.next_node_id

View file

@ -218,14 +218,17 @@ impl<'a> ResolverExpand for Resolver<'a> {
parent_module_id: Option<NodeId>,
) -> ExpnId {
let parent_module = parent_module_id.map(|module_id| self.local_def_id(module_id));
let expn_id = ExpnId::fresh(Some(ExpnData::allow_unstable(
ExpnKind::AstPass(pass),
call_site,
self.session.edition(),
features.into(),
None,
parent_module.map(LocalDefId::to_def_id),
)));
let expn_id = ExpnId::fresh(
ExpnData::allow_unstable(
ExpnKind::AstPass(pass),
call_site,
self.session.edition(),
features.into(),
None,
parent_module.map(LocalDefId::to_def_id),
),
self.create_stable_hashing_context(),
);
let parent_scope = parent_module
.map_or(self.empty_module, |parent_def_id| self.module_map[&parent_def_id]);
@ -287,15 +290,18 @@ impl<'a> ResolverExpand for Resolver<'a> {
)?;
let span = invoc.span();
invoc_id.set_expn_data(ext.expn_data(
parent_scope.expansion,
span,
fast_print_path(path),
res.opt_def_id(),
res.opt_def_id().map(|macro_def_id| {
self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod
}),
));
invoc_id.set_expn_data(
ext.expn_data(
parent_scope.expansion,
span,
fast_print_path(path),
res.opt_def_id(),
res.opt_def_id().map(|macro_def_id| {
self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod
}),
),
self.create_stable_hashing_context(),
);
if let Res::Def(_, _) = res {
// Gate macro attributes in `#[derive]` output.