Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 0cba80b

Browse files
committed
Auto merge of rust-lang#124976 - petrochenkov:usedcrates, r=<try>
rustc: Use `tcx.used_crates(())` more And explain when it should be used. Addresses comments from rust-lang#121167.
2 parents f0038a7 + 0f35ba8 commit 0cba80b

File tree

23 files changed

+47
-46
lines changed

23 files changed

+47
-46
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ fn upstream_monomorphizations_provider(
399399
tcx: TyCtxt<'_>,
400400
(): (),
401401
) -> DefIdMap<UnordMap<GenericArgsRef<'_>, CrateNum>> {
402-
let cnums = tcx.crates(());
402+
let cnums = tcx.used_crates(());
403403

404404
let mut instances: DefIdMap<UnordMap<_, _>> = Default::default();
405405

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ pub fn collect_debugger_visualizers_transitive(
539539
tcx.debugger_visualizers(LOCAL_CRATE)
540540
.iter()
541541
.chain(
542-
tcx.crates(())
542+
tcx.used_crates(())
543543
.iter()
544544
.filter(|&cnum| {
545545
let used_crate_source = tcx.used_crate_source(*cnum);
@@ -849,7 +849,7 @@ impl CrateInfo {
849849
// `compiler_builtins` are always placed last to ensure that they're linked correctly.
850850
used_crates.extend(compiler_builtins);
851851

852-
let crates = tcx.crates(());
852+
let crates = tcx.used_crates(());
853853
let n_crates = crates.len();
854854
let mut info = CrateInfo {
855855
target_cpu,

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
458458
}
459459
}
460460

461-
for &cnum in tcx.crates(()) {
461+
for &cnum in tcx.all_crates(()) {
462462
let source = tcx.used_crate_source(cnum);
463463
if let Some((path, _)) = &source.dylib {
464464
files.push(escape_dep_filename(&path.display().to_string()));

compiler/rustc_metadata/src/dependency_format.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
143143
&& sess.crt_static(Some(ty))
144144
&& !sess.target.crt_static_allows_dylibs)
145145
{
146-
for &cnum in tcx.crates(()).iter() {
146+
for &cnum in tcx.used_crates(()).iter() {
147147
if tcx.dep_kind(cnum).macros_only() {
148148
continue;
149149
}
@@ -164,7 +164,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
164164
// Sweep all crates for found dylibs. Add all dylibs, as well as their
165165
// dependencies, ensuring there are no conflicts. The only valid case for a
166166
// dependency to be relied upon twice is for both cases to rely on a dylib.
167-
for &cnum in tcx.crates(()).iter() {
167+
for &cnum in tcx.used_crates(()).iter() {
168168
if tcx.dep_kind(cnum).macros_only() {
169169
continue;
170170
}
@@ -182,7 +182,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
182182
}
183183

184184
// Collect what we've got so far in the return vector.
185-
let last_crate = tcx.crates(()).len();
185+
let last_crate = tcx.used_crates(()).len();
186186
let mut ret = (1..last_crate + 1)
187187
.map(|cnum| match formats.get(&CrateNum::new(cnum)) {
188188
Some(&RequireDynamic) => Linkage::Dynamic,
@@ -196,7 +196,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
196196
//
197197
// If the crate hasn't been included yet and it's not actually required
198198
// (e.g., it's an allocator) then we skip it here as well.
199-
for &cnum in tcx.crates(()).iter() {
199+
for &cnum in tcx.used_crates(()).iter() {
200200
let src = tcx.used_crate_source(cnum);
201201
if src.dylib.is_none()
202202
&& !formats.contains_key(&cnum)
@@ -284,7 +284,7 @@ fn add_library(
284284

285285
fn attempt_static(tcx: TyCtxt<'_>, unavailable: &mut Vec<CrateNum>) -> Option<DependencyList> {
286286
let all_crates_available_as_rlib = tcx
287-
.crates(())
287+
.used_crates(())
288288
.iter()
289289
.copied()
290290
.filter_map(|cnum| {
@@ -305,7 +305,7 @@ fn attempt_static(tcx: TyCtxt<'_>, unavailable: &mut Vec<CrateNum>) -> Option<De
305305
// All crates are available in an rlib format, so we're just going to link
306306
// everything in explicitly so long as it's actually required.
307307
let mut ret = tcx
308-
.crates(())
308+
.used_crates(())
309309
.iter()
310310
.map(|&cnum| match tcx.dep_kind(cnum) {
311311
CrateDepKind::Explicit => Linkage::Static,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
435435
// traversal, but not globally minimal across all crates.
436436
let bfs_queue = &mut VecDeque::new();
437437

438-
for &cnum in tcx.crates(()) {
438+
for &cnum in tcx.all_crates(()) {
439439
// Ignore crates without a corresponding local `extern crate` item.
440440
if tcx.missing_extern_crate_item(cnum) {
441441
continue;
@@ -505,7 +505,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
505505
tcx.arena
506506
.alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE))
507507
},
508-
crates: |tcx, ()| {
508+
all_crates: |tcx, ()| {
509509
// The list of loaded crates is now frozen in query cache,
510510
// so make sure cstore is not mutably accessed from here on.
511511
tcx.untracked().cstore.freeze();

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1898,7 +1898,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18981898

18991899
let deps = self
19001900
.tcx
1901-
.crates(())
1901+
.all_crates(())
19021902
.iter()
19031903
.map(|&cnum| {
19041904
let dep = CrateDep {

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
10161016
let krate = tcx.hir_crate(());
10171017
let hir_body_hash = krate.opt_hir_hash.expect("HIR hash missing while computing crate hash");
10181018

1019-
let upstream_crates = upstream_crates(tcx);
1019+
let upstream_crates = upstream_crates_for_hashing(tcx);
10201020

10211021
let resolutions = tcx.resolutions(());
10221022

@@ -1085,9 +1085,9 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
10851085
Svh::new(crate_hash)
10861086
}
10871087

1088-
fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
1088+
fn upstream_crates_for_hashing(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
10891089
let mut upstream_crates: Vec<_> = tcx
1090-
.crates(())
1090+
.all_crates(())
10911091
.iter()
10921092
.map(|&cnum| {
10931093
let stable_crate_id = tcx.stable_crate_id(cnum);

compiler/rustc_middle/src/query/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,13 +1860,21 @@ rustc_queries! {
18601860
eval_always
18611861
desc { "calculating the stability index for the local crate" }
18621862
}
1863-
query crates(_: ()) -> &'tcx [CrateNum] {
1863+
// All loaded crates, including those loaded purely for doc links or diagnostics.
1864+
// (Diagnostics include lints, so speculatively loaded crates may occur in successful
1865+
// compilation even without doc links.)
1866+
// Should be used when encoding crate metadata (and therefore when generating crate hash,
1867+
// depinfo and similar things), to avoid dangling crate references in other encoded data,
1868+
// like source maps.
1869+
// May also be used for diagnostics - if we are loading a crate anyway we can suggest some
1870+
// items from it as well.
1871+
query all_crates(_: ()) -> &'tcx [CrateNum] {
18641872
eval_always
18651873
desc { "fetching all foreign CrateNum instances" }
18661874
}
18671875
// Crates that are loaded non-speculatively (not for diagnostics or doc links).
1868-
// FIXME: This is currently only used for collecting lang items, but should be used instead of
1869-
// `crates` in most other cases too.
1876+
// Should be used to maintain observable language behavior, for example when collecting lang
1877+
// items or impls from all crates, or collecting libraries to link.
18701878
query used_crates(_: ()) -> &'tcx [CrateNum] {
18711879
eval_always
18721880
desc { "fetching `CrateNum`s for all crates loaded non-speculatively" }

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,7 @@ impl<'tcx> TyCtxt<'tcx> {
16111611

16121612
pub fn all_traits(self) -> impl Iterator<Item = DefId> + 'tcx {
16131613
iter::once(LOCAL_CRATE)
1614-
.chain(self.crates(()).iter().copied())
1614+
.chain(self.used_crates(()).iter().copied())
16151615
.flat_map(move |cnum| self.traits(cnum).iter().copied())
16161616
}
16171617

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3258,7 +3258,7 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N
32583258
let queue = &mut Vec::new();
32593259
let mut seen_defs: DefIdSet = Default::default();
32603260

3261-
for &cnum in tcx.crates(()).iter() {
3261+
for &cnum in tcx.all_crates(()).iter() {
32623262
let def_id = cnum.as_def_id();
32633263

32643264
// Ignore crates that are not direct dependencies.

0 commit comments

Comments
 (0)