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

Commit 0f35ba8

Browse files
committed
rustc: Use tcx.used_crates(()) more
And explain when it should be used.
1 parent cf77474 commit 0f35ba8

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
@@ -142,7 +142,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
142142
&& sess.crt_static(Some(ty))
143143
&& !sess.target.crt_static_allows_dylibs)
144144
{
145-
for &cnum in tcx.crates(()).iter() {
145+
for &cnum in tcx.used_crates(()).iter() {
146146
if tcx.dep_kind(cnum).macros_only() {
147147
continue;
148148
}
@@ -163,7 +163,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
163163
// Sweep all crates for found dylibs. Add all dylibs, as well as their
164164
// dependencies, ensuring there are no conflicts. The only valid case for a
165165
// dependency to be relied upon twice is for both cases to rely on a dylib.
166-
for &cnum in tcx.crates(()).iter() {
166+
for &cnum in tcx.used_crates(()).iter() {
167167
if tcx.dep_kind(cnum).macros_only() {
168168
continue;
169169
}
@@ -181,7 +181,7 @@ fn calculate_type(tcx: TyCtxt<'_>, ty: CrateType) -> DependencyList {
181181
}
182182

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

284284
fn attempt_static(tcx: TyCtxt<'_>, unavailable: &mut Vec<CrateNum>) -> Option<DependencyList> {
285285
let all_crates_available_as_rlib = tcx
286-
.crates(())
286+
.used_crates(())
287287
.iter()
288288
.copied()
289289
.filter_map(|cnum| {
@@ -304,7 +304,7 @@ fn attempt_static(tcx: TyCtxt<'_>, unavailable: &mut Vec<CrateNum>) -> Option<De
304304
// All crates are available in an rlib format, so we're just going to link
305305
// everything in explicitly so long as it's actually required.
306306
let mut ret = tcx
307-
.crates(())
307+
.used_crates(())
308308
.iter()
309309
.map(|&cnum| match tcx.dep_kind(cnum) {
310310
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
@@ -434,7 +434,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
434434
// traversal, but not globally minimal across all crates.
435435
let bfs_queue = &mut VecDeque::new();
436436

437-
for &cnum in tcx.crates(()) {
437+
for &cnum in tcx.all_crates(()) {
438438
// Ignore crates without a corresponding local `extern crate` item.
439439
if tcx.missing_extern_crate_item(cnum) {
440440
continue;
@@ -504,7 +504,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
504504
tcx.arena
505505
.alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE))
506506
},
507-
crates: |tcx, ()| {
507+
all_crates: |tcx, ()| {
508508
// The list of loaded crates is now frozen in query cache,
509509
// so make sure cstore is not mutably accessed from here on.
510510
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
@@ -1897,7 +1897,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
18971897

18981898
let deps = self
18991899
.tcx
1900-
.crates(())
1900+
.all_crates(())
19011901
.iter()
19021902
.map(|&cnum| {
19031903
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
@@ -1011,7 +1011,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
10111011
let krate = tcx.hir_crate(());
10121012
let hir_body_hash = krate.opt_hir_hash.expect("HIR hash missing while computing crate hash");
10131013

1014-
let upstream_crates = upstream_crates(tcx);
1014+
let upstream_crates = upstream_crates_for_hashing(tcx);
10151015

10161016
let resolutions = tcx.resolutions(());
10171017

@@ -1080,9 +1080,9 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
10801080
Svh::new(crate_hash)
10811081
}
10821082

1083-
fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
1083+
fn upstream_crates_for_hashing(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
10841084
let mut upstream_crates: Vec<_> = tcx
1085-
.crates(())
1085+
.all_crates(())
10861086
.iter()
10871087
.map(|&cnum| {
10881088
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
@@ -1508,7 +1508,7 @@ impl<'tcx> TyCtxt<'tcx> {
15081508

15091509
pub fn all_traits(self) -> impl Iterator<Item = DefId> + 'tcx {
15101510
iter::once(LOCAL_CRATE)
1511-
.chain(self.crates(()).iter().copied())
1511+
.chain(self.used_crates(()).iter().copied())
15121512
.flat_map(move |cnum| self.traits(cnum).iter().copied())
15131513
}
15141514

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

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

3263-
for &cnum in tcx.crates(()).iter() {
3263+
for &cnum in tcx.all_crates(()).iter() {
32643264
let def_id = cnum.as_def_id();
32653265

32663266
// Ignore crates that are not direct dependencies.

0 commit comments

Comments
 (0)