Skip to content

Commit 32bd3c3

Browse files
committed
Start replacing CStore trait methods with hooks.
This also avoids the cyclic definition issues with CrateStore being defined after TyCtxt, but needing to be used in TyCtxt.
1 parent b13a71a commit 32bd3c3

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -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
@@ -664,11 +665,14 @@ impl CrateStore for CStore {
664665
) -> ExpnId {
665666
self.get_crate_data(cnum).expn_hash_to_expn_id(sess, index_guess, hash)
666667
}
668+
}
667669

668-
fn import_source_files(&self, sess: &Session, cnum: CrateNum) {
669-
let cdata = self.get_crate_data(cnum);
670+
fn provide_cstore_hooks(providers: &mut Providers) {
671+
providers.hooks.import_source_files = |tcx, cnum| {
672+
let cstore = CStore::from_tcx(tcx.tcx);
673+
let cdata = cstore.get_crate_data(cnum);
670674
for file_index in 0..cdata.root.source_map.size() {
671-
cdata.imported_source_file(file_index as u32, sess);
675+
cdata.imported_source_file(file_index as u32, tcx.sess);
672676
}
673-
}
677+
};
674678
}

compiler/rustc_middle/src/hooks/mod.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use crate::mir;
77
use crate::query::TyCtxtAt;
88
use crate::ty::{Ty, TyCtxt};
9-
use rustc_span::def_id::LocalDefId;
9+
use rustc_span::def_id::{CrateNum, LocalDefId};
1010
use rustc_span::DUMMY_SP;
1111

1212
macro_rules! declare_hooks {
@@ -16,7 +16,6 @@ macro_rules! declare_hooks {
1616
$(
1717
$(#[$attr])*
1818
#[inline(always)]
19-
#[must_use]
2019
pub fn $name(self, $($arg: $K,)*) -> $V
2120
{
2221
self.at(DUMMY_SP).$name($($arg,)*)
@@ -28,7 +27,6 @@ macro_rules! declare_hooks {
2827
$(
2928
$(#[$attr])*
3029
#[inline(always)]
31-
#[must_use]
3230
#[instrument(level = "debug", skip(self), ret)]
3331
pub fn $name(self, $($arg: $K,)*) -> $V
3432
{
@@ -83,4 +81,11 @@ declare_hooks! {
8381
/// You do not want to call this yourself, instead use the cached version
8482
/// via `mir_built`
8583
hook build_mir(key: LocalDefId) -> mir::Body<'tcx>;
84+
85+
86+
/// Imports all `SourceFile`s from the given crate into the current session.
87+
/// This normally happens automatically when we decode a `Span` from
88+
/// that crate's metadata - however, the incr comp cache needs
89+
/// to trigger this manually when decoding a foreign `Span`
90+
hook import_source_files(key: CrateNum) -> ();
8691
}

compiler/rustc_middle/src/query/on_disk_cache.rs

+1-3
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

compiler/rustc_session/src/cstore.rs

-6
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,6 @@ pub trait CrateStore: std::fmt::Debug {
230230
index_guess: u32,
231231
hash: ExpnHash,
232232
) -> 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);
239233
}
240234

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

0 commit comments

Comments
 (0)