Skip to content

Commit f037024

Browse files
authored
Rollup merge of rust-lang#66334 - Mark-Simulacrum:sess-cstore, r=petrochenkov
Move Session fields to CrateStore `allocator_kind` and `injected_panic_runtime` are both query-like, this moves them out of Session and into CrateStore, avoiding the `Once` they previously had by clearing separating initialization and de-initialization.
2 parents 9091d50 + 2c6d609 commit f037024

File tree

9 files changed

+38
-23
lines changed

9 files changed

+38
-23
lines changed

src/librustc/middle/cstore.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::path::{Path, PathBuf};
1515
use syntax::ast;
1616
use syntax::symbol::Symbol;
1717
use syntax_pos::Span;
18+
use syntax::expand::allocator::AllocatorKind;
1819
use rustc_target::spec::Target;
1920
use rustc_data_structures::sync::{self, MetadataRef};
2021
use rustc_macros::HashStable;
@@ -227,6 +228,8 @@ pub trait CrateStore {
227228
// utility functions
228229
fn encode_metadata(&self, tcx: TyCtxt<'_>) -> EncodedMetadata;
229230
fn metadata_encoding_version(&self) -> &[u8];
231+
fn injected_panic_runtime(&self) -> Option<CrateNum>;
232+
fn allocator_kind(&self) -> Option<AllocatorKind>;
230233
}
231234

232235
pub type CrateStoreDyn = dyn CrateStore + sync::Sync;

src/librustc/session/mod.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ pub use self::code_stats::{DataTypeKind, SizeKind, FieldInfo, VariantInfo};
22
use self::code_stats::CodeStats;
33

44
use crate::dep_graph::cgu_reuse_tracker::CguReuseTracker;
5-
use crate::hir::def_id::CrateNum;
65
use rustc_data_structures::fingerprint::Fingerprint;
76

87
use crate::lint;
@@ -22,7 +21,6 @@ use errors::emitter::{Emitter, EmitterWriter};
2221
use errors::emitter::HumanReadableErrorType;
2322
use errors::annotate_snippet_emitter_writer::{AnnotateSnippetEmitterWriter};
2423
use syntax::edition::Edition;
25-
use syntax::expand::allocator::AllocatorKind;
2624
use syntax::feature_gate::{self, AttributeType};
2725
use syntax::json::JsonEmitter;
2826
use syntax::source_map;
@@ -102,12 +100,6 @@ pub struct Session {
102100
/// The maximum number of stackframes allowed in const eval.
103101
pub const_eval_stack_frame_limit: usize,
104102

105-
/// The `metadata::creader` module may inject an allocator/`panic_runtime`
106-
/// dependency if it didn't already find one, and this tracks what was
107-
/// injected.
108-
pub allocator_kind: Once<Option<AllocatorKind>>,
109-
pub injected_panic_runtime: Once<Option<CrateNum>>,
110-
111103
/// Map from imported macro spans (which consist of
112104
/// the localized span for the macro body) to the
113105
/// macro name and definition span in the source crate.
@@ -1182,8 +1174,6 @@ fn build_session_(
11821174
recursion_limit: Once::new(),
11831175
type_length_limit: Once::new(),
11841176
const_eval_stack_frame_limit: 100,
1185-
allocator_kind: Once::new(),
1186-
injected_panic_runtime: Once::new(),
11871177
imported_macro_spans: OneThread::new(RefCell::new(FxHashMap::default())),
11881178
incr_comp_session: OneThread::new(RefCell::new(IncrCompSession::NotInitialized)),
11891179
cgu_reuse_tracker,

src/librustc/ty/context.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ use syntax::source_map::MultiSpan;
7575
use syntax::feature_gate;
7676
use syntax::symbol::{Symbol, kw, sym};
7777
use syntax_pos::Span;
78+
use syntax::expand::allocator::AllocatorKind;
7879

7980
pub struct AllArenas {
8081
pub interner: SyncDroplessArena,
@@ -1338,6 +1339,14 @@ impl<'tcx> TyCtxt<'tcx> {
13381339
self.all_crate_nums(LOCAL_CRATE)
13391340
}
13401341

1342+
pub fn injected_panic_runtime(self) -> Option<CrateNum> {
1343+
self.cstore.injected_panic_runtime()
1344+
}
1345+
1346+
pub fn allocator_kind(self) -> Option<AllocatorKind> {
1347+
self.cstore.allocator_kind()
1348+
}
1349+
13411350
pub fn features(self) -> &'tcx feature_gate::Features {
13421351
self.features_query(LOCAL_CRATE)
13431352
}

src/librustc_codegen_ssa/back/symbol_export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn exported_symbols_provider_local(
194194
symbols.push((exported_symbol, SymbolExportLevel::C));
195195
}
196196

197-
if tcx.sess.allocator_kind.get().is_some() {
197+
if tcx.allocator_kind().is_some() {
198198
for method in ALLOCATOR_METHODS {
199199
let symbol_name = format!("__rust_{}", method.name);
200200
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(&symbol_name));

src/librustc_codegen_ssa/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
549549
});
550550
let allocator_module = if any_dynamic_crate {
551551
None
552-
} else if let Some(kind) = *tcx.sess.allocator_kind.get() {
552+
} else if let Some(kind) = tcx.allocator_kind() {
553553
let llmod_id = cgu_name_builder.build_cgu_name(LOCAL_CRATE,
554554
&["crate"],
555555
Some("allocator")).to_string();

src/librustc_metadata/creader.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ impl<'a> CrateLoader<'a> {
531531
});
532532
if !any_non_rlib {
533533
info!("panic runtime injection skipped, only generating rlib");
534-
self.sess.injected_panic_runtime.set(None);
534+
self.cstore.injected_panic_runtime = None;
535535
return
536536
}
537537

@@ -563,7 +563,7 @@ impl<'a> CrateLoader<'a> {
563563
// we just don't need one at all, then we're done here and there's
564564
// nothing else to do.
565565
if !needs_panic_runtime || runtime_found {
566-
self.sess.injected_panic_runtime.set(None);
566+
self.cstore.injected_panic_runtime = None;
567567
return
568568
}
569569

@@ -600,7 +600,7 @@ impl<'a> CrateLoader<'a> {
600600
name, desired_strategy.desc()));
601601
}
602602

603-
self.sess.injected_panic_runtime.set(Some(cnum));
603+
self.cstore.injected_panic_runtime = Some(cnum);
604604
self.inject_dependency_if(cnum, "a panic runtime",
605605
&|data| data.root.needs_panic_runtime);
606606
}
@@ -722,7 +722,7 @@ impl<'a> CrateLoader<'a> {
722722
}
723723
}
724724

