Skip to content

Commit febf293

Browse files
committed
Introduce a query for visible crates
Add a query for crates that are visible and shoule appear in diagnostics. This excludes stdlib-private crates unless the `rustc_private` feature is set, in which case there is no difference from the existing `.crates(())` query.
1 parent c1211a5 commit febf293

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

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

+17-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_middle::util::Providers;
1818
use rustc_session::cstore::{CrateStore, ExternCrate};
1919
use rustc_session::{Session, StableCrateId};
2020
use rustc_span::hygiene::ExpnId;
21-
use rustc_span::{Span, Symbol, kw};
21+
use rustc_span::{Span, Symbol, kw, sym};
2222

2323
use super::{Decodable, DecodeContext, DecodeIterator};
2424
use crate::creader::{CStore, LoadedMacro};
@@ -561,6 +561,22 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
561561
tcx.untracked().cstore.freeze();
562562
tcx.arena.alloc_from_iter(CStore::from_tcx(tcx).iter_crate_data().map(|(cnum, _)| cnum))
563563
},
564+
visible_crates: |tcx, ()| {
565+
// The list of loaded crates is now frozen in query cache,
566+
// so make sure cstore is not mutably accessed from here on.
567+
tcx.untracked().cstore.freeze();
568+
569+
// if `#![feature(rustc_private)]` is enabled in the current crate, include
570+
if tcx.features().enabled(sym::rustc_private) {
571+
tcx.crates(())
572+
} else {
573+
tcx.arena.alloc_from_iter(
574+
CStore::from_tcx(tcx)
575+
.iter_crate_data()
576+
.filter_map(|(cnum, meta)| (!meta.stdlib_private_dep).then_some(cnum)),
577+
)
578+
}
579+
},
564580
used_crates: |tcx, ()| {
565581
// The list of loaded crates is now frozen in query cache,
566582
// so make sure cstore is not mutably accessed from here on.

compiler/rustc_middle/src/query/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2128,10 +2128,21 @@ rustc_queries! {
21282128
eval_always
21292129
desc { "calculating the stability index for the local crate" }
21302130
}
2131+
/// All available crates in the graph. Use `.visible_crates(())` instead if results should only
2132+
/// include user-visible crates (such as for diagnostics).
21312133
query crates(_: ()) -> &'tcx [CrateNum] {
21322134
eval_always
21332135
desc { "fetching all foreign CrateNum instances" }
21342136
}
2137+
/// Crates that are meant to be accessible by the user. Use `.crates(())` if the entire crate
2138+
/// graph is required.
2139+
///
2140+
/// This excludes e.g. private dependencies of the standard library, unless `rustc_private` is
2141+
/// enabled.
2142+
query visible_crates(_: ()) -> &'tcx [CrateNum] {
2143+
eval_always
2144+
desc { "fetching all user-reachable CrateNum instances" }
2145+
}
21352146
// Crates that are loaded non-speculatively (not for diagnostics or doc links).
21362147
// FIXME: This is currently only used for collecting lang items, but should be used instead of
21372148
// `crates` in most other cases too.

0 commit comments

Comments
 (0)