Skip to content

Commit 616ce3c

Browse files
committed
Cache expansion hash.
1 parent cff0ea5 commit 616ce3c

File tree

12 files changed

+263
-217
lines changed

12 files changed

+263
-217
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+43-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use rustc_data_structures::sync::Lrc;
4848
use rustc_errors::{struct_span_err, Applicability};
4949
use rustc_hir as hir;
5050
use rustc_hir::def::{DefKind, Namespace, PartialRes, PerNS, Res};
51-
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, CRATE_DEF_ID};
51+
use rustc_hir::def_id::{DefId, DefIdMap, DefPathHash, LocalDefId, CRATE_DEF_ID};
5252
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
5353
use rustc_hir::intravisit;
5454
use rustc_hir::{ConstArg, GenericArg, ParamName};
@@ -59,7 +59,7 @@ use rustc_session::utils::{FlattenNonterminals, NtToTokenstream};
5959
use rustc_session::Session;
6060
use rustc_span::edition::Edition;
6161
use rustc_span::hygiene::ExpnId;
62-
use rustc_span::source_map::{respan, DesugaringKind};
62+
use rustc_span::source_map::{respan, CachingSourceMapView, DesugaringKind};
6363
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6464
use rustc_span::{Span, DUMMY_SP};
6565
use rustc_target::spec::abi::Abi;
@@ -204,6 +204,8 @@ pub trait ResolverAstLowering {
204204

205205
fn local_def_id(&self, node: NodeId) -> LocalDefId;
206206

207+
fn def_path_hash(&self, def_id: DefId) -> DefPathHash;
208+
207209
fn create_def(
208210
&mut self,
209211
parent: LocalDefId,
@@ -214,6 +216,32 @@ pub trait ResolverAstLowering {
214216
) -> LocalDefId;
215217
}
216218

219+
struct LoweringHasher<'a> {
220+
source_map: CachingSourceMapView<'a>,
221+
resolver: &'a dyn ResolverAstLowering,
222+
}
223+
224+
impl<'a> rustc_span::HashStableContext for LoweringHasher<'a> {
225+
#[inline]
226+
fn hash_spans(&self) -> bool {
227+
true
228+
}
229+
230+
#[inline]
231+
fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
232+
self.resolver.def_path_hash(def_id)
233+
}
234+
235+
#[inline]
236+
fn span_data_to_lines_and_cols(
237+
&mut self,
238+
span: &rustc_span::SpanData,
239+
) -> Option<(Lrc<rustc_span::SourceFile>, usize, rustc_span::BytePos, usize, rustc_span::BytePos)>
240+
{
241+
self.source_map.span_data_to_lines_and_cols(span)
242+
}
243+
}
244+
217245
/// Context of `impl Trait` in code, which determines whether it is allowed in an HIR subtree,
218246
/// and if so, what meaning it has.
219247
#[derive(Debug)]
@@ -565,6 +593,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
565593
lowered
566594
}
567595

596+
fn create_stable_hashing_context(&self) -> LoweringHasher<'_> {
597+
LoweringHasher {
598+
source_map: CachingSourceMapView::new(self.sess.source_map()),
599+
resolver: self.resolver,
600+
}
601+
}
602+
568603
fn lower_node_id_generic(
569604
&mut self,
570605
ast_node_id: NodeId,
@@ -684,7 +719,12 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
684719
span: Span,
685720
allow_internal_unstable: Option<Lrc<[Symbol]>>,
686721
) -> Span {
687-
span.mark_with_reason(allow_internal_unstable, reason, self.sess.edition())
722+
span.mark_with_reason(
723+
allow_internal_unstable,
724+
reason,
725+
self.sess.edition(),
726+
self.create_stable_hashing_context(),
727+
)
688728
}
689729

