Skip to content

Commit 7910391

Browse files
authored
Rollup merge of #150014 - bjorn3:metadata_loader_cleanups, r=jieyouxu
Metadata loader cleanups Couple of cleanups I found while working on #149273 This renames some fields and enum variants to clarify what they are used for, moves a check to another method and slightly simplifies the way profiler_builtins is linked.
2 parents 6ac2bdb + 0e1e72a commit 7910391

File tree

6 files changed

+65
-64
lines changed

6 files changed

+65
-64
lines changed

compiler/rustc_codegen_ssa/src/back/link.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,11 +2792,9 @@ fn add_upstream_rust_crates(
27922792
// We must always link crates `compiler_builtins` and `profiler_builtins` statically.
27932793
// Even if they were already included into a dylib
27942794
// (e.g. `libstd` when `-C prefer-dynamic` is used).
2795-
// FIXME: `dependency_formats` can report `profiler_builtins` as `NotLinked` for some
2796-
// reason, it shouldn't do that because `profiler_builtins` should indeed be linked.
27972795
let linkage = data[cnum];
27982796
let link_static_crate = linkage == Linkage::Static
2799-
|| (linkage == Linkage::IncludedFromDylib || linkage == Linkage::NotLinked)
2797+
|| linkage == Linkage::IncludedFromDylib
28002798
&& (codegen_results.crate_info.compiler_builtins == Some(cnum)
28012799
|| codegen_results.crate_info.profiler_runtime == Some(cnum));
28022800

compiler/rustc_metadata/src/creader.rs

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ impl<'a> std::fmt::Debug for CrateDump<'a> {
156156
enum CrateOrigin<'a> {
157157
/// This crate was a dependency of another crate.
158158
IndirectDependency {
159-
/// Where this dependency was included from.
160-
dep_root: &'a CratePaths,
159+
/// Where this dependency was included from. Should only be used in error messages.
160+
dep_root_for_errors: &'a CratePaths,
161161
/// True if the parent is private, meaning the dependent should also be private.
162162
parent_private: bool,
163163
/// Dependency info about this crate.
@@ -171,9 +171,11 @@ enum CrateOrigin<'a> {
171171

172172
impl<'a> CrateOrigin<'a> {
173173
/// Return the dependency root, if any.
174-
fn dep_root(&self) -> Option<&'a CratePaths> {
174+
fn dep_root_for_errors(&self) -> Option<&'a CratePaths> {
175175
match self {
176-
CrateOrigin::IndirectDependency { dep_root, .. } => Some(dep_root),
176+
CrateOrigin::IndirectDependency { dep_root_for_errors, .. } => {
177+
Some(dep_root_for_errors)
178+
}
177179
_ => None,
178180
}
179181
}
@@ -193,6 +195,7 @@ impl<'a> CrateOrigin<'a> {
193195
CrateOrigin::IndirectDependency { parent_private, dep, .. } => {
194196
Some(dep.is_private || *parent_private)
195197
}
198+
CrateOrigin::Injected => Some(true),
196199
_ => None,
197200
}
198201
}
@@ -544,17 +547,7 @@ impl CStore {
544547
/// Sometimes the directly dependent crate is not specified by `--extern`, in this case,
545548
/// `private-dep` is none during loading. This is equivalent to the scenario where the
546549
/// command parameter is set to `public-dependency`
547-
fn is_private_dep(
548-
&self,
549-
externs: &Externs,
550-
name: Symbol,
551-
private_dep: Option<bool>,
552-
origin: CrateOrigin<'_>,
553-
) -> bool {
554-
if matches!(origin, CrateOrigin::Injected) {
555-
return true;
556-
}
557-
550+
fn is_private_dep(&self, externs: &Externs, name: Symbol, private_dep: Option<bool>) -> bool {
558551
let extern_private = externs.get(name.as_str()).map(|e| e.is_private_dep);
559552
match (extern_private, private_dep) {
560553
// Explicit non-private via `--extern`, explicit non-private from metadata, or
@@ -581,7 +574,7 @@ impl CStore {
581574
let Library { source, metadata } = lib;
582575
let crate_root = metadata.get_root();
583576
let host_hash = host_lib.as_ref().map(|lib| lib.metadata.get_root().hash());
584-
let private_dep = self.is_private_dep(&tcx.sess.opts.externs, name, private_dep, origin);
577+
let private_dep = self.is_private_dep(&tcx.sess.opts.externs, name, private_dep);
585578

586579
// Claim this crate number and cache it
587580
let feed = self.intern_stable_crate_id(tcx, &crate_root)?;
@@ -597,16 +590,16 @@ impl CStore {
597590
// Maintain a reference to the top most crate.
598591
// Stash paths for top-most crate locally if necessary.
599592
let crate_paths;
600-
let dep_root = if let Some(dep_root) = origin.dep_root() {
601-
dep_root
593+
let dep_root_for_errors = if let Some(dep_root_for_errors) = origin.dep_root_for_errors() {
594+
dep_root_for_errors
602595
} else {
603596
crate_paths = CratePaths::new(crate_root.name(), source.clone());
604597
&crate_paths
605598
};
606599

607600
let cnum_map = self.resolve_crate_deps(
608601
tcx,
609-
dep_root,
602+
dep_root_for_errors,
610603
&crate_root,
611604
&metadata,
612605
cnum,
@@ -726,7 +719,7 @@ impl CStore {
726719
self.used_extern_options.insert(name);
727720
match self.maybe_resolve_crate(tcx, name, dep_kind, origin) {
728721
Ok(cnum) => {
729-
self.set_used_recursively(tcx, cnum);
722+
self.set_used_recursively(cnum);
730723
Some(cnum)
731724
}
732725
Err(err) => {
@@ -735,7 +728,7 @@ impl CStore {
735728
.maybe_resolve_crate(
736729
tcx,
737730
sym::core,
738-
CrateDepKind::Explicit,
731+
CrateDepKind::Unconditional,
739732
CrateOrigin::Extern,
740733
)
741734
.is_err();
@@ -757,7 +750,7 @@ impl CStore {
757750
return Err(CrateError::NonAsciiName(name));
758751
}
759752

760-
let dep_root = origin.dep_root();
753+
let dep_root_for_errors = origin.dep_root_for_errors();
761754
let dep = origin.dep();
762755
let hash = dep.map(|d| d.hash);
763756
let host_hash = dep.map(|d| d.host_hash).flatten();
@@ -795,7 +788,11 @@ impl CStore {
795788
host_hash,
796789
)? {
797790
Some(res) => res,
798-
None => return Err(locator.into_error(crate_rejections, dep_root.cloned())),
791+
None => {
792+
return Err(
793+
locator.into_error(crate_rejections, dep_root_for_errors.cloned())
794+
);
795+
}
799796
}
800797
}
801798
}
@@ -808,8 +805,7 @@ impl CStore {
808805
// not specified by `--extern` on command line parameters, it may be
809806
// `private-dependency` when `register_crate` is called for the first time. Then it must be updated to
810807
// `public-dependency` here.
811-
let private_dep =
812-
self.is_private_dep(&tcx.sess.opts.externs, name, private_dep, origin);
808+
let private_dep = self.is_private_dep(&tcx.sess.opts.externs, name, private_dep);
813809
let data = self.get_crate_data_mut(cnum);
814810
if data.is_proc_macro_crate() {
815811
dep_kind = CrateDepKind::MacrosOnly;
@@ -856,7 +852,7 @@ impl CStore {
856852
fn resolve_crate_deps(
857853
&mut self,
858854
tcx: TyCtxt<'_>,
859-
dep_root: &CratePaths,
855+
dep_root_for_errors: &CratePaths,
860856
crate_root: &CrateRoot,
861857
metadata: &MetadataBlob,
862858
krate: CrateNum,
@@ -866,7 +862,7 @@ impl CStore {
866862
debug!(
867863
"resolving deps of external crate `{}` with dep root `{}`",
868864
crate_root.name(),
869-
dep_root.name
865+
dep_root_for_errors.name
870866
);
871867
if crate_root.is_proc_macro_crate() {
872868
return Ok(CrateNumMap::new());
@@ -896,7 +892,7 @@ impl CStore {
896892
dep.name,
897893
dep_kind,
898894
CrateOrigin::IndirectDependency {
899-
dep_root,
895+
dep_root_for_errors,
900896
parent_private: parent_is_private,
901897
dep: &dep,
902898
},
@@ -979,9 +975,15 @@ impl CStore {
979975
};
980976
info!("panic runtime not found -- loading {}", name);
981977

982-
let Some(cnum) =
983-
self.resolve_crate(tcx, name, DUMMY_SP, CrateDepKind::Implicit, CrateOrigin::Injected)
984-
else {
978+
// This has to be conditional as both panic_unwind and panic_abort may be present in the
979+
// crate graph at the same time. One of them will later be activated in dependency_formats.
980+
let Some(cnum) = self.resolve_crate(
981+
tcx,
982+
name,
983+
DUMMY_SP,
984+
CrateDepKind::Conditional,
985+
CrateOrigin::Injected,
986+
) else {
985987
return;
986988
};
987989
let data = self.get_crate_data(cnum);
@@ -1009,9 +1011,13 @@ impl CStore {
10091011
info!("loading profiler");
10101012

10111013
let name = Symbol::intern(&tcx.sess.opts.unstable_opts.profiler_runtime);
1012-
let Some(cnum) =
1013-
self.resolve_crate(tcx, name, DUMMY_SP, CrateDepKind::Implicit, CrateOrigin::Injected)
1014-
else {
1014+
let Some(cnum) = self.resolve_crate(
1015+
tcx,
1016+
name,
1017+
DUMMY_SP,
1018+
CrateDepKind::Unconditional,
1019+
CrateOrigin::Injected,
1020+
) else {
10151021
return;
10161022
};
10171023
let data = self.get_crate_data(cnum);
@@ -1131,7 +1137,7 @@ impl CStore {
11311137
tcx,
11321138
name_interned,
11331139
DUMMY_SP,
1134-
CrateDepKind::Explicit,
1140+
CrateDepKind::Unconditional,
11351141
CrateOrigin::Extern,
11361142
);
11371143
}
@@ -1163,7 +1169,7 @@ impl CStore {
11631169
tcx,
11641170
sym::compiler_builtins,
11651171
krate.spans.inner_span.shrink_to_lo(),
1166-
CrateDepKind::Explicit,
1172+
CrateDepKind::Unconditional,
11671173
CrateOrigin::Injected,
11681174
) else {
11691175
info!("`compiler_builtins` not resolved");
@@ -1280,7 +1286,7 @@ impl CStore {
12801286
let dep_kind = if attr::contains_name(&item.attrs, sym::no_link) {
12811287
CrateDepKind::MacrosOnly
12821288
} else {
1283-
CrateDepKind::Explicit
1289+
CrateDepKind::Unconditional
12841290
};
12851291

12861292
let cnum =
@@ -1310,7 +1316,7 @@ impl CStore {
13101316
span: Span,
13111317
) -> Option<CrateNum> {
13121318
let cnum =
1313-
self.resolve_crate(tcx, name, span, CrateDepKind::Explicit, CrateOrigin::Extern)?;
1319+
self.resolve_crate(tcx, name, span, CrateDepKind::Unconditional, CrateOrigin::Extern)?;
13141320

13151321
self.update_extern_crate(
13161322
cnum,
@@ -1328,7 +1334,7 @@ impl CStore {
13281334
}
13291335

13301336
pub fn maybe_process_path_extern(&mut self, tcx: TyCtxt<'_>, name: Symbol) -> Option<CrateNum> {
1331-
self.maybe_resolve_crate(tcx, name, CrateDepKind::Explicit, CrateOrigin::Extern).ok()
1337+
self.maybe_resolve_crate(tcx, name, CrateDepKind::Unconditional, CrateOrigin::Extern).ok()
13321338
}
13331339
}
13341340

compiler/rustc_metadata/src/dependency_format.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
242242
let src = tcx.used_crate_source(cnum);
243243
if src.dylib.is_none()
244244
&& !formats.contains_key(&cnum)
245-
&& tcx.dep_kind(cnum) == CrateDepKind::Explicit
245+
&& tcx.dep_kind(cnum) == CrateDepKind::Unconditional
246246
{
247247
assert!(src.rlib.is_some() || src.rmeta.is_some());
248248
info!("adding staticlib: {}", tcx.crate_name(cnum));
@@ -355,8 +355,8 @@ fn attempt_static(tcx: TyCtxt<'_>, unavailable: &mut Vec<CrateNum>) -> Option<De
355355
for &cnum in tcx.crates(()) {
356356
assert_eq!(
357357
ret.push(match tcx.dep_kind(cnum) {
358-
CrateDepKind::Explicit => Linkage::Static,
359-
CrateDepKind::MacrosOnly | CrateDepKind::Implicit => Linkage::NotLinked,
358+
CrateDepKind::Unconditional => Linkage::Static,
359+
CrateDepKind::MacrosOnly | CrateDepKind::Conditional => Linkage::NotLinked,
360360
}),
361361
cnum
362362
);

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,6 @@ pub(crate) struct CrateMetadata {
116116
/// Maps crate IDs as they are were seen from this crate's compilation sessions into
117117
/// IDs as they are seen from the current compilation session.
118118
cnum_map: CrateNumMap,
119-
/// Same ID set as `cnum_map` plus maybe some injected crates like panic runtime.
120-
dependencies: Vec<CrateNum>,
121119
/// How to link (or not link) this crate to the currently compiled crate.
122120
dep_kind: CrateDepKind,
123121
/// Filesystem location of this crate.
@@ -1897,7 +1895,6 @@ impl CrateMetadata {
18971895
.collect();
18981896
let alloc_decoding_state =
18991897
AllocDecodingState::new(root.interpret_alloc_index.decode(&blob).collect());
1900-
let dependencies = cnum_map.iter().copied().collect();
19011898

19021899
// Pre-decode the DefPathHash->DefIndex table. This is a cheap operation
19031900
// that does not copy any data. It just does some data verification.
@@ -1915,7 +1912,6 @@ impl CrateMetadata {
19151912
alloc_decoding_state,
19161913
cnum,
19171914
cnum_map,
1918-
dependencies,
19191915
dep_kind,
19201916
source: Arc::new(source),
19211917
private_dep,
@@ -1941,7 +1937,7 @@ impl CrateMetadata {
19411937
}
19421938

19431939
pub(crate) fn dependencies(&self) -> impl Iterator<Item = CrateNum> {
1944-
self.dependencies.iter().copied()
1940+
self.cnum_map.iter().copied()
19451941
}
19461942

19471943
pub(crate) fn target_modifiers(&self) -> TargetModifiers {

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -623,15 +623,15 @@ impl CStore {
623623
self.get_crate_data(cnum).get_proc_macro_quoted_span(tcx, id)
624624
}
625625

626-
pub fn set_used_recursively(&mut self, tcx: TyCtxt<'_>, cnum: CrateNum) {
626+
pub fn set_used_recursively(&mut self, cnum: CrateNum) {
627627
let cmeta = self.get_crate_data_mut(cnum);
628628
if !cmeta.used {
629629
cmeta.used = true;
630-
let dependencies = mem::take(&mut cmeta.dependencies);
631-
for &dep_cnum in &dependencies {
632-
self.set_used_recursively(tcx, dep_cnum);
630+
let cnum_map = mem::take(&mut cmeta.cnum_map);
631+
for &dep_cnum in cnum_map.iter() {
632+
self.set_used_recursively(dep_cnum);
633633
}
634-
self.get_crate_data_mut(cnum).dependencies = dependencies;
634+
self.get_crate_data_mut(cnum).cnum_map = cnum_map;
635635
}
636636
}
637637

@@ -663,11 +663,11 @@ impl CStore {
663663
if cmeta.update_extern_crate_diagnostics(extern_crate) {
664664
// Propagate the extern crate info to dependencies if it was updated.
665665
let extern_crate = ExternCrate { dependency_of: cnum, ..extern_crate };
666-
let dependencies = mem::take(&mut cmeta.dependencies);
667-
for &dep_cnum in &dependencies {
666+
let cnum_map = mem::take(&mut cmeta.cnum_map);
667+
for &dep_cnum in cnum_map.iter() {
668668
self.update_transitive_extern_crate_diagnostics(dep_cnum, extern_crate);
669669
}
670-
self.get_crate_data_mut(cnum).dependencies = dependencies;
670+
self.get_crate_data_mut(cnum).cnum_map = cnum_map;
671671
}
672672
}
673673
}

compiler/rustc_session/src/cstore.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,21 @@ impl CrateSource {
3939
pub enum CrateDepKind {
4040
/// A dependency that is only used for its macros.
4141
MacrosOnly,
42-
/// A dependency that is always injected into the dependency list and so
43-
/// doesn't need to be linked to an rlib, e.g., the injected panic runtime.
44-
Implicit,
42+
/// A dependency that is injected into the crate graph but which only
43+
/// sometimes needs to actually be linked in, e.g., the injected panic runtime.
44+
Conditional,
4545
/// A dependency that is required by an rlib version of this crate.
46-
/// Ordinary `extern crate`s result in `Explicit` dependencies.
47-
Explicit,
46+
/// Ordinary `extern crate`s as well as most injected dependencies result
47+
/// in `Unconditional` dependencies.
48+
Unconditional,
4849
}
4950

5051
impl CrateDepKind {
5152
#[inline]
5253
pub fn macros_only(self) -> bool {
5354
match self {
5455
CrateDepKind::MacrosOnly => true,
55-
CrateDepKind::Implicit | CrateDepKind::Explicit => false,
56+
CrateDepKind::Conditional | CrateDepKind::Unconditional => false,
5657
}
5758
}
5859
}

0 commit comments

Comments
 (0)