Skip to content

Commit d2b04f0

Browse files
committed
Auto merge of rust-lang#86105 - bjorn3:link_info_refactor, r=petrochenkov
Refactor the generation of the metadata for linking
2 parents c5e344f + 2977dff commit d2b04f0

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

compiler/rustc_codegen_ssa/src/base.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1919
use rustc_hir::lang_items::LangItem;
2020
use rustc_index::vec::Idx;
2121
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
22-
use rustc_middle::middle::cstore::{self, EncodedMetadata};
22+
use rustc_middle::middle::cstore::EncodedMetadata;
2323
use rustc_middle::middle::lang_items;
2424
use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem};
2525
use rustc_middle::ty::layout::{HasTyCtxt, TyAndLayout};
@@ -775,6 +775,22 @@ impl CrateInfo {
775775
subsystem.to_string()
776776
});
777777

778+
// This list is used when generating the command line to pass through to
779+
// system linker. The linker expects undefined symbols on the left of the
780+
// command line to be defined in libraries on the right, not the other way
781+
// around. For more info, see some comments in the add_used_library function
782+
// below.
783+
//
784+
// In order to get this left-to-right dependency ordering, we use the reverse
785+
// postorder of all crates putting the leaves at the right-most positions.
786+
let used_crates = tcx
787+
.postorder_cnums(())
788+
.iter()
789+
.rev()
790+
.copied()
791+
.filter(|&cnum| !tcx.dep_kind(cnum).macros_only())
792+
.collect();
793+
778794
let mut info = CrateInfo {
779795
target_cpu,
780796
exported_symbols,
@@ -785,7 +801,7 @@ impl CrateInfo {
785801
native_libraries: Default::default(),
786802
used_libraries: tcx.native_libraries(LOCAL_CRATE).iter().map(Into::into).collect(),
787803
crate_name: Default::default(),
788-
used_crates: cstore::used_crates(tcx),
804+
used_crates,
789805
used_crate_source: Default::default(),
790806
lang_item_to_crate: Default::default(),
791807
missing_lang_items: Default::default(),

compiler/rustc_middle/src/middle/cstore.rs

-27
Original file line numberDiff line numberDiff line change
@@ -197,30 +197,3 @@ pub trait CrateStore {
197197
}
198198

199199
pub type CrateStoreDyn = dyn CrateStore + sync::Sync;
200-
201-
// This method is used when generating the command line to pass through to
202-
// system linker. The linker expects undefined symbols on the left of the
203-
// command line to be defined in libraries on the right, not the other way
204-
// around. For more info, see some comments in the add_used_library function
205-
// below.
206-
//
207-
// In order to get this left-to-right dependency ordering, we perform a
208-
// topological sort of all crates putting the leaves at the right-most
209-
// positions.
210-
pub fn used_crates(tcx: TyCtxt<'_>) -> Vec<CrateNum> {
211-
let mut libs = tcx
212-
.crates(())
213-
.iter()
214-
.cloned()
215-
.filter_map(|cnum| {
216-
if tcx.dep_kind(cnum).macros_only() {
217-
return None;
218-
}
219-
Some(cnum)
220-
})
221-
.collect::<Vec<_>>();
222-
let mut ordering = tcx.postorder_cnums(()).to_owned();
223-
ordering.reverse();
224-
libs.sort_by_cached_key(|&a| ordering.iter().position(|x| *x == a));
225-
libs
226-
}

0 commit comments

Comments
 (0)