Skip to content

Commit 87bbbcd

Browse files
authored
Rollup merge of rust-lang#134251 - bjorn3:various_cleanups2, r=oli-obk
A bunch of cleanups (part 2) Just like rust-lang#133567 these were all found while looking at the respective code, but are not blocking any other changes I want to make in the short term.
2 parents 34e6075 + 3198496 commit 87bbbcd

File tree

18 files changed

+43
-71
lines changed

18 files changed

+43
-71
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -3507,7 +3507,6 @@ dependencies = [
35073507
"cc",
35083508
"either",
35093509
"itertools",
3510-
"jobserver",
35113510
"libc",
35123511
"object 0.36.5",
35133512
"pathdiff",

compiler/rustc_codegen_cranelift/src/concurrency_limiter.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use std::sync::{Arc, Condvar, Mutex};
22

3-
use jobserver::HelperThread;
3+
use rustc_data_structures::jobserver::{self, HelperThread};
44
use rustc_errors::DiagCtxtHandle;
5-
use rustc_session::Session;
65

76
// FIXME don't panic when a worker thread panics
87

@@ -14,14 +13,13 @@ pub(super) struct ConcurrencyLimiter {
1413
}
1514

1615
impl ConcurrencyLimiter {
17-
pub(super) fn new(sess: &Session, pending_jobs: usize) -> Self {
16+
pub(super) fn new(pending_jobs: usize) -> Self {
1817
let state = Arc::new(Mutex::new(state::ConcurrencyLimiterState::new(pending_jobs)));
1918
let available_token_condvar = Arc::new(Condvar::new());
2019

2120
let state_helper = state.clone();
2221
let available_token_condvar_helper = available_token_condvar.clone();
23-
let helper_thread = sess
24-
.jobserver
22+
let helper_thread = jobserver::client()
2523
.clone()
2624
.into_helper_thread(move |token| {
2725
let mut state = state_helper.lock().unwrap();
@@ -113,7 +111,7 @@ impl Drop for ConcurrencyLimiterToken {
113111
}
114112

115113
mod state {
116-
use jobserver::Acquired;
114+
use rustc_data_structures::jobserver::Acquired;
117115

118116
#[derive(Debug)]
119117
pub(super) struct ConcurrencyLimiterState {

compiler/rustc_codegen_cranelift/src/driver/aot.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ pub(crate) fn run_aot(
679679
metadata_module: None,
680680
metadata,
681681
crate_info: CrateInfo::new(tcx, target_cpu),
682-
concurrency_limiter: ConcurrencyLimiter::new(tcx.sess, 0),
682+
concurrency_limiter: ConcurrencyLimiter::new(0),
683683
});
684684
};
685685

@@ -711,7 +711,7 @@ pub(crate) fn run_aot(
711711
CguReuse::PreLto | CguReuse::PostLto => false,
712712
});
713713

714-
let concurrency_limiter = IntoDynSyncSend(ConcurrencyLimiter::new(tcx.sess, todo_cgus.len()));
714+
let concurrency_limiter = IntoDynSyncSend(ConcurrencyLimiter::new(todo_cgus.len()));
715715

716716
let modules = tcx.sess.time("codegen mono items", || {
717717
let mut modules: Vec<_> = par_map(todo_cgus, |(_, cgu)| {

compiler/rustc_codegen_cranelift/src/driver/jit.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,7 @@ fn dep_symbol_lookup_fn(
287287

288288
let mut dylib_paths = Vec::new();
289289

290-
let data = &crate_info
291-
.dependency_formats
292-
.iter()
293-
.find(|(crate_type, _data)| *crate_type == rustc_session::config::CrateType::Executable)
294-
.unwrap()
295-
.1;
290+
let data = &crate_info.dependency_formats[&rustc_session::config::CrateType::Executable].1;
296291
// `used_crates` is in reverse postorder in terms of dependencies. Reverse the order here to
297292
// get a postorder which ensures that all dependencies of a dylib are loaded before the dylib
298293
// itself. This helps the dynamic linker to find dylibs not in the regular dynamic library

compiler/rustc_codegen_cranelift/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#![warn(unused_lifetimes)]
1313
// tidy-alphabetical-end
1414

15-
extern crate jobserver;
1615
#[macro_use]
1716
extern crate rustc_middle;
1817
extern crate rustc_abi;

compiler/rustc_codegen_ssa/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ bitflags = "2.4.1"
1111
cc = "1.1.23"
1212
either = "1.5.0"
1313
itertools = "0.12"
14-
jobserver = "0.1.28"
1514
pathdiff = "0.2.0"
1615
regex = "1.4"
1716
rustc_abi = { path = "../rustc_abi" }

compiler/rustc_codegen_ssa/src/back/link.rs

+13-21
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,13 @@ pub fn each_linked_rlib(
236236
) -> Result<(), errors::LinkRlibError> {
237237
let crates = info.used_crates.iter();
238238

239-
let fmts = if crate_type.is_none() {
239+
let fmts = if let Some(crate_type) = crate_type {
240+
let Some(fmts) = info.dependency_formats.get(&crate_type) else {
241+
return Err(errors::LinkRlibError::MissingFormat);
242+
};
243+
244+
fmts
245+
} else {
240246
for combination in info.dependency_formats.iter().combinations(2) {
241247
let (ty1, list1) = &combination[0];
242248
let (ty2, list2) = &combination[1];
@@ -252,18 +258,7 @@ pub fn each_linked_rlib(
252258
if info.dependency_formats.is_empty() {
253259
return Err(errors::LinkRlibError::MissingFormat);
254260
}
255-
&info.dependency_formats[0].1
256-
} else {
257-
let fmts = info
258-
.dependency_formats
259-
.iter()
260-
.find_map(|&(ty, ref list)| if Some(ty) == crate_type { Some(list) } else { None });
261-
262-
let Some(fmts) = fmts else {
263-
return Err(errors::LinkRlibError::MissingFormat);
264-
};
265-
266-
fmts
261+
info.dependency_formats.first().unwrap().1
267262
};
268263

269264
for &cnum in crates {
@@ -624,8 +619,7 @@ fn link_staticlib(
624619
let fmts = codegen_results
625620
.crate_info
626621
.dependency_formats
627-
.iter()
628-
.find_map(|&(ty, ref list)| if ty == CrateType::Staticlib { Some(list) } else { None })
622+
.get(&CrateType::Staticlib)
629623
.expect("no dependency formats for staticlib");
630624

631625
let mut all_rust_dylibs = vec![];
@@ -2355,11 +2349,10 @@ fn linker_with_args(
23552349
// they are used within inlined functions or instantiated generic functions. We do this *after*
23562350
// handling the raw-dylib symbols in the current crate to make sure that those are chosen first
23572351
// by the linker.
2358-
let (_, dependency_linkage) = codegen_results
2352+
let dependency_linkage = codegen_results
23592353
.crate_info
23602354
.dependency_formats
2361-
.iter()
2362-
.find(|(ty, _)| *ty == crate_type)
2355+
.get(&crate_type)
23632356
.expect("failed to find crate type in dependency format list");
23642357

23652358
// We sort the libraries below
@@ -2738,11 +2731,10 @@ fn add_upstream_rust_crates(
27382731
// Linking to a rlib involves just passing it to the linker (the linker
27392732
// will slurp up the object files inside), and linking to a dynamic library
27402733
// involves just passing the right -l flag.
2741-
let (_, data) = codegen_results
2734+
let data = codegen_results
27422735
.crate_info
27432736
.dependency_formats
2744-
.iter()
2745-
.find(|(ty, _)| *ty == crate_type)
2737+
.get(&crate_type)
27462738
.expect("failed to find crate type in dependency format list");
27472739

27482740
if sess.target.is_like_aix {

compiler/rustc_codegen_ssa/src/back/linker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1749,7 +1749,7 @@ fn for_each_exported_symbols_include_dep<'tcx>(
17491749
}
17501750

17511751
let formats = tcx.dependency_formats(());
1752-
let deps = formats.iter().find_map(|(t, list)| (*t == crate_type).then_some(list)).unwrap();
1752+
let deps = &formats[&crate_type];
17531753

17541754
for (index, dep_format) in deps.iter().enumerate() {
17551755
let cnum = CrateNum::new(index + 1);

compiler/rustc_codegen_ssa/src/back/write.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use std::sync::Arc;
66
use std::sync::mpsc::{Receiver, Sender, channel};
77
use std::{fs, io, mem, str, thread};
88

9-
use jobserver::{Acquired, Client};
109
use rustc_ast::attr;
1110
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
11+
use rustc_data_structures::jobserver::{self, Acquired};
1212
use rustc_data_structures::memmap::Mmap;
1313
use rustc_data_structures::profiling::{SelfProfilerRef, VerboseTimingGuard};
1414
use rustc_errors::emitter::Emitter;
@@ -456,7 +456,6 @@ pub(crate) fn start_async_codegen<B: ExtraBackendMethods>(
456456
metadata_module: Option<CompiledModule>,
457457
) -> OngoingCodegen<B> {
458458
let (coordinator_send, coordinator_receive) = channel();
459-
let sess = tcx.sess;
460459

461460
let crate_attrs = tcx.hir().attrs(rustc_hir::CRATE_HIR_ID);
462461
let no_builtins = attr::contains_name(crate_attrs, sym::no_builtins);
@@ -477,7 +476,6 @@ pub(crate) fn start_async_codegen<B: ExtraBackendMethods>(
477476
shared_emitter,
478477
codegen_worker_send,
479478
coordinator_receive,
480-
sess.jobserver.clone(),
481479
Arc::new(regular_config),
482480
Arc::new(metadata_config),
483481
Arc::new(allocator_config),
@@ -1093,7 +1091,6 @@ fn start_executing_work<B: ExtraBackendMethods>(
10931091
shared_emitter: SharedEmitter,
10941092
codegen_worker_send: Sender<CguMessage>,
10951093
coordinator_receive: Receiver<Box<dyn Any + Send>>,
1096-
jobserver: Client,
10971094
regular_config: Arc<ModuleConfig>,
10981095
metadata_config: Arc<ModuleConfig>,
10991096
allocator_config: Arc<ModuleConfig>,
@@ -1145,7 +1142,7 @@ fn start_executing_work<B: ExtraBackendMethods>(
11451142
// get tokens on `coordinator_receive` which will
11461143
// get managed in the main loop below.
11471144
let coordinator_send2 = coordinator_send.clone();
1148-
let helper = jobserver
1145+
let helper = jobserver::client()
11491146
.into_helper_thread(move |token| {
11501147
drop(coordinator_send2.send(Box::new(Message::Token::<B>(token))));
11511148
})

compiler/rustc_data_structures/src/jobserver.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::sync::{LazyLock, OnceLock};
22

3-
pub use jobserver_crate::Client;
3+
pub use jobserver_crate::{Acquired, Client, HelperThread};
44
use jobserver_crate::{FromEnv, FromEnvErrorKind};
55

66
// We can only call `from_env_ext` once per process

compiler/rustc_driver_impl/src/lib.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,8 @@ fn run_compiler(
347347

348348
callbacks.config(&mut config);
349349

350+
let registered_lints = config.register_lints.is_some();
351+
350352
interface::run_compiler(config, |compiler| {
351353
let sess = &compiler.sess;
352354
let codegen_backend = &*compiler.codegen_backend;
@@ -362,7 +364,7 @@ fn run_compiler(
362364
// `--help`/`-Zhelp`/`-Chelp`. This is the earliest it can run, because
363365
// it must happen after lints are registered, during session creation.
364366
if sess.opts.describe_lints {
365-
describe_lints(sess);
367+
describe_lints(sess, registered_lints);
366368
return early_exit();
367369
}
368370

@@ -980,7 +982,7 @@ the command line flag directly.
980982
}
981983

982984
/// Write to stdout lint command options, together with a list of all available lints
983-
pub fn describe_lints(sess: &Session) {
985+
pub fn describe_lints(sess: &Session, registered_lints: bool) {
984986
safe_println!(
985987
"
986988
Available lint options:
@@ -1084,7 +1086,7 @@ Available lint options:
10841086

10851087
print_lint_groups(builtin_groups, true);
10861088

1087-
match (sess.registered_lints, loaded.len(), loaded_groups.len()) {
1089+
match (registered_lints, loaded.len(), loaded_groups.len()) {
10881090
(false, 0, _) | (false, _, 0) => {
10891091
safe_println!("Lint tools like Clippy can load additional lints and lint groups.");
10901092
}

compiler/rustc_interface/src/interface.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,6 @@ pub(crate) fn initialize_checked_jobserver(early_dcx: &EarlyDiagCtxt) {
371371

372372
// JUSTIFICATION: before session exists, only config
373373
#[allow(rustc::bad_opt_access)]
374-
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
375374
pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
376375
trace!("run_compiler");
377376

@@ -425,7 +424,11 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
425424
config.opts.unstable_opts.translate_directionality_markers,
426425
) {
427426
Ok(bundle) => bundle,
428-
Err(e) => early_dcx.early_fatal(format!("failed to load fluent bundle: {e}")),
427+
Err(e) => {
428+
// We can't translate anything if we failed to load translations
429+
#[allow(rustc::untranslatable_diagnostic)]
430+
early_dcx.early_fatal(format!("failed to load fluent bundle: {e}"))
431+
}
429432
};
430433

431434
let mut locale_resources = config.locale_resources;
@@ -479,7 +482,6 @@ pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Se
479482
let mut lint_store = rustc_lint::new_lint_store(sess.enable_internal_lints());
480483
if let Some(register_lints) = config.register_lints.as_deref() {
481484
register_lints(&sess, &mut lint_store);
482-
sess.registered_lints = true;
483485
}
484486
sess.lint_store = Some(Lrc::new(lint_store));
485487

compiler/rustc_metadata/src/dependency_format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub(crate) fn calculate(tcx: TyCtxt<'_>) -> Dependencies {
7777
verify_ok(tcx, &linkage);
7878
(ty, linkage)
7979
})
80-
.collect::<Vec<_>>()
80+
.collect()
8181
}
8282

8383
fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {

compiler/rustc_metadata/src/rmeta/encoder.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -2161,10 +2161,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
21612161
fn encode_dylib_dependency_formats(&mut self) -> LazyArray<Option<LinkagePreference>> {
21622162
empty_proc_macro!(self);
21632163
let formats = self.tcx.dependency_formats(());
2164-
for (ty, arr) in formats.iter() {
2165-
if *ty != CrateType::Dylib {
2166-
continue;
2167-
}
2164+
if let Some(arr) = formats.get(&CrateType::Dylib) {
21682165
return self.lazy_array(arr.iter().map(|slot| match *slot {
21692166
Linkage::NotLinked | Linkage::IncludedFromDylib => None,
21702167

compiler/rustc_middle/src/middle/dependency_format.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// FIXME: move this file to rustc_metadata::dependency_format, but
88
// this will introduce circular dependency between rustc_metadata and rustc_middle
99

10+
use rustc_data_structures::fx::FxIndexMap;
1011
use rustc_macros::{Decodable, Encodable, HashStable};
1112
use rustc_session::config::CrateType;
1213

@@ -18,7 +19,7 @@ pub type DependencyList = Vec<Linkage>;
1819
/// A mapping of all required dependencies for a particular flavor of output.
1920
///
2021
/// This is local to the tcx, and is generally relevant to one session.
21-
pub type Dependencies = Vec<(CrateType, DependencyList)>;
22+
pub type Dependencies = FxIndexMap<CrateType, DependencyList>;
2223

2324
#[derive(Copy, Clone, PartialEq, Debug, HashStable, Encodable, Decodable)]
2425
pub enum Linkage {

compiler/rustc_session/src/session.rs

-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use std::{env, fmt, io};
88

99
use rustc_data_structures::flock;
1010
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
11-
use rustc_data_structures::jobserver::{self, Client};
1211
use rustc_data_structures::profiling::{SelfProfiler, SelfProfilerRef};
1312
use rustc_data_structures::sync::{
1413
DynSend, DynSync, Lock, Lrc, MappedReadGuard, ReadGuard, RwLock,
@@ -154,16 +153,9 @@ pub struct Session {
154153
/// Data about code being compiled, gathered during compilation.
155154
pub code_stats: CodeStats,
156155

157-
/// Loaded up early on in the initialization of this `Session` to avoid
158-
/// false positives about a job server in our environment.
159-
pub jobserver: Client,
160-
161156
/// This only ever stores a `LintStore` but we don't want a dependency on that type here.
162157
pub lint_store: Option<Lrc<dyn LintStoreMarker>>,
163158

164-
/// Should be set if any lints are registered in `lint_store`.
165-
pub registered_lints: bool,
166-
167159
/// Cap lint level specified by a driver specifically.
168160
pub driver_lint_caps: FxHashMap<lint::LintId, lint::Level>,
169161

@@ -1072,9 +1064,7 @@ pub fn build_session(
10721064
incr_comp_session: RwLock::new(IncrCompSession::NotInitialized),
10731065
prof,
10741066
code_stats: Default::default(),
1075-
jobserver: jobserver::client(),
10761067
lint_store: None,
1077-
registered_lints: false,
10781068
driver_lint_caps,
10791069
ctfe_backtrace,
10801070
miri_unleashed_features: Lock::new(Default::default()),

src/librustdoc/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -846,11 +846,13 @@ fn main_args(
846846

847847
let config = core::create_config(input, options, &render_options, using_internal_features);
848848

849+
let registered_lints = config.register_lints.is_some();
850+
849851
interface::run_compiler(config, |compiler| {
850852
let sess = &compiler.sess;
851853

852854
if sess.opts.describe_lints {
853-
rustc_driver::describe_lints(sess);
855+
rustc_driver::describe_lints(sess, registered_lints);
854856
return;
855857
}
856858

src/tools/miri/src/helpers.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,9 @@ pub fn iter_exported_symbols<'tcx>(
149149
let dependency_formats = tcx.dependency_formats(());
150150
// Find the dependencies of the executable we are running.
151151
let dependency_format = dependency_formats
152-
.iter()
153-
.find(|(crate_type, _)| *crate_type == CrateType::Executable)
152+
.get(&CrateType::Executable)
154153
.expect("interpreting a non-executable crate");
155-
for cnum in dependency_format.1.iter().enumerate().filter_map(|(num, &linkage)| {
154+
for cnum in dependency_format.iter().enumerate().filter_map(|(num, &linkage)| {
156155
// We add 1 to the number because that's what rustc also does everywhere it
157156
// calls `CrateNum::new`...
158157
#[expect(clippy::arithmetic_side_effects)]

0 commit comments

Comments
 (0)