690730
fn with_anonymous_lifetime_mode<R>(

compiler/rustc_expand/src/expand.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
506506
.map(|(path, item, _exts)| {
507507
// FIXME: Consider using the derive resolutions (`_exts`)
508508
// instead of enqueuing the derives to be resolved again later.
509-
let expn_id = ExpnId::fresh(None);
509+
let expn_id = ExpnId::fresh_empty();
510510
derive_invocations.push((
511511
Invocation {
512512
kind: InvocationKind::Derive { path, item },
@@ -989,7 +989,7 @@ struct InvocationCollector<'a, 'b> {
989989

990990
impl<'a, 'b> InvocationCollector<'a, 'b> {
991991
fn collect(&mut self, fragment_kind: AstFragmentKind, kind: InvocationKind) -> AstFragment {
992-
let expn_id = ExpnId::fresh(None);
992+
let expn_id = ExpnId::fresh_empty();
993993
let vis = kind.placeholder_visibility();
994994
self.invocations.push((
995995
Invocation {

compiler/rustc_metadata/src/rmeta/decoder.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -393,12 +393,19 @@ impl<'a, 'tcx> Decodable<DecodeContext<'a, 'tcx>> for ExpnId {
393393
} else {
394394
local_cdata.cstore.get_crate_data(cnum)
395395
};
396-
Ok(crate_data
396+
let expn_data = crate_data
397397
.root
398398
.expn_data
399399
.get(&crate_data, index)
400400
.unwrap()
401-
.decode((&crate_data, sess)))
401+
.decode((&crate_data, sess));
402+
let expn_hash = crate_data
403+
.root
404+
.expn_hashes
405+
.get(&crate_data, index)
406+
.unwrap()
407+
.decode((&crate_data, sess));
408+
Ok((expn_data, expn_hash))
402409
},
403410
)
404411
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
653653
// Therefore, we need to encode the hygiene data last to ensure that we encode
654654
// any `SyntaxContext`s that might be used.
655655
i = self.position();
656-
let (syntax_contexts, expn_data) = self.encode_hygiene();
656+
let (syntax_contexts, expn_data, expn_hashes) = self.encode_hygiene();
657657
let hygiene_bytes = self.position() - i;
658658

659659
// Encode source_map. This needs to be done last,
@@ -701,6 +701,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
701701
tables,
702702
syntax_contexts,
703703
expn_data,
704+
expn_hashes,
704705
});
705706

706707
let total_bytes = self.position();
@@ -1578,23 +1579,29 @@ impl EncodeContext<'a, 'tcx> {
15781579
self.lazy(foreign_modules.iter().map(|(_, m)| m).cloned())
15791580
}
15801581

1581-
fn encode_hygiene(&mut self) -> (SyntaxContextTable, ExpnDataTable) {
1582+
fn encode_hygiene(&mut self) -> (SyntaxContextTable, ExpnDataTable, ExpnHashTable) {
15821583
let mut syntax_contexts: TableBuilder<_, _> = Default::default();
15831584
let mut expn_data_table: TableBuilder<_, _> = Default::default();
1585+
let mut expn_hash_table: TableBuilder<_, _> = Default::default();
15841586

15851587
let _: Result<(), !> = self.hygiene_ctxt.encode(
1586-
&mut (&mut *self, &mut syntax_contexts, &mut expn_data_table),
1587-
|(this, syntax_contexts, _), index, ctxt_data| {
1588+
&mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table),
1589+
|(this, syntax_contexts, _, _), index, ctxt_data| {
15881590
syntax_contexts.set(index, this.lazy(ctxt_data));
15891591
Ok(())
15901592
},
1591-
|(this, _, expn_data_table), index, expn_data| {
1593+
|(this, _, expn_data_table, expn_hash_table), index, expn_data, hash| {
15921594
expn_data_table.set(index, this.lazy(expn_data));
1595+
expn_hash_table.set(index, this.lazy(hash));
15931596
Ok(())
15941597
},
15951598
);
15961599

1597-
(syntax_contexts.encode(&mut self.opaque), expn_data_table.encode(&mut self.opaque))
1600+
(
1601+
syntax_contexts.encode(&mut self.opaque),
1602+
expn_data_table.encode(&mut self.opaque),
1603+
expn_hash_table.encode(&mut self.opaque),
1604+
)
15981605
}
15991606

16001607
fn encode_proc_macros(&mut self) -> Option<ProcMacroData> {

compiler/rustc_metadata/src/rmeta/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_session::config::SymbolManglingVersion;
2121
use rustc_span::edition::Edition;
2222
use rustc_span::hygiene::MacroKind;
2323
use rustc_span::symbol::{Ident, Symbol};
24-
use rustc_span::{self, ExpnData, ExpnId, Span};
24+
use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Span};
2525
use rustc_target::spec::{PanicStrategy, TargetTriple};
2626

2727
use std::marker::PhantomData;
@@ -171,6 +171,7 @@ macro_rules! Lazy {
171171

172172
type SyntaxContextTable = Lazy<Table<u32, Lazy<SyntaxContextData>>>;
173173
type ExpnDataTable = Lazy<Table<u32, Lazy<ExpnData>>>;
174+
type ExpnHashTable = Lazy<Table<u32, Lazy<ExpnHash>>>;
174175

175176
#[derive(MetadataEncodable, MetadataDecodable)]
176177
crate struct ProcMacroData {
@@ -226,6 +227,7 @@ crate struct CrateRoot<'tcx> {
226227

227228
syntax_contexts: SyntaxContextTable,
228229
expn_data: ExpnDataTable,
230+
expn_hashes: ExpnHashTable,
229231

230232
source_map: Lazy<[rustc_span::SourceFile]>,
231233

compiler/rustc_middle/src/ich/hcx.rs

-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_span::{BytePos, CachingSourceMapView, SourceFile, SpanData};
1616

1717
use smallvec::SmallVec;
1818
use std::cmp::Ord;
19-
use std::thread::LocalKey;
2019

2120
fn compute_ignored_attr_names() -> FxHashSet<Symbol> {
2221
debug_assert!(!ich::IGNORED_ATTRIBUTES.is_empty());
@@ -230,13 +229,6 @@ impl<'a> rustc_span::HashStableContext for StableHashingContext<'a> {
230229
self.def_path_hash(def_id)
231230
}
232231

233-
fn expn_id_cache() -> &'static LocalKey<rustc_span::ExpnIdCache> {
234-
thread_local! {
235-
static CACHE: rustc_span::ExpnIdCache = Default::default();
236-
}
237-
&CACHE
238-
}
239-
240232
fn span_data_to_lines_and_cols(
241233
&mut self,
242234
span: &SpanData,

compiler/rustc_middle/src/ty/query/on_disk_cache.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_span::hygiene::{
2525
};
2626
use rustc_span::source_map::{SourceMap, StableSourceFileId};
2727
use rustc_span::CachingSourceMapView;
28-
use rustc_span::{BytePos, ExpnData, SourceFile, Span, DUMMY_SP};
28+
use rustc_span::{BytePos, ExpnData, ExpnHash, SourceFile, Span, DUMMY_SP};
2929
use std::collections::hash_map::Entry;
3030
use std::mem;
3131

@@ -364,9 +364,9 @@ impl<'sess> OnDiskCache<'sess> {
364364
syntax_contexts.insert(index, pos);
365365
Ok(())
366366
},
367-
|encoder, index, expn_data| -> FileEncodeResult {
367+
|encoder, index, expn_data, hash| -> FileEncodeResult {
368368
let pos = AbsoluteBytePos::new(encoder.position());
369-
encoder.encode_tagged(TAG_EXPN_DATA, expn_data)?;
369+
encoder.encode_tagged(TAG_EXPN_DATA, &(expn_data, hash))?;
370370
expn_ids.insert(index, pos);
371371
Ok(())
372372
},
@@ -804,7 +804,7 @@ impl<'a, 'tcx> Decodable<CacheDecoder<'a, 'tcx>> for ExpnId {
804804
.unwrap_or_else(|| panic!("Bad index {:?} (map {:?})", index, expn_data));
805805

806806
this.with_position(pos.to_usize(), |decoder| {
807-
let data: ExpnData = decode_tagged(decoder, TAG_EXPN_DATA)?;
807+
let data: (ExpnData, ExpnHash) = decode_tagged(decoder, TAG_EXPN_DATA)?;
808808
Ok(data)
809809
})
810810
},

compiler/rustc_mir/src/transform/inline.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,8 @@ impl<'a, 'tcx> MutVisitor<'tcx> for Integrator<'a, 'tcx> {
839839
ExpnData::default(ExpnKind::Inlined, *span, self.tcx.sess.edition(), None, None);
840840
expn_data.def_site = self.body_span;
841841
// Make sure that all spans track the fact that they were inlined.
842-
*span = self.callsite_span.fresh_expansion(expn_data);
842+
*span =
843+
self.callsite_span.fresh_expansion(expn_data, self.tcx.create_stable_hashing_context());
843844
}
844845

845846
fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {

compiler/rustc_resolve/src/lib.rs

+42-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder};
3939
use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind};
4040
use rustc_hir::def::Namespace::*;
4141
use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes};
42-
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
42+
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefPathHash, LocalDefId, CRATE_DEF_INDEX};
4343
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
4444
use rustc_hir::TraitCandidate;
4545
use rustc_index::vec::IndexVec;
@@ -54,7 +54,7 @@ use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
5454
use rustc_session::Session;
5555
use rustc_span::edition::Edition;
5656
use rustc_span::hygiene::{ExpnId, ExpnKind, MacroKind, SyntaxContext, Transparency};
57-
use rustc_span::source_map::Spanned;
57+
use rustc_span::source_map::{CachingSourceMapView, Spanned};
5858
use rustc_span::symbol::{kw, sym, Ident, Symbol};
5959
use rustc_span::{Span, DUMMY_SP};
6060

@@ -1149,6 +1149,13 @@ impl ResolverAstLowering for Resolver<'_> {
11491149
self.opt_local_def_id(node).unwrap_or_else(|| panic!("no entry for node id: `{:?}`", node))
11501150
}
11511151

1152+
fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
1153+
match def_id.as_local() {
1154+
Some(def_id) => self.definitions.def_path_hash(def_id),
1155+
None => self.cstore().def_path_hash(def_id),
1156+
}
1157+
}
1158+
11521159
/// Adds a definition with a parent definition.
11531160
fn create_def(
11541161
&mut self,
@@ -1192,6 +1199,32 @@ impl ResolverAstLowering for Resolver<'_> {
11921199
}
11931200
}
11941201

1202+
struct ExpandHasher<'a, 'b> {
1203+
source_map: CachingSourceMapView<'a>,
1204+
resolver: &'a Resolver<'b>,
1205+
}
1206+
1207+
impl<'a, 'b> rustc_span::HashStableContext for ExpandHasher<'a, 'b> {
1208+
#[inline]
1209+
fn hash_spans(&self) -> bool {
1210+
true
1211+
}
1212+
1213+
#[inline]
1214+
fn def_path_hash(&self, def_id: DefId) -> DefPathHash {
1215+
self.resolver.def_path_hash(def_id)
1216+
}
1217+
1218+
#[inline]
1219+
fn span_data_to_lines_and_cols(
1220+
&mut self,
1221+
span: &rustc_span::SpanData,
1222+
) -> Option<(Lrc<rustc_span::SourceFile>, usize, rustc_span::BytePos, usize, rustc_span::BytePos)>
1223+
{
1224+
self.source_map.span_data_to_lines_and_cols(span)
1225+
}
1226+
}
1227+
11951228
impl<'a> Resolver<'a> {
11961229
pub fn new(
11971230
session: &'a Session,
@@ -1364,6 +1397,13 @@ impl<'a> Resolver<'a> {
13641397
resolver
13651398
}
13661399

1400+
fn create_stable_hashing_context(&self) -> ExpandHasher<'_, 'a> {
1401+
ExpandHasher {
1402+
source_map: CachingSourceMapView::new(self.session.source_map()),
1403+
resolver: self,
1404+
}
1405+
}
1406+
13671407
pub fn next_node_id(&mut self) -> NodeId {
13681408
let next = self
13691409
.next_node_id

compiler/rustc_resolve/src/macros.rs

+23-17
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,17 @@ impl<'a> ResolverExpand for Resolver<'a> {
218218
parent_module_id: Option<NodeId>,
219219
) -> ExpnId {
220220
let parent_module = parent_module_id.map(|module_id| self.local_def_id(module_id));
221-
let expn_id = ExpnId::fresh(Some(ExpnData::allow_unstable(
222-
ExpnKind::AstPass(pass),
223-
call_site,
224-
self.session.edition(),
225-
features.into(),
226-
None,
227-
parent_module.map(LocalDefId::to_def_id),
228-
)));
221+
let expn_id = ExpnId::fresh(
222+
ExpnData::allow_unstable(
223+
ExpnKind::AstPass(pass),
224+
call_site,
225+
self.session.edition(),
226+
features.into(),
227+
None,
228+
parent_module.map(LocalDefId::to_def_id),
229+
),
230+
self.create_stable_hashing_context(),
231+
);
229232

230233
let parent_scope = parent_module
231234
.map_or(self.empty_module, |parent_def_id| self.module_map[&parent_def_id]);
@@ -287,15 +290,18 @@ impl<'a> ResolverExpand for Resolver<'a> {
287290
)?;
288291

289292
let span = invoc.span();
290-
invoc_id.set_expn_data(ext.expn_data(
291-
parent_scope.expansion,
292-
span,
293-
fast_print_path(path),
294-
res.opt_def_id(),
295-
res.opt_def_id().map(|macro_def_id| {
296-
self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod
297-
}),
298-
));
293+
invoc_id.set_expn_data(
294+
ext.expn_data(
295+
parent_scope.expansion,
296+
span,
297+
fast_print_path(path),
298+
res.opt_def_id(),
299+
res.opt_def_id().map(|macro_def_id| {
300+
self.macro_def_scope_from_def_id(macro_def_id).nearest_parent_mod
301+
}),
302+
),
303+
self.create_stable_hashing_context(),
304+
);
299305

300306
if let Res::Def(_, _) = res {
301307
// Gate macro attributes in `#[derive]` output.

0 commit comments

Comments
 (0)