Skip to content

Commit e28ed25

Browse files
committed
Auto merge of rust-lang#123099 - oli-obk:span_tcx, r=<try>
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. This is work towards unblocking rust-lang#122993 (the next step is giving `Span` deserialization access to the `TyCtxt`)
2 parents 536606b + 7ac3e6c commit e28ed25

File tree

7 files changed

+74
-81
lines changed

7 files changed

+74
-81
lines changed

compiler/rustc_hir/src/definitions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -380,14 +380,14 @@ impl Definitions {
380380
pub fn local_def_path_hash_to_def_id(
381381
&self,
382382
hash: DefPathHash,
383-
err: &mut dyn FnMut() -> !,
383+
err_msg: std::fmt::Arguments<'_>,
384384
) -> LocalDefId {
385385
debug_assert!(hash.stable_crate_id() == self.table.stable_crate_id);
386386
self.table
387387
.def_path_hash_to_index
388388
.get(&hash.local_hash())
389389
.map(|local_def_index| LocalDefId { local_def_index })
390-
.unwrap_or_else(|| err())
390+
.unwrap_or_else(|| panic!("{err_msg:?}"))
391391
}
392392

393393
pub fn def_path_hash_to_def_index_map(&self) -> &DefPathHashMap {

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

+35-19
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,41 @@ 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-
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-
}
655+
fn provide_cstore_hooks(providers: &mut Providers) {
656+
providers.hooks.def_path_hash_to_def_id = |tcx, hash, err_msg| {
657+
debug!("def_path_hash_to_def_id({:?})", hash);
658+
659+
let stable_crate_id = hash.stable_crate_id();
660+
661+
// If this is a DefPathHash from the local crate, we can look up the
662+
// DefId in the tcx's `Definitions`.
663+
if stable_crate_id == tcx.stable_crate_id(LOCAL_CRATE) {
664+
tcx.untracked()
665+
.definitions
666+
.read()
667+
.local_def_path_hash_to_def_id(hash, err_msg)
668+
.to_def_id()
669+
} else {
670+
// If this is a DefPathHash from an upstream crate, let the CrateStore map
671+
// it to a DefId.
672+
let cstore = CStore::from_tcx(tcx.tcx);
673+
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
674+
let def_index = cstore.get_crate_data(cnum).def_path_hash_to_def_index(hash);
675+
DefId { krate: cnum, index: def_index }
676+
}
677+
};
667678

668-
fn import_source_files(&self, sess: &Session, cnum: CrateNum) {
669-
let cdata = self.get_crate_data(cnum);
679+
providers.hooks.expn_hash_to_expn_id = |tcx, cnum, index_guess, hash| {
680+
let cstore = CStore::from_tcx(tcx.tcx);
681+
cstore.get_crate_data(cnum).expn_hash_to_expn_id(tcx.sess, index_guess, hash)
682+
};
683+
providers.hooks.import_source_files = |tcx, cnum| {
684+
let cstore = CStore::from_tcx(tcx.tcx);
685+
let cdata = cstore.get_crate_data(cnum);
670686
for file_index in 0..cdata.root.source_map.size() {
671-
cdata.imported_source_file(file_index as u32, sess);
687+
cdata.imported_source_file(file_index as u32, tcx.sess);
672688
}
673-
}
689+
};
674690
}

compiler/rustc_middle/src/dep_graph/dep_node.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ impl DepNodeExt for DepNode {
194194
/// has been removed.
195195
fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId> {
196196
if tcx.fingerprint_style(self.kind) == FingerprintStyle::DefPathHash {
197-
Some(tcx.def_path_hash_to_def_id(DefPathHash(self.hash.into()), &mut || {
198-
panic!("Failed to extract DefId: {:?} {}", self.kind, self.hash)
199-
}))
197+
Some(tcx.def_path_hash_to_def_id(
198+
DefPathHash(self.hash.into()),
199+
format_args!("Failed to extract DefId: {:?} {}", self.kind, self.hash),
200+
))
200201
} else {
201202
None
202203
}
@@ -390,9 +391,10 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
390391
let (local_hash, local_id) = Fingerprint::from(dep_node.hash).split();
391392
let def_path_hash = DefPathHash::new(tcx.stable_crate_id(LOCAL_CRATE), local_hash);
392393
let def_id = tcx
393-
.def_path_hash_to_def_id(def_path_hash, &mut || {
394-
panic!("Failed to extract HirId: {:?} {}", dep_node.kind, dep_node.hash)
395-
})
394+
.def_path_hash_to_def_id(
395+
def_path_hash,
396+
format_args!("Failed to extract HirId: {:?} {}", dep_node.kind, dep_node.hash),
397+
)
396398
.expect_local();
397399
let local_id = local_id
398400
.as_u64()

compiler/rustc_middle/src/hooks/mod.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
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_span::def_id::{CrateNum, LocalDefId};
11+
use rustc_span::{ExpnHash, ExpnId, DUMMY_SP};
1112

1213
macro_rules! declare_hooks {
1314
($($(#[$attr:meta])*hook $name:ident($($arg:ident: $K:ty),*) -> $V:ty;)*) => {
@@ -83,4 +84,23 @@ declare_hooks! {
8384
/// You do not want to call this yourself, instead use the cached version
8485
/// via `mir_built`
8586
hook build_mir(key: LocalDefId) -> mir::Body<'tcx>;
87+
88+
89+
/// Imports all `SourceFile`s from the given crate into the current session.
90+
/// This normally happens automatically when we decode a `Span` from
91+
/// that crate's metadata - however, the incr comp cache needs
92+
/// to trigger this manually when decoding a foreign `Span`
93+
hook import_source_files(key: CrateNum) -> ();
94+
95+
hook expn_hash_to_expn_id(
96+
cnum: CrateNum,
97+
index_guess: u32,
98+
hash: ExpnHash
99+
) -> ExpnId;
100+
101+
/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
102+
/// session, if it still exists. This is used during incremental compilation to
103+
/// turn a deserialized `DefPathHash` into its current `DefId`.
104+
/// Will fetch a DefId from a DefPathHash for a foreign crate.
105+
hook def_path_hash_to_def_id(hash: DefPathHash, err_msg: std::fmt::Arguments<'_>) -> DefId;
86106
}

compiler/rustc_middle/src/query/on_disk_cache.rs

+6-12
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+
let () = 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);
@@ -737,9 +730,10 @@ impl<'a, 'tcx> SpanDecoder for CacheDecoder<'a, 'tcx> {
737730
// If we get to this point, then all of the query inputs were green,
738731
// which means that the definition with this hash is guaranteed to
739732
// still exist in the current compilation session.
740-
self.tcx.def_path_hash_to_def_id(def_path_hash, &mut || {
741-
panic!("Failed to convert DefPathHash {def_path_hash:?}")
742-
})
733+
self.tcx.def_path_hash_to_def_id(
734+
def_path_hash,
735+
format_args!("Failed to convert DefPathHash {def_path_hash:?}"),
736+
)
743737
}
744738

745739
fn decode_attr_id(&mut self) -> rustc_span::AttrId {

compiler/rustc_middle/src/ty/context.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use rustc_session::config::CrateType;
6262
use rustc_session::cstore::{CrateStoreDyn, Untracked};
6363
use rustc_session::lint::Lint;
6464
use rustc_session::{Limit, MetadataKind, Session};
65-
use rustc_span::def_id::{DefPathHash, StableCrateId, CRATE_DEF_ID};
65+
use rustc_span::def_id::{StableCrateId, CRATE_DEF_ID};
6666
use rustc_span::symbol::{kw, sym, Ident, Symbol};
6767
use rustc_span::{Span, DUMMY_SP};
6868
use rustc_target::abi::{FieldIdx, Layout, LayoutS, TargetDataLayout, VariantIdx};
@@ -1060,27 +1060,6 @@ impl<'tcx> TyCtxt<'tcx> {
10601060
}
10611061
}
10621062

1063-
/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
1064-
/// session, if it still exists. This is used during incremental compilation to
1065-
/// turn a deserialized `DefPathHash` into its current `DefId`.
1066-
pub fn def_path_hash_to_def_id(self, hash: DefPathHash, err: &mut dyn FnMut() -> !) -> DefId {
1067-
debug!("def_path_hash_to_def_id({:?})", hash);
1068-
1069-
let stable_crate_id = hash.stable_crate_id();
1070-
1071-
// If this is a DefPathHash from the local crate, we can look up the
1072-
// DefId in the tcx's `Definitions`.
1073-
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
1074-
self.untracked.definitions.read().local_def_path_hash_to_def_id(hash, err).to_def_id()
1075-
} else {
1076-
// If this is a DefPathHash from an upstream crate, let the CrateStore map
1077-
// it to a DefId.
1078-
let cstore = &*self.cstore_untracked();
1079-
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
1080-
cstore.def_path_hash_to_def_id(cnum, hash)
1081-
}
1082-
}
1083-
10841063
pub fn def_path_debug_str(self, def_id: DefId) -> String {
10851064
// We are explicitly not going through queries here in order to get
10861065
// crate name and stable crate id since this code is called from debug!()

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)