725-
fn inject_allocator_crate(&self, krate: &ast::Crate) {
725+
fn inject_allocator_crate(&mut self, krate: &ast::Crate) {
726726
let has_global_allocator = match &*global_allocator_spans(krate) {
727727
[span1, span2, ..] => {
728728
self.sess.struct_span_err(*span2, "cannot define multiple global allocators")
@@ -742,7 +742,7 @@ impl<'a> CrateLoader<'a> {
742742
needs_allocator = needs_allocator || data.root.needs_allocator;
743743
});
744744
if !needs_allocator {
745-
self.sess.allocator_kind.set(None);
745+
self.cstore.allocator_kind = None;
746746
return
747747
}
748748

@@ -758,7 +758,7 @@ impl<'a> CrateLoader<'a> {
758758
}
759759
});
760760
if all_rlib {
761-
self.sess.allocator_kind.set(None);
761+
self.cstore.allocator_kind = None;
762762
return
763763
}
764764

@@ -795,7 +795,7 @@ impl<'a> CrateLoader<'a> {
795795
}
796796
});
797797
if global_allocator.is_some() {
798-
self.sess.allocator_kind.set(Some(AllocatorKind::Global));
798+
self.cstore.allocator_kind = Some(AllocatorKind::Global);
799799
return
800800
}
801801

@@ -816,7 +816,7 @@ impl<'a> CrateLoader<'a> {
816816
add `#[global_allocator]` to a static item \
817817
that implements the GlobalAlloc trait.");
818818
}
819-
self.sess.allocator_kind.set(Some(AllocatorKind::DefaultLib));
819+
self.cstore.allocator_kind = Some(AllocatorKind::DefaultLib);
820820
}
821821

822822
fn inject_dependency_if(&self,

src/librustc_metadata/cstore.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_data_structures::svh::Svh;
1414
use syntax::ast;
1515
use syntax::edition::Edition;
1616
use syntax_expand::base::SyntaxExtension;
17+
use syntax::expand::allocator::AllocatorKind;
1718
use syntax_pos;
1819
use proc_macro::bridge::client::ProcMacro;
1920

@@ -101,6 +102,8 @@ crate struct CrateMetadata {
101102
#[derive(Clone)]
102103
pub struct CStore {
103104
metas: IndexVec<CrateNum, Option<Lrc<CrateMetadata>>>,
105+
pub(crate) injected_panic_runtime: Option<CrateNum>,
106+
pub(crate) allocator_kind: Option<AllocatorKind>,
104107
}
105108

106109
pub enum LoadedMacro {
@@ -116,6 +119,8 @@ impl Default for CStore {
116119
// corresponding `CrateNum`. This first entry will always remain
117120
// `None`.
118121
metas: IndexVec::from_elem_n(None, 1),
122+
injected_panic_runtime: None,
123+
allocator_kind: None,
119124
}
120125
}
121126
}

src/librustc_metadata/dependency_format.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: config::CrateType) -> DependencyList {
184184
//
185185
// Things like allocators and panic runtimes may not have been activated
186186
// quite yet, so do so here.
187-
activate_injected_dep(*sess.injected_panic_runtime.get(), &mut ret,
187+
activate_injected_dep(tcx.injected_panic_runtime(), &mut ret,
188188
&|cnum| tcx.is_panic_runtime(cnum));
189189

190190
// When dylib B links to dylib A, then when using B we must also link to A.
@@ -244,7 +244,6 @@ fn add_library(
244244
}
245245

246246
fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {
247-
let sess = &tcx.sess;
248247
let crates = cstore::used_crates(tcx, RequireStatic);
249248
if !crates.iter().by_ref().all(|&(_, ref p)| p.is_some()) {
250249
return None
@@ -264,7 +263,7 @@ fn attempt_static(tcx: TyCtxt<'_>) -> Option<DependencyList> {
264263
// Our allocator/panic runtime may not have been linked above if it wasn't
265264
// explicitly linked, which is the case for any injected dependency. Handle
266265
// that here and activate them.
267-
activate_injected_dep(*sess.injected_panic_runtime.get(), &mut ret,
266+
activate_injected_dep(tcx.injected_panic_runtime(), &mut ret,
268267
&|cnum| tcx.is_panic_runtime(cnum));
269268

270269
Some(ret)

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use syntax::attr;
3131
use syntax::source_map;
3232
use syntax::source_map::Spanned;
3333
use syntax::symbol::Symbol;
34+
use syntax::expand::allocator::AllocatorKind;
3435
use syntax_pos::{Span, FileName};
3536

3637
macro_rules! provide {
@@ -527,4 +528,12 @@ impl CrateStore for cstore::CStore {
527528
{
528529
rmeta::METADATA_HEADER
529530
}
531+
532+
fn injected_panic_runtime(&self) -> Option<CrateNum> {
533+
self.injected_panic_runtime
534+
}
535+
536+
fn allocator_kind(&self) -> Option<AllocatorKind> {
537+
self.allocator_kind
538+
}
530539
}

0 commit comments

Comments
 (0)