Skip to content

Commit 3198496

Browse files
committed
Make dependency_formats an FxIndexMap rather than a list of tuples
It is treated as a map already. This is using FxIndexMap rather than UnordMap because the latter doesn't provide an api to pick a single value iff all values are equal, which each_linked_rlib depends on.
1 parent ea9e8c1 commit 3198496

File tree

7 files changed

+21
-37
lines changed

7 files changed

+21
-37
lines changed

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_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_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
@@ -2164,10 +2164,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
21642164
fn encode_dylib_dependency_formats(&mut self) -> LazyArray<Option<LinkagePreference>> {
21652165
empty_proc_macro!(self);
21662166
let formats = self.tcx.dependency_formats(());
2167-
for (ty, arr) in formats.iter() {
2168-
if *ty != CrateType::Dylib {
2169-
continue;
2170-
}
2167+
if let Some(arr) = formats.get(&CrateType::Dylib) {
21712168
return self.lazy_array(arr.iter().map(|slot| match *slot {
21722169
Linkage::NotLinked | Linkage::IncludedFromDylib => None,
21732170

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 {

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)