Skip to content

Commit 94533d9

Browse files
Rollup merge of #114622 - petrochenkov:noplugin, r=oli-obk
rustc: Move `crate_types` and `stable_crate_id` from `Session` to `GlobalCtxt` Removes two pieces of mutable state. Follow up to #114578.
2 parents 12551a5 + 907aa44 commit 94533d9

File tree

32 files changed

+133
-135
lines changed

32 files changed

+133
-135
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, (): ()) -> hir::Crate<'_> {
457457

458458
// Don't hash unless necessary, because it's expensive.
459459
let opt_hir_hash =
460-
if tcx.sess.needs_crate_hash() { Some(compute_hir_hash(tcx, &owners)) } else { None };
460+
if tcx.needs_crate_hash() { Some(compute_hir_hash(tcx, &owners)) } else { None };
461461
hir::Crate { owners, opt_hir_hash }
462462
}
463463

@@ -648,7 +648,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
648648
let bodies = SortedMap::from_presorted_elements(bodies);
649649

650650
// Don't hash unless necessary, because it's expensive.
651-
let (opt_hash_including_bodies, attrs_hash) = if self.tcx.sess.needs_crate_hash() {
651+
let (opt_hash_including_bodies, attrs_hash) = if self.tcx.needs_crate_hash() {
652652
self.tcx.with_stable_hashing_context(|mut hcx| {
653653
let mut stable_hasher = StableHasher::new();
654654
hcx.with_hir_bodies(node.def_id(), &bodies, |hcx| {

compiler/rustc_codegen_cranelift/src/driver/jit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub(crate) fn run_jit(tcx: TyCtxt<'_>, backend_config: BackendConfig) -> ! {
9898
tcx.sess.fatal("JIT mode doesn't work with `cargo check`");
9999
}
100100

101-
if !tcx.sess.crate_types().contains(&rustc_session::config::CrateType::Executable) {
101+
if !tcx.crate_types().contains(&rustc_session::config::CrateType::Executable) {
102102
tcx.sess.fatal("can't jit non-executable crate");
103103
}
104104

compiler/rustc_codegen_llvm/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ pub unsafe fn create_module<'ll>(
209209
// PIE is potentially more effective than PIC, but can only be used in executables.
210210
// If all our outputs are executables, then we can relax PIC to PIE.
211211
if reloc_model == RelocModel::Pie
212-
|| sess.crate_types().iter().all(|ty| *ty == CrateType::Executable)
212+
|| tcx.crate_types().iter().all(|ty| *ty == CrateType::Executable)
213213
{
214214
llvm::LLVMRustSetModulePIELevel(llmod);
215215
}

compiler/rustc_codegen_llvm/src/debuginfo/gdb.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn needs_gdb_debug_scripts_section(cx: &CodegenCx<'_, '_>) -> bool {
9292
// each rlib could produce a different set of visualizers that would be embedded
9393
// in the `.debug_gdb_scripts` section. For that reason, we make sure that the
9494
// section is only emitted for leaf crates.
95-
let embed_visualizers = cx.sess().crate_types().iter().any(|&crate_type| match crate_type {
95+
let embed_visualizers = cx.tcx.crate_types().iter().any(|&crate_type| match crate_type {
9696
CrateType::Executable | CrateType::Dylib | CrateType::Cdylib | CrateType::Staticlib => {
9797
// These are crate types for which we will embed pretty printers since they
9898
// are treated as leaf crates.

compiler/rustc_codegen_llvm/src/mono_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl CodegenCx<'_, '_> {
111111
}
112112

113113
// Symbols from executables can't really be imported any further.
114-
let all_exe = self.tcx.sess.crate_types().iter().all(|ty| *ty == CrateType::Executable);
114+
let all_exe = self.tcx.crate_types().iter().all(|ty| *ty == CrateType::Executable);
115115
let is_declaration_for_linker =
116116
is_declaration || linkage == llvm::Linkage::AvailableExternallyLinkage;
117117
if all_exe && !is_declaration_for_linker {

compiler/rustc_codegen_ssa/src/back/link.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn link_binary<'a>(
6969
let _timer = sess.timer("link_binary");
7070
let output_metadata = sess.opts.output_types.contains_key(&OutputType::Metadata);
7171
let mut tempfiles_for_stdout_output: Vec<PathBuf> = Vec::new();
72-
for &crate_type in sess.crate_types().iter() {
72+
for &crate_type in &codegen_results.crate_info.crate_types {
7373
// Ignore executable crates if we have -Z no-codegen, as they will error.
7474
if (sess.opts.unstable_opts.no_codegen || !sess.opts.output_types.should_codegen())
7575
&& !output_metadata

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_codegen_ssa/src/back/symbol_export.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_session::config::{CrateType, OomStrategy};
1919
use rustc_target::spec::SanitizerSet;
2020

2121
pub fn threshold(tcx: TyCtxt<'_>) -> SymbolExportLevel {
22-
crates_export_threshold(&tcx.sess.crate_types())
22+
crates_export_threshold(tcx.crate_types())
2323
}
2424

2525
fn crate_export_threshold(crate_type: CrateType) -> SymbolExportLevel {
@@ -290,8 +290,8 @@ fn exported_symbols_provider_local(
290290
}));
291291
}
292292

293-
if tcx.sess.crate_types().contains(&CrateType::Dylib)
294-
|| tcx.sess.crate_types().contains(&CrateType::ProcMacro)
293+
if tcx.crate_types().contains(&CrateType::Dylib)
294+
|| tcx.crate_types().contains(&CrateType::ProcMacro)
295295
{
296296
let symbol_name = metadata_symbol_name(tcx);
297297
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new(tcx, &symbol_name));

compiler/rustc_codegen_ssa/src/back/write.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ pub struct ModuleConfig {
123123
impl ModuleConfig {
124124
fn new(
125125
kind: ModuleKind,
126-
sess: &Session,
126+
tcx: TyCtxt<'_>,
127127
no_builtins: bool,
128128
is_compiler_builtins: bool,
129129
) -> ModuleConfig {
@@ -135,6 +135,7 @@ impl ModuleConfig {
135135
};
136136
}
137137

138+
let sess = tcx.sess;
138139
let opt_level_and_size = if_regular!(Some(sess.opts.optimize), None);
139140

140141
let save_temps = sess.opts.cg.save_temps;
@@ -166,7 +167,7 @@ impl ModuleConfig {
166167
// `#![no_builtins]` is assumed to not participate in LTO and
167168
// instead goes on to generate object code.
168169
EmitObj::Bitcode
169-
} else if need_bitcode_in_object(sess) {
170+
} else if need_bitcode_in_object(tcx) {
170171
EmitObj::ObjectCode(BitcodeSection::Full)
171172
} else {
172173
EmitObj::ObjectCode(BitcodeSection::None)
@@ -414,9 +415,10 @@ pub struct CompiledModules {
414415
pub allocator_module: Option<CompiledModule>,
415416
}
416417

417-
fn need_bitcode_in_object(sess: &Session) -> bool {
418+
fn need_bitcode_in_object(tcx: TyCtxt<'_>) -> bool {
419+
let sess = tcx.sess;
418420
let requested_for_rlib = sess.opts.cg.embed_bitcode
419-
&& sess.crate_types().contains(&CrateType::Rlib)
421+
&& tcx.crate_types().contains(&CrateType::Rlib)
420422
&& sess.opts.output_types.contains_key(&OutputType::Exe);
421423
let forced_by_target = sess.target.forces_embed_bitcode;
422424
requested_for_rlib || forced_by_target
@@ -450,11 +452,11 @@ pub fn start_async_codegen<B: ExtraBackendMethods>(
450452
let crate_info = CrateInfo::new(tcx, target_cpu);
451453

452454
let regular_config =
453-
ModuleConfig::new(ModuleKind::Regular, sess, no_builtins, is_compiler_builtins);
455+
ModuleConfig::new(ModuleKind::Regular, tcx, no_builtins, is_compiler_builtins);
454456
let metadata_config =
455-
ModuleConfig::new(ModuleKind::Metadata, sess, no_builtins, is_compiler_builtins);
457+
ModuleConfig::new(ModuleKind::Metadata, tcx, no_builtins, is_compiler_builtins);
456458
let allocator_config =
457-
ModuleConfig::new(ModuleKind::Allocator, sess, no_builtins, is_compiler_builtins);
459+
ModuleConfig::new(ModuleKind::Allocator, tcx, no_builtins, is_compiler_builtins);
458460

459461
let (shared_emitter, shared_emitter_main) = SharedEmitter::new();
460462
let (codegen_worker_send, codegen_worker_receive) = channel();
@@ -1092,7 +1094,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
10921094
};
10931095

10941096
let cgcx = CodegenContext::<B> {
1095-
crate_types: sess.crate_types().to_vec(),
1097+
crate_types: tcx.crate_types().to_vec(),
10961098
each_linked_rlib_for_lto,
10971099
lto: sess.lto(),
10981100
fewer_names: sess.fewer_names(),
@@ -2063,7 +2065,7 @@ fn msvc_imps_needed(tcx: TyCtxt<'_>) -> bool {
20632065
);
20642066

20652067
tcx.sess.target.is_like_windows &&
2066-
tcx.sess.crate_types().iter().any(|ct| *ct == CrateType::Rlib) &&
2068+
tcx.crate_types().iter().any(|ct| *ct == CrateType::Rlib) &&
20672069
// ThinLTO can't handle this workaround in all cases, so we don't
20682070
// emit the `__imp_` symbols. Instead we make them unnecessary by disallowing
20692071
// dynamic linking when linker plugin LTO is enabled.

compiler/rustc_codegen_ssa/src/base.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -779,18 +779,13 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
779779

780780
impl CrateInfo {
781781
pub fn new(tcx: TyCtxt<'_>, target_cpu: String) -> CrateInfo {
782-
let exported_symbols = tcx
783-
.sess
784-
.crate_types()
782+
let crate_types = tcx.crate_types().to_vec();
783+
let exported_symbols = crate_types
785784
.iter()
786785
.map(|&c| (c, crate::back::linker::exported_symbols(tcx, c)))
787786
.collect();
788-
let linked_symbols = tcx
789-
.sess
790-
.crate_types()
791-
.iter()
792-
.map(|&c| (c, crate::back::linker::linked_symbols(tcx, c)))
793-
.collect();
787+
let linked_symbols =
788+
crate_types.iter().map(|&c| (c, crate::back::linker::linked_symbols(tcx, c))).collect();
794789
let local_crate_name = tcx.crate_name(LOCAL_CRATE);
795790
let crate_attrs = tcx.hir().attrs(rustc_hir::CRATE_HIR_ID);
796791
let subsystem = attr::first_attr_value_str_by_name(crate_attrs, sym::windows_subsystem);
@@ -829,6 +824,7 @@ impl CrateInfo {
829824

830825
let mut info = CrateInfo {
831826
target_cpu,
827+
crate_types,
832828
exported_symbols,
833829
linked_symbols,
834830
local_crate_name,
@@ -916,7 +912,7 @@ impl CrateInfo {
916912
});
917913
}
918914

919-
let embed_visualizers = tcx.sess.crate_types().iter().any(|&crate_type| match crate_type {
915+
let embed_visualizers = tcx.crate_types().iter().any(|&crate_type| match crate_type {
920916
CrateType::Executable | CrateType::Dylib | CrateType::Cdylib => {
921917
// These are crate types for which we invoke the linker and can embed
922918
// NatVis visualizers.
@@ -1013,7 +1009,7 @@ fn determine_cgu_reuse<'tcx>(tcx: TyCtxt<'tcx>, cgu: &CodegenUnit<'tcx>) -> CguR
10131009
match compute_per_cgu_lto_type(
10141010
&tcx.sess.lto(),
10151011
&tcx.sess.opts,
1016-
&tcx.sess.crate_types(),
1012+
tcx.crate_types(),
10171013
ModuleKind::Regular,
10181014
) {
10191015
ComputedLtoType::No => CguReuse::PostLto,

compiler/rustc_codegen_ssa/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ impl From<&cstore::NativeLib> for NativeLib {
150150
#[derive(Debug, Encodable, Decodable)]
151151
pub struct CrateInfo {
152152
pub target_cpu: String,
153+
pub crate_types: Vec<CrateType>,
153154
pub exported_symbols: FxHashMap<CrateType, Vec<String>>,
154155
pub linked_symbols: FxHashMap<CrateType, Vec<(String, SymbolExportKind)>>,
155156
pub local_crate_name: Symbol,

compiler/rustc_driver_impl/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,6 @@ fn show_md_content_with_pager(content: &str, color: ColorConfig) {
653653
pub fn try_process_rlink(sess: &Session, compiler: &interface::Compiler) -> Compilation {
654654
if sess.opts.unstable_opts.link_only {
655655
if let Input::File(file) = &sess.io.input {
656-
// FIXME: #![crate_type] and #![crate_name] support not implemented yet
657-
sess.init_crate_types(collect_crate_types(sess, &[]));
658656
let outputs = compiler.build_output_filenames(sess, &[]);
659657
let rlink_data = fs::read(file).unwrap_or_else(|err| {
660658
sess.emit_fatal(RlinkUnableToRead { err });

compiler/rustc_interface/src/passes.rs

+10-5
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;
@@ -248,7 +248,7 @@ fn configure_and_expand(
248248
rustc_ast_passes::ast_validation::check_crate(sess, &krate, resolver.lint_buffer())
249249
});
250250

251-
let crate_types = sess.crate_types();
251+
let crate_types = tcx.crate_types();
252252
let is_executable_crate = crate_types.contains(&CrateType::Executable);
253253
let is_proc_macro_crate = crate_types.contains(&CrateType::ProcMacro);
254254

@@ -340,11 +340,12 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
340340

341341
// Returns all the paths that correspond to generated files.
342342
fn generated_output_paths(
343-
sess: &Session,
343+
tcx: TyCtxt<'_>,
344344
outputs: &OutputFilenames,
345345
exact_name: bool,
346346
crate_name: Symbol,
347347
) -> Vec<PathBuf> {
348+
let sess = tcx.sess;
348349
let mut out_filenames = Vec::new();
349350
for output_type in sess.opts.output_types.keys() {
350351
let out_filename = outputs.path(*output_type);
@@ -353,7 +354,7 @@ fn generated_output_paths(
353354
// If the filename has been overridden using `-o`, it will not be modified
354355
// by appending `.rlib`, `.exe`, etc., so we can skip this transformation.
355356
OutputType::Exe if !exact_name => {
356-
for crate_type in sess.crate_types().iter() {
357+
for crate_type in tcx.crate_types().iter() {
357358
let p = filename_for_input(sess, *crate_type, crate_name, outputs);
358359
out_filenames.push(p.as_path().to_path_buf());
359360
}
@@ -586,7 +587,7 @@ fn output_filenames(tcx: TyCtxt<'_>, (): ()) -> Arc<OutputFilenames> {
586587
let outputs = util::build_output_filenames(&krate.attrs, sess);
587588

588589
let output_paths =
589-
generated_output_paths(sess, &outputs, sess.io.output_file.is_some(), crate_name);
590+
generated_output_paths(tcx, &outputs, sess.io.output_file.is_some(), crate_name);
590591

591592
// Ensure the source file isn't accidentally overwritten during compilation.
592593
if let Some(ref input_path) = sess.io.input.opt_path() {
@@ -664,6 +665,8 @@ pub static DEFAULT_EXTERN_QUERY_PROVIDERS: LazyLock<ExternProviders> = LazyLock:
664665

665666
pub fn create_global_ctxt<'tcx>(
666667
compiler: &'tcx Compiler,
668+
crate_types: Vec<CrateType>,
669+
stable_crate_id: StableCrateId,
667670
lint_store: Lrc<LintStore>,
668671
dep_graph: DepGraph,
669672
untracked: Untracked,
@@ -696,6 +699,8 @@ pub fn create_global_ctxt<'tcx>(
696699
gcx_cell.get_or_init(move || {
697700
TyCtxt::create_global_ctxt(
698701
sess,
702+
crate_types,
703+
stable_crate_id,
699704
lint_store,
700705
arena,
701706
hir_arena,

compiler/rustc_interface/src/queries.rs

+4-4
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.init_crate_types(crate_types);
239-
sess.stable_crate_id.set(stable_crate_id).expect("not yet initialized");
237+
// FIXME: Move features from session to tcx and make them immutable.
240238
sess.init_features(rustc_expand::config::features(sess, &pre_configured_attrs));
241239

242240
let qcx = passes::create_global_ctxt(
243241
self.compiler,
242+
crate_types,
243+
stable_crate_id,
244244
lint_store,
245245
self.dep_graph(dep_graph_future),
246246
untracked,
@@ -320,7 +320,7 @@ impl<'tcx> Queries<'tcx> {
320320

321321
let (crate_hash, prepare_outputs, dep_graph) = self.global_ctxt()?.enter(|tcx| {
322322
(
323-
if tcx.sess.needs_crate_hash() { Some(tcx.crate_hash(LOCAL_CRATE)) } else { None },
323+
if tcx.needs_crate_hash() { Some(tcx.crate_hash(LOCAL_CRATE)) } else { None },
324324
tcx.output_filenames(()).clone(),
325325
tcx.dep_graph.clone(),
326326
)

compiler/rustc_metadata/src/creader.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
545545
name,
546546
// The all loop is because `--crate-type=rlib --crate-type=rlib` is
547547
// legal and produces both inside this type.
548-
self.sess.crate_types().iter().all(|c| *c == CrateType::Rlib),
548+
self.tcx.crate_types().iter().all(|c| *c == CrateType::Rlib),
549549
hash,
550550
extra_filename,
551551
false, // is_host
@@ -689,7 +689,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
689689
fn inject_panic_runtime(&mut self, krate: &ast::Crate) {
690690
// If we're only compiling an rlib, then there's no need to select a
691691
// panic runtime, so we just skip this section entirely.
692-
let any_non_rlib = self.sess.crate_types().iter().any(|ct| *ct != CrateType::Rlib);
692+
let any_non_rlib = self.tcx.crate_types().iter().any(|ct| *ct != CrateType::Rlib);
693693
if !any_non_rlib {
694694
info!("panic runtime injection skipped, only generating rlib");
695695
return;
@@ -818,7 +818,7 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
818818
// At this point we've determined that we need an allocator. Let's see
819819
// if our compilation session actually needs an allocator based on what
820820
// we're emitting.
821-
let all_rlib = self.sess.crate_types().iter().all(|ct| matches!(*ct, CrateType::Rlib));
821+
let all_rlib = self.tcx.crate_types().iter().all(|ct| matches!(*ct, CrateType::Rlib));
822822
if all_rlib {
823823
return;
824824
}

compiler/rustc_metadata/src/dependency_format.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ use rustc_session::cstore::CrateDepKind;
6666
use rustc_session::cstore::LinkagePreference::{self, RequireDynamic, RequireStatic};
6767

6868
pub(crate) fn calculate(tcx: TyCtxt<'_>) -> Dependencies {
69-
tcx.sess
70-
.crate_types()
69+
tcx.crate_types()
7170
.iter()
7271
.map(|&ty| {
7372
let linkage = calculate_type(tcx, ty);

compiler/rustc_metadata/src/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub fn encode_and_write_metadata(tcx: TyCtxt<'_>) -> (EncodedMetadata, bool) {
5656

5757
// Always create a file at `metadata_filename`, even if we have nothing to write to it.
5858
// This simplifies the creation of the output `out_filename` when requested.
59-
let metadata_kind = tcx.sess.metadata_kind();
59+
let metadata_kind = tcx.metadata_kind();
6060
match metadata_kind {
6161
MetadataKind::None => {
6262
std::fs::File::create(&metadata_filename).unwrap_or_else(|err| {

0 commit comments

Comments
 (0)