Skip to content

Commit bd12986

Browse files
committed
Auto merge of #123099 - oli-obk:span_tcx, r=petrochenkov
Replace some `CrateStore` trait methods with hooks. Just like with the `CrateStore` trait, this avoids the cyclic definition issues with `CStore` being defined after TyCtxt, but needing to be used in TyCtxt.
2 parents 59c808f + 24fc6e9 commit bd12986

File tree

5 files changed

+46
-54
lines changed

5 files changed

+46
-54
lines changed

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+20-18
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_middle::ty::{self, TyCtxt};
2121
use rustc_middle::util::Providers;
2222
use rustc_session::cstore::{CrateStore, ExternCrate};
2323
use rustc_session::{Session, StableCrateId};
24-
use rustc_span::hygiene::{ExpnHash, ExpnId};
24+
use rustc_span::hygiene::ExpnId;
2525
use rustc_span::symbol::{kw, Symbol};
2626
use rustc_span::Span;
2727

@@ -378,6 +378,7 @@ provide! { tcx, def_id, other, cdata,
378378
}
379379

380380
pub(in crate::rmeta) fn provide(providers: &mut Providers) {
381+
provide_cstore_hooks(providers);
381382
// FIXME(#44234) - almost all of these queries have no sub-queries and
382383
// therefore no actual inputs, they're just reading tables calculated in
383384
// resolve! Does this work? Unsure! That's what the issue is about
@@ -649,26 +650,27 @@ impl CrateStore for CStore {
649650
fn def_path_hash(&self, def: DefId) -> DefPathHash {
650651
self.get_crate_data(def.krate).def_path_hash(def.index)
651652
}
653+
}
652654

653-
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId {
654-
let def_index = self.get_crate_data(cnum).def_path_hash_to_def_index(hash);
655+
fn provide_cstore_hooks(providers: &mut Providers) {
656+
providers.hooks.def_path_hash_to_def_id_extern = |tcx, hash, stable_crate_id| {
657+
// If this is a DefPathHash from an upstream crate, let the CrateStore map
658+
// it to a DefId.
659+
let cstore = CStore::from_tcx(tcx.tcx);
660+
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
661+
let def_index = cstore.get_crate_data(cnum).def_path_hash_to_def_index(hash);
655662
DefId { krate: cnum, index: def_index }
656-
}
657-
658-
fn expn_hash_to_expn_id(
659-
&self,
660-
sess: &Session,
661-
cnum: CrateNum,
662-
index_guess: u32,
663-
hash: ExpnHash,
664-
) -> ExpnId {
665-
self.get_crate_data(cnum).expn_hash_to_expn_id(sess, index_guess, hash)
666-
}
663+
};
667664

668-
fn import_source_files(&self, sess: &Session, cnum: CrateNum) {
669-
let cdata = self.get_crate_data(cnum);
665+
providers.hooks.expn_hash_to_expn_id = |tcx, cnum, index_guess, hash| {
666+
let cstore = CStore::from_tcx(tcx.tcx);
667+
cstore.get_crate_data(cnum).expn_hash_to_expn_id(tcx.sess, index_guess, hash)
668+
};
669+
providers.hooks.import_source_files = |tcx, cnum| {
670+
let cstore = CStore::from_tcx(tcx.tcx);
671+
let cdata = cstore.get_crate_data(cnum);
670672
for file_index in 0..cdata.root.source_map.size() {
671-
cdata.imported_source_file(file_index as u32, sess);
673+
cdata.imported_source_file(file_index as u32, tcx.sess);
672674
}
673-
}
675+
};
674676
}

compiler/rustc_middle/src/hooks/mod.rs

+23-4
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
use crate::mir;
77
use crate::query::TyCtxtAt;
88
use crate::ty::{Ty, TyCtxt};
9-
use rustc_span::def_id::LocalDefId;
10-
use rustc_span::DUMMY_SP;
9+
use rustc_hir::def_id::{DefId, DefPathHash};
10+
use rustc_session::StableCrateId;
11+
use rustc_span::def_id::{CrateNum, LocalDefId};
12+
use rustc_span::{ExpnHash, ExpnId, DUMMY_SP};
1113

1214
macro_rules! declare_hooks {
1315
($($(#[$attr:meta])*hook $name:ident($($arg:ident: $K:ty),*) -> $V:ty;)*) => {
@@ -16,7 +18,6 @@ macro_rules! declare_hooks {
1618
$(
1719
$(#[$attr])*
1820
#[inline(always)]
19-
#[must_use]
2021
pub fn $name(self, $($arg: $K,)*) -> $V
2122
{
2223
self.at(DUMMY_SP).$name($($arg,)*)
@@ -28,7 +29,6 @@ macro_rules! declare_hooks {
2829
$(
2930
$(#[$attr])*
3031
#[inline(always)]
31-
#[must_use]
3232
#[instrument(level = "debug", skip(self), ret)]
3333
pub fn $name(self, $($arg: $K,)*) -> $V
3434
{
@@ -83,4 +83,23 @@ declare_hooks! {
8383
/// You do not want to call this yourself, instead use the cached version
8484
/// via `mir_built`
8585
hook build_mir(key: LocalDefId) -> mir::Body<'tcx>;
86+
87+
88+
/// Imports all `SourceFile`s from the given crate into the current session.
89+
/// This normally happens automatically when we decode a `Span` from
90+
/// that crate's metadata - however, the incr comp cache needs
91+
/// to trigger this manually when decoding a foreign `Span`
92+
hook import_source_files(key: CrateNum) -> ();
93+
94+
hook expn_hash_to_expn_id(
95+
cnum: CrateNum,
96+
index_guess: u32,
97+
hash: ExpnHash
98+
) -> ExpnId;
99+
100+
/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
101+
/// session, if it still exists. This is used during incremental compilation to
102+
/// turn a deserialized `DefPathHash` into its current `DefId`.
103+
/// Will fetch a DefId from a DefPathHash for a foreign crate.
104+
hook def_path_hash_to_def_id_extern(hash: DefPathHash, stable_crate_id: StableCrateId) -> DefId;
86105
}

compiler/rustc_middle/src/query/on_disk_cache.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -492,9 +492,7 @@ impl<'a, 'tcx> CacheDecoder<'a, 'tcx> {
492492
// expansion, so we use `import_source_files` to ensure that the foreign
493493
// source files are actually imported before we call `source_file_by_stable_id`.
494494
if source_file_cnum != LOCAL_CRATE {
495-
self.tcx
496-
.cstore_untracked()
497-
.import_source_files(self.tcx.sess, source_file_cnum);
495+
self.tcx.import_source_files(source_file_cnum);
498496
}
499497

500498
source_map
@@ -634,12 +632,7 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
634632
expn_id
635633
} else {
636634
let index_guess = self.foreign_expn_data[&hash];
637-
self.tcx.cstore_untracked().expn_hash_to_expn_id(
638-
self.tcx.sess,
639-
krate,
640-
index_guess,
641-
hash,
642-
)
635+
self.tcx.expn_hash_to_expn_id(krate, index_guess, hash)
643636
};
644637

645638
debug_assert_eq!(expn_id.krate, krate);

compiler/rustc_middle/src/ty/context.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1165,11 +1165,7 @@ impl<'tcx> TyCtxt<'tcx> {
11651165
.local_def_path_hash_to_def_id(hash, err_msg)
11661166
.to_def_id()
11671167
} else {
1168-
// If this is a DefPathHash from an upstream crate, let the CrateStore map
1169-
// it to a DefId.
1170-
let cstore = &*self.cstore_untracked();
1171-
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
1172-
cstore.def_path_hash_to_def_id(cnum, hash)
1168+
self.def_path_hash_to_def_id_extern(hash, stable_crate_id)
11731169
}
11741170
}
11751171

compiler/rustc_session/src/cstore.rs

-18
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
55
use crate::search_paths::PathKind;
66
use crate::utils::NativeLibKind;
7-
use crate::Session;
87
use rustc_ast as ast;
98
use rustc_data_structures::sync::{self, AppendOnlyIndexVec, FreezeLock};
109
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, StableCrateId, LOCAL_CRATE};
1110
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash, Definitions};
12-
use rustc_span::hygiene::{ExpnHash, ExpnId};
1311
use rustc_span::symbol::Symbol;
1412
use rustc_span::Span;
1513
use rustc_target::spec::abi::Abi;
@@ -220,22 +218,6 @@ pub trait CrateStore: std::fmt::Debug {
220218
fn crate_name(&self, cnum: CrateNum) -> Symbol;
221219
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId;
222220
fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum;
223-
224-
/// Fetch a DefId from a DefPathHash for a foreign crate.
225-
fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId;
226-
fn expn_hash_to_expn_id(
227-
&self,
228-
sess: &Session,
229-
cnum: CrateNum,
230-
index_guess: u32,
231-
hash: ExpnHash,
232-
) -> ExpnId;
233-
234-
/// Imports all `SourceFile`s from the given crate into the current session.
235-
/// This normally happens automatically when we decode a `Span` from
236-
/// that crate's metadata - however, the incr comp cache needs
237-
/// to trigger this manually when decoding a foreign `Span`
238-
fn import_source_files(&self, sess: &Session, cnum: CrateNum);
239221
}
240222

241223
pub type CrateStoreDyn = dyn CrateStore + sync::DynSync + sync::DynSend;

0 commit comments

Comments
 (0)