Skip to content

Commit 907aa44

Browse files
committed
rustc: Move stable_crate_id from Session to GlobalCtxt
Removes a piece of mutable state. Follow up to #114578.
1 parent 0b89aac commit 907aa44

File tree

10 files changed

+25
-27
lines changed

10 files changed

+25
-27
lines changed

compiler/rustc_codegen_ssa/src/back/linker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1703,7 +1703,7 @@ fn exported_symbols_for_proc_macro_crate(tcx: TyCtxt<'_>) -> Vec<String> {
17031703
return Vec::new();
17041704
}
17051705

1706-
let stable_crate_id = tcx.sess.local_stable_crate_id();
1706+
let stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
17071707
let proc_macro_decls_name = tcx.sess.generate_proc_macro_decls_symbol(stable_crate_id);
17081708
let metadata_symbol_name = exported_symbols::metadata_symbol_name(tcx);
17091709

compiler/rustc_interface/src/passes.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
1212
use rustc_errors::PResult;
1313
use rustc_expand::base::{ExtCtxt, LintStoreExpand};
1414
use rustc_fs_util::try_canonicalize;
15-
use rustc_hir::def_id::LOCAL_CRATE;
15+
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
1616
use rustc_lint::{unerased_lint_store, BufferedEarlyLint, EarlyCheckNode, LintStore};
1717
use rustc_metadata::creader::CStore;
1818
use rustc_middle::arena::Arena;
@@ -666,6 +666,7 @@ pub static DEFAULT_EXTERN_QUERY_PROVIDERS: LazyLock<ExternProviders> = LazyLock:
666666
pub fn create_global_ctxt<'tcx>(
667667
compiler: &'tcx Compiler,
668668
crate_types: Vec<CrateType>,
669+
stable_crate_id: StableCrateId,
669670
lint_store: Lrc<LintStore>,
670671
dep_graph: DepGraph,
671672
untracked: Untracked,
@@ -699,6 +700,7 @@ pub fn create_global_ctxt<'tcx>(
699700
TyCtxt::create_global_ctxt(
700701
sess,
701702
crate_types,
703+
stable_crate_id,
702704
lint_store,
703705
arena,
704706
hir_arena,

compiler/rustc_interface/src/queries.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,13 @@ impl<'tcx> Queries<'tcx> {
234234
debug_assert_eq!(_id, CRATE_DEF_ID);
235235
let untracked = Untracked { cstore, source_span, definitions };
236236

237-
// FIXME: Move these fields from session to tcx and make them immutable.
238-
sess.stable_crate_id.set(stable_crate_id).expect("not yet initialized");
237+
// FIXME: Move features from session to tcx and make them immutable.
239238
sess.init_features(rustc_expand::config::features(sess, &pre_configured_attrs));
240239

241240
let qcx = passes::create_global_ctxt(
242241
self.compiler,
243242
crate_types,
243+
stable_crate_id,
244244
lint_store,
245245
self.dep_graph(dep_graph_future),
246246
untracked,

compiler/rustc_middle/src/dep_graph/dep_node.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use crate::mir::mono::MonoItem;
6060
use crate::ty::TyCtxt;
6161

6262
use rustc_data_structures::fingerprint::Fingerprint;
63-
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
63+
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
6464
use rustc_hir::definitions::DefPathHash;
6565
use rustc_hir::{HirId, ItemLocalId, OwnerId};
6666
use rustc_query_system::dep_graph::FingerprintStyle;
@@ -371,7 +371,7 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
371371
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
372372
if tcx.fingerprint_style(dep_node.kind) == FingerprintStyle::HirId {
373373
let (local_hash, local_id) = Fingerprint::from(dep_node.hash).split();
374-
let def_path_hash = DefPathHash::new(tcx.sess.local_stable_crate_id(), local_hash);
374+
let def_path_hash = DefPathHash::new(tcx.stable_crate_id(LOCAL_CRATE), local_hash);
375375
let def_id = tcx
376376
.def_path_hash_to_def_id(def_path_hash, &mut || {
377377
panic!("Failed to extract HirId: {:?} {}", dep_node.kind, dep_node.hash)

compiler/rustc_middle/src/hir/map/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
12101210
owner_spans.hash_stable(&mut hcx, &mut stable_hasher);
12111211
}
12121212
tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher);
1213-
tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher);
1213+
tcx.stable_crate_id(LOCAL_CRATE).hash_stable(&mut hcx, &mut stable_hasher);
12141214
// Hash visibility information since it does not appear in HIR.
12151215
resolutions.visibilities.hash_stable(&mut hcx, &mut stable_hasher);
12161216
resolutions.has_pub_restricted.hash_stable(&mut hcx, &mut stable_hasher);

compiler/rustc_middle/src/middle/exported_symbols.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String {
7272
format!(
7373
"rust_metadata_{}_{:08x}",
7474
tcx.crate_name(LOCAL_CRATE),
75-
tcx.sess.local_stable_crate_id(),
75+
tcx.stable_crate_id(LOCAL_CRATE),
7676
)
7777
}

compiler/rustc_middle/src/mir/mono.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -524,13 +524,13 @@ impl<'tcx> CodegenUnitNameBuilder<'tcx> {
524524
// local crate's ID. Otherwise there can be collisions between CGUs
525525
// instantiating stuff for upstream crates.
526526
let local_crate_id = if cnum != LOCAL_CRATE {
527-
let local_stable_crate_id = tcx.sess.local_stable_crate_id();
527+
let local_stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
528528
format!("-in-{}.{:08x}", tcx.crate_name(LOCAL_CRATE), local_stable_crate_id)
529529
} else {
530530
String::new()
531531
};
532532

533-
let stable_crate_id = tcx.sess.local_stable_crate_id();
533+
let stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
534534
format!("{}.{:08x}{}", tcx.crate_name(cnum), stable_crate_id, local_crate_id)
535535
});
536536

compiler/rustc_middle/src/ty/context.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,12 @@ pub struct GlobalCtxt<'tcx> {
527527

528528
pub sess: &'tcx Session,
529529
crate_types: Vec<CrateType>,
530+
/// The `stable_crate_id` is constructed out of the crate name and all the
531+
/// `-C metadata` arguments passed to the compiler. Its value forms a unique
532+
/// global identifier for the crate. It is used to allow multiple crates
533+
/// with the same name to coexist. See the
534+
/// `rustc_symbol_mangling` crate for more information.
535+
stable_crate_id: StableCrateId,
530536

531537
/// This only ever stores a `LintStore` but we don't want a dependency on that type here.
532538
///
@@ -688,6 +694,7 @@ impl<'tcx> TyCtxt<'tcx> {
688694
pub fn create_global_ctxt(
689695
s: &'tcx Session,
690696
crate_types: Vec<CrateType>,
697+
stable_crate_id: StableCrateId,
691698
lint_store: Lrc<dyn Any + sync::DynSend + sync::DynSync>,
692699
arena: &'tcx WorkerLocal<Arena<'tcx>>,
693700
hir_arena: &'tcx WorkerLocal<hir::Arena<'tcx>>,
@@ -707,6 +714,7 @@ impl<'tcx> TyCtxt<'tcx> {
707714
GlobalCtxt {
708715
sess: s,
709716
crate_types,
717+
stable_crate_id,
710718
lint_store,
711719
arena,
712720
hir_arena,
@@ -842,7 +850,7 @@ impl<'tcx> TyCtxt<'tcx> {
842850
#[inline]
843851
pub fn stable_crate_id(self, crate_num: CrateNum) -> StableCrateId {
844852
if crate_num == LOCAL_CRATE {
845-
self.sess.local_stable_crate_id()
853+
self.stable_crate_id
846854
} else {
847855
self.cstore_untracked().stable_crate_id(crate_num)
848856
}
@@ -852,7 +860,7 @@ impl<'tcx> TyCtxt<'tcx> {
852860
/// that the crate in question has already been loaded by the CrateStore.
853861
#[inline]
854862
pub fn stable_crate_id_to_crate_num(self, stable_crate_id: StableCrateId) -> CrateNum {
855-
if stable_crate_id == self.sess.local_stable_crate_id() {
863+
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
856864
LOCAL_CRATE
857865
} else {
858866
self.cstore_untracked().stable_crate_id_to_crate_num(stable_crate_id)
@@ -869,7 +877,7 @@ impl<'tcx> TyCtxt<'tcx> {
869877

870878
// If this is a DefPathHash from the local crate, we can look up the
871879
// DefId in the tcx's `Definitions`.
872-
if stable_crate_id == self.sess.local_stable_crate_id() {
880+
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
873881
self.untracked.definitions.read().local_def_path_hash_to_def_id(hash, err).to_def_id()
874882
} else {
875883
// If this is a DefPathHash from an upstream crate, let the CrateStore map
@@ -886,7 +894,7 @@ impl<'tcx> TyCtxt<'tcx> {
886894
// statements within the query system and we'd run into endless
887895
// recursion otherwise.
888896
let (crate_name, stable_crate_id) = if def_id.is_local() {
889-
(self.crate_name(LOCAL_CRATE), self.sess.local_stable_crate_id())
897+
(self.crate_name(LOCAL_CRATE), self.stable_crate_id(LOCAL_CRATE))
890898
} else {
891899
let cstore = &*self.cstore_untracked();
892900
(cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate))

compiler/rustc_session/src/session.rs

-12
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,6 @@ pub struct Session {
152152
/// Input, input file path and output file path to this compilation process.
153153
pub io: CompilerIO,
154154

155-
/// The `stable_crate_id` is constructed out of the crate name and all the
156-
/// `-C metadata` arguments passed to the compiler. Its value forms a unique
157-
/// global identifier for the crate. It is used to allow multiple crates
158-
/// with the same name to coexist. See the
159-
/// `rustc_symbol_mangling` crate for more information.
160-
pub stable_crate_id: OnceCell<StableCrateId>,
161-
162155
features: OnceCell<rustc_feature::Features>,
163156

164157
incr_comp_session: OneThread<RefCell<IncrCompSession>>,
@@ -309,10 +302,6 @@ impl Session {
309302
self.parse_sess.span_diagnostic.emit_future_breakage_report(diags);
310303
}
311304

312-
pub fn local_stable_crate_id(&self) -> StableCrateId {
313-
self.stable_crate_id.get().copied().unwrap()
314-
}
315-
316305
/// Returns true if the crate is a testing one.
317306
pub fn is_test_crate(&self) -> bool {
318307
self.opts.test
@@ -1475,7 +1464,6 @@ pub fn build_session(
14751464
parse_sess,
14761465
sysroot,
14771466
io,
1478-
stable_crate_id: OnceCell::new(),
14791467
features: OnceCell::new(),
14801468
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
14811469
cgu_reuse_tracker,

compiler/rustc_symbol_mangling/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ fn compute_symbol_name<'tcx>(
180180

181181
if let Some(def_id) = def_id.as_local() {
182182
if tcx.proc_macro_decls_static(()) == Some(def_id) {
183-
let stable_crate_id = tcx.sess.local_stable_crate_id();
183+
let stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE);
184184
return tcx.sess.generate_proc_macro_decls_symbol(stable_crate_id);
185185
}
186186
}

0 commit comments

Comments
 (0)