Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c2f2714

Browse files
committedFeb 15, 2024
resolve: Scale back unloading of speculatively loaded crates
1 parent b656f51 commit c2f2714

File tree

7 files changed

+26
-18
lines changed

7 files changed

+26
-18
lines changed
 

‎compiler/rustc_metadata/src/creader.rs

-10
Original file line numberDiff line numberDiff line change
@@ -1070,16 +1070,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
10701070
pub fn maybe_process_path_extern(&mut self, name: Symbol) -> Option<CrateNum> {
10711071
self.maybe_resolve_crate(name, CrateDepKind::Explicit, None).ok()
10721072
}
1073-
1074-
pub fn unload_unused_crates(&mut self) {
1075-
for opt_cdata in &mut self.cstore.metas {
1076-
if let Some(cdata) = opt_cdata
1077-
&& !cdata.used()
1078-
{
1079-
*opt_cdata = None;
1080-
}
1081-
}
1082-
}
10831073
}
10841074

10851075
fn global_allocator_spans(krate: &ast::Crate) -> Vec<Span> {

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

+10
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,16 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
519519
tcx.untracked().cstore.freeze();
520520
tcx.arena.alloc_from_iter(CStore::from_tcx(tcx).iter_crate_data().map(|(cnum, _)| cnum))
521521
},
522+
used_crates: |tcx, ()| {
523+
// The list of loaded crates is now frozen in query cache,
524+
// so make sure cstore is not mutably accessed from here on.
525+
tcx.untracked().cstore.freeze();
526+
tcx.arena.alloc_from_iter(
527+
CStore::from_tcx(tcx)
528+
.iter_crate_data()
529+
.filter_map(|(cnum, data)| data.used().then_some(cnum)),
530+
)
531+
},
522532
..providers.queries
523533
};
524534
provide_extern(&mut providers.extern_queries);

‎compiler/rustc_middle/src/query/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,13 @@ rustc_queries! {
18721872
eval_always
18731873
desc { "fetching all foreign CrateNum instances" }
18741874
}
1875+
// Crates that are loaded non-speculatively (not for diagnostics or doc links).
1876+
// FIXME: This is currently only used for collecting lang items, but should be used instead of
1877+
// `crates` in most other cases too.
1878+
query used_crates(_: ()) -> &'tcx [CrateNum] {
1879+
eval_always
1880+
desc { "fetching `CrateNum`s for all crates loaded non-speculatively" }
1881+
}
18751882

18761883
/// A list of all traits in a crate, used by rustdoc and error reporting.
18771884
query traits(_: CrateNum) -> &'tcx [DefId] {

‎compiler/rustc_passes/src/lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
250250
let mut collector = LanguageItemCollector::new(tcx, resolver);
251251

252252
// Collect lang items in other crates.
253-
for &cnum in tcx.crates(()).iter() {
253+
for &cnum in tcx.used_crates(()).iter() {
254254
for &(def_id, lang_item) in tcx.defined_lang_items(cnum).iter() {
255255
collector.collect_item(lang_item, def_id, None);
256256
}

‎compiler/rustc_resolve/src/late.rs

-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use rustc_hir::def::Namespace::{self, *};
2323
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, NonMacroAttrKind, PartialRes, PerNS};
2424
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
2525
use rustc_hir::{BindingAnnotation, PrimTy, TraitCandidate};
26-
use rustc_metadata::creader::CStore;
2726
use rustc_middle::middle::resolve_bound_vars::Set1;
2827
use rustc_middle::{bug, span_bug};
2928
use rustc_session::config::{CrateType, ResolveDocLinks};
@@ -4557,10 +4556,6 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
45574556
// Encoding foreign def ids in proc macro crate metadata will ICE.
45584557
return None;
45594558
}
4560-
// Doc paths should be resolved speculatively and should not produce any
4561-
// diagnostics, but if they are indeed resolved, then we need to keep the
4562-
// corresponding crate alive.
4563-
CStore::from_tcx_mut(self.r.tcx).set_used_recursively(def_id.krate);
45644559
}
45654560
res
45664561
});

‎compiler/rustc_resolve/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
16341634
self.tcx
16351635
.sess
16361636
.time("resolve_postprocess", || self.crate_loader(|c| c.postprocess(krate)));
1637-
self.crate_loader(|c| c.unload_unused_crates());
16381637
});
16391638

16401639
// Make sure we don't mutate the cstore from here on.
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
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+
310
error: requires `sized` lang_item
411

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

0 commit comments

Comments
 (0)
Please sign in to comment.