1
Fork 0

Rollup merge of #124976 - petrochenkov:usedcrates, r=oli-obk

rustc: Use `tcx.used_crates(())` more

And explain when it should be used.

Addresses comments from https://github.com/rust-lang/rust/pull/121167.
This commit is contained in:
Matthias Krüger 2024-05-23 14:09:23 +02:00 committed by GitHub
commit eda4a35f36
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 54 additions and 47 deletions

View file

@ -1016,7 +1016,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
let krate = tcx.hir_crate(());
let hir_body_hash = krate.opt_hir_hash.expect("HIR hash missing while computing crate hash");
let upstream_crates = upstream_crates(tcx);
let upstream_crates = upstream_crates_for_hashing(tcx);
let resolutions = tcx.resolutions(());
@ -1085,9 +1085,9 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
Svh::new(crate_hash)
}
fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
fn upstream_crates_for_hashing(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
let mut upstream_crates: Vec<_> = tcx
.crates(())
.crates_including_speculative(())
.iter()
.map(|&cnum| {
let stable_crate_id = tcx.stable_crate_id(cnum);

View file

@ -1860,13 +1860,22 @@ rustc_queries! {
eval_always
desc { "calculating the stability index for the local crate" }
}
query crates(_: ()) -> &'tcx [CrateNum] {
/// All loaded crates, including those loaded purely for doc links or diagnostics.
/// (Diagnostics include lints, so speculatively loaded crates may occur in successful
/// compilation even without doc links.)
/// Should be used when encoding crate metadata (and therefore when generating crate hash,
/// depinfo and similar things), to avoid dangling crate references in other encoded data,
/// like source maps.
/// May also be used for diagnostics - if we are loading a crate anyway we can suggest some
/// items from it as well.
/// But otherwise, `used_crates` should generally be used.
query crates_including_speculative(_: ()) -> &'tcx [CrateNum] {
eval_always
desc { "fetching all foreign CrateNum instances" }
}
// Crates that are loaded non-speculatively (not for diagnostics or doc links).
// FIXME: This is currently only used for collecting lang items, but should be used instead of
// `crates` in most other cases too.
/// Crates that are loaded non-speculatively (not for diagnostics or doc links).
/// Should be used to maintain observable language behavior, for example when collecting lang
/// items or impls from all crates, or collecting libraries to link.
query used_crates(_: ()) -> &'tcx [CrateNum] {
eval_always
desc { "fetching `CrateNum`s for all crates loaded non-speculatively" }

View file

@ -1611,7 +1611,7 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn all_traits(self) -> impl Iterator<Item = DefId> + 'tcx {
iter::once(LOCAL_CRATE)
.chain(self.crates(()).iter().copied())
.chain(self.used_crates(()).iter().copied())
.flat_map(move |cnum| self.traits(cnum).iter().copied())
}

View file

@ -3258,7 +3258,7 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N
let queue = &mut Vec::new();
let mut seen_defs: DefIdSet = Default::default();
for &cnum in tcx.crates(()).iter() {
for &cnum in tcx.crates_including_speculative(()).iter() {
let def_id = cnum.as_def_id();
// Ignore crates that are not direct dependencies.

View file

@ -205,7 +205,7 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> Trait
// Traits defined in the current crate can't have impls in upstream
// crates, so we don't bother querying the cstore.
if !trait_id.is_local() {
for &cnum in tcx.crates(()).iter() {
for &cnum in tcx.used_crates(()).iter() {
for &(impl_def_id, simplified_self_ty) in
tcx.implementations_of_trait((cnum, trait_id)).iter()
{
@ -247,7 +247,7 @@ pub(super) fn incoherent_impls_provider(
let mut impls = Vec::new();
let mut res = Ok(());
for cnum in iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) {
for cnum in iter::once(LOCAL_CRATE).chain(tcx.used_crates(()).iter().copied()) {
let incoherent_impls = match tcx.crate_incoherent_impls((cnum, simp)) {
Ok(impls) => impls,
Err(e) => {