Skip to content

Commit 0cba80b

Browse files
committed
Auto merge of #124976 - petrochenkov:usedcrates, r=<try>
rustc: Use `tcx.used_crates(())` more And explain when it should be used. Addresses comments from #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

+1-1
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

+2-2
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

+1-1
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

+6-6
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

+2-2
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

+1-1
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

+3-3
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

+11-3
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

+1-1
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

+1-1
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.

compiler/rustc_middle/src/ty/trait_def.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> Trait
205205
// Traits defined in the current crate can't have impls in upstream
206206
// crates, so we don't bother querying the cstore.
207207
if !trait_id.is_local() {
208-
for &cnum in tcx.crates(()).iter() {
208+
for &cnum in tcx.used_crates(()).iter() {
209209
for &(impl_def_id, simplified_self_ty) in
210210
tcx.implementations_of_trait((cnum, trait_id)).iter()
211211
{
@@ -247,7 +247,7 @@ pub(super) fn incoherent_impls_provider(
247247
let mut impls = Vec::new();
248248

249249
let mut res = Ok(());
250-
for cnum in iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) {
250+
for cnum in iter::once(LOCAL_CRATE).chain(tcx.used_crates(()).iter().copied()) {
251251
let incoherent_impls = match tcx.crate_incoherent_impls((cnum, simp)) {
252252
Ok(impls) => impls,
253253
Err(e) => {

compiler/rustc_passes/src/diagnostic_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn all_diagnostic_items(tcx: TyCtxt<'_>, (): ()) -> DiagnosticItems {
8282
let mut items = DiagnosticItems::default();
8383

8484
// Collect diagnostic items in other crates.
85-
for &cnum in tcx.crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
85+
for &cnum in tcx.all_crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
8686
for (&name, &def_id) in &tcx.diagnostic_items(cnum).name_to_id {
8787
collect_item(tcx, &mut items, name, def_id);
8888
}

compiler/rustc_passes/src/stability.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
10201020
// stabilization diagnostic, but it can be avoided when there are no
10211021
// `remaining_lib_features`.
10221022
let mut all_implications = remaining_implications.clone();
1023-
for &cnum in tcx.crates(()) {
1023+
for &cnum in tcx.used_crates(()) {
10241024
all_implications
10251025
.extend_unord(tcx.stability_implications(cnum).items().map(|(k, v)| (*k, *v)));
10261026
}
@@ -1033,7 +1033,7 @@ pub fn check_unused_or_stable_features(tcx: TyCtxt<'_>) {
10331033
&all_implications,
10341034
);
10351035

1036-
for &cnum in tcx.crates(()) {
1036+
for &cnum in tcx.used_crates(()) {
10371037
if remaining_lib_features.is_empty() && remaining_implications.is_empty() {
10381038
break;
10391039
}

compiler/rustc_passes/src/weak_lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fn verify(tcx: TyCtxt<'_>, items: &lang_items::LanguageItems) {
6868
}
6969

7070
let mut missing = FxHashSet::default();
71-
for &cnum in tcx.crates(()).iter() {
71+
for &cnum in tcx.used_crates(()).iter() {
7272
for &item in tcx.missing_lang_items(cnum).iter() {
7373
missing.insert(item);
7474
}

compiler/rustc_smir/src/rustc_smir/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
126126
let mut tables = self.0.borrow_mut();
127127
let tcx = tables.tcx;
128128
iter::once(LOCAL_CRATE)
129-
.chain(tables.tcx.crates(()).iter().copied())
129+
.chain(tables.tcx.used_crates(()).iter().copied())
130130
.flat_map(|cnum| tcx.trait_impls_in_crate(cnum).iter())
131131
.map(|impl_def_id| tables.impl_def(*impl_def_id))
132132
.collect()
@@ -201,14 +201,14 @@ impl<'tcx> Context for TablesWrapper<'tcx> {
201201

202202
fn external_crates(&self) -> Vec<stable_mir::Crate> {
203203
let tables = self.0.borrow();
204-
tables.tcx.crates(()).iter().map(|crate_num| smir_crate(tables.tcx, *crate_num)).collect()
204+
tables.tcx.used_crates(()).iter().map(|crate_num| smir_crate(tables.tcx, *crate_num)).collect()
205205
}
206206

207207
fn find_crates(&self, name: &str) -> Vec<stable_mir::Crate> {
208208
let tables = self.0.borrow();
209209
let crates: Vec<stable_mir::Crate> = [LOCAL_CRATE]
210210
.iter()
211-
.chain(tables.tcx.crates(()).iter())
211+
.chain(tables.tcx.used_crates(()).iter())
212212
.filter_map(|crate_num| {
213213
let crate_name = tables.tcx.crate_name(*crate_num).to_string();
214214
(name == crate_name).then(|| smir_crate(tables.tcx, *crate_num))

src/librustdoc/clean/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1937,7 +1937,7 @@ impl PrimitiveType {
19371937
let mut primitive_locations = FxHashMap::default();
19381938
// NOTE: technically this misses crates that are only passed with `--extern` and not loaded when checking the crate.
19391939
// This is a degenerate case that I don't plan to support.
1940-
for &crate_num in tcx.crates(()) {
1940+
for &crate_num in tcx.all_crates(()) {
19411941
let e = ExternalCrate { crate_num };
19421942
let crate_name = e.name(tcx);
19431943
debug!(?crate_num, ?crate_name);

src/librustdoc/core.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ pub(crate) fn run_global_ctxt(
347347
show_coverage,
348348
};
349349

350-
for cnum in tcx.crates(()) {
350+
for cnum in tcx.all_crates(()) {
351351
crate::visit_lib::lib_embargo_visit_item(&mut ctxt, cnum.as_def_id());
352352
}
353353

src/librustdoc/formats/cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl Cache {
155155

156156
// Cache where all our extern crates are located
157157
// FIXME: this part is specific to HTML so it'd be nice to remove it from the common code
158-
for &crate_num in tcx.crates(()) {
158+
for &crate_num in tcx.all_crates(()) {
159159
let e = ExternalCrate { crate_num };
160160

161161
let name = e.name(tcx);

src/librustdoc/passes/collect_trait_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) ->
4747
// External trait impls.
4848
{
4949
let _prof_timer = tcx.sess.prof.generic_activity("build_extern_trait_impls");
50-
for &cnum in tcx.crates(()) {
50+
for &cnum in tcx.all_crates(()) {
5151
for &impl_def_id in tcx.trait_impls_in_crate(cnum) {
5252
cx.with_param_env(impl_def_id, |cx| {
5353
inline::build_impl(cx, impl_def_id, None, &mut new_items_external);

src/librustdoc/scrape_examples.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ pub(crate) fn run(
283283
// Collect CrateIds corresponding to provided target crates
284284
// If two different versions of the crate in the dependency tree, then examples will be collected from both.
285285
let all_crates = tcx
286-
.crates(())
286+
.all_crates(())
287287
.iter()
288288
.chain([&LOCAL_CRATE])
289289
.map(|crate_num| (crate_num, tcx.crate_name(*crate_num)))

src/tools/clippy/clippy_utils/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ fn item_children_by_name(tcx: TyCtxt<'_>, def_id: DefId, name: Symbol) -> Vec<Re
647647
/// This function is expensive and should be used sparingly.
648648
pub fn def_path_res(cx: &LateContext<'_>, path: &[&str]) -> Vec<Res> {
649649
fn find_crates(tcx: TyCtxt<'_>, name: Symbol) -> impl Iterator<Item = DefId> + '_ {
650-
tcx.crates(())
650+
tcx.all_crates(())
651651
.iter()
652652
.copied()
653653
.filter(move |&num| tcx.crate_name(num) == name)

src/tools/miri/src/helpers.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ fn try_resolve_did(tcx: TyCtxt<'_>, path: &[&str], namespace: Option<Namespace>)
126126
// the one in the sysroot and the one locally built by `cargo test`.)
127127
// FIXME: can we prefer the one from the sysroot?
128128
'crates: for krate in
129-
tcx.crates(()).iter().filter(|&&krate| tcx.crate_name(krate).as_str() == crate_name)
129+
tcx.all_crates(()).iter().filter(|&&krate| tcx.crate_name(krate).as_str() == crate_name)
130130
{
131131
let mut cur_item = DefId { krate: *krate, index: CRATE_DEF_INDEX };
132132
// Go over the modules.
@@ -1365,7 +1365,7 @@ pub fn get_local_crates(tcx: TyCtxt<'_>) -> Vec<CrateNum> {
13651365
.map(|crates| crates.split(',').map(|krate| krate.to_string()).collect::<Vec<_>>())
13661366
.unwrap_or_default();
13671367
let mut local_crates = Vec::new();
1368-
for &crate_num in tcx.crates(()) {
1368+
for &crate_num in tcx.all_crates(()) {
13691369
let name = tcx.crate_name(crate_num);
13701370
let name = name.as_str();
13711371
if local_crate_names.iter().any(|local_name| local_name == name) {
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
error: extern location for std does not exist:
22

3-
error: `#[panic_handler]` function required, but not found
4-
5-
error: unwinding panics are not supported without std
6-
|
7-
= help: using nightly cargo, use -Zbuild-std with panic="abort" to avoid unwinding
8-
= note: since the core library is usually precompiled with panic="unwind", rebuilding your crate with panic="abort" may not be enough to fix the problem
9-
103
error: requires `sized` lang_item
114

12-
error: aborting due to 4 previous errors
5+
error: aborting due to 2 previous errors
136

0 commit comments

Comments
 (0)