Skip to content

Commit 2da9acc

Browse files
committed
Add tcx.visible_traits() and use it for producing diagnostics
Add an alternative to `tcx.all_traits()` that only shows traits that the user might be able to use, for diagnostic purposes. With this available, make use of it for diagnostics including associated type errors, which is part of the problem with [1]. Includes a few comment updates for related API. [1]: #135232
1 parent 9c34253 commit 2da9acc

File tree

5 files changed

+23
-4
lines changed

5 files changed

+23
-4
lines changed

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
179179
// all visible traits. If there's one clear winner, just suggest that.
180180

181181
let visible_traits: Vec<_> = tcx
182-
.all_traits()
182+
.visible_traits()
183183
.filter(|trait_def_id| {
184184
let viz = tcx.visibility(*trait_def_id);
185185
let def_id = self.item_def_id();

Diff for: compiler/rustc_middle/src/query/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2128,6 +2128,8 @@ rustc_queries! {
21282128
eval_always
21292129
desc { "calculating the stability index for the local crate" }
21302130
}
2131+
/// All available crates in the graph, including those that should not be user-facing
2132+
/// (such as private crates).
21312133
query crates(_: ()) -> &'tcx [CrateNum] {
21322134
eval_always
21332135
desc { "fetching all foreign CrateNum instances" }

Diff for: compiler/rustc_middle/src/ty/context.rs

+11
Original file line numberDiff line numberDiff line change
@@ -2078,12 +2078,23 @@ impl<'tcx> TyCtxt<'tcx> {
20782078
self.limits(()).move_size_limit
20792079
}
20802080

2081+
/// All traits in the crate graph, including those not visible to the user.
20812082
pub fn all_traits(self) -> impl Iterator<Item = DefId> + 'tcx {
20822083
iter::once(LOCAL_CRATE)
20832084
.chain(self.crates(()).iter().copied())
20842085
.flat_map(move |cnum| self.traits(cnum).iter().copied())
20852086
}
20862087

2088+
/// All traits that are visible within the crate graph (i.e. excluding private dependencies).
2089+
pub fn visible_traits(self) -> impl Iterator<Item = DefId> + 'tcx {
2090+
let visible_crates =
2091+
self.crates(()).iter().copied().filter(move |cnum| self.is_user_visible_dep(*cnum));
2092+
2093+
iter::once(LOCAL_CRATE)
2094+
.chain(visible_crates)
2095+
.flat_map(move |cnum| self.traits(cnum).iter().copied())
2096+
}
2097+
20872098
#[inline]
20882099
pub fn local_visibility(self, def_id: LocalDefId) -> Visibility {
20892100
self.visibility(def_id).expect_local()

Diff for: compiler/rustc_passes/src/diagnostic_items.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,14 @@ fn all_diagnostic_items(tcx: TyCtxt<'_>, (): ()) -> DiagnosticItems {
7979
// Initialize the collector.
8080
let mut items = DiagnosticItems::default();
8181

82-
// Collect diagnostic items in other crates.
83-
for &cnum in tcx.crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
82+
// Collect diagnostic items in visible crates.
83+
for cnum in tcx
84+
.crates(())
85+
.iter()
86+
.copied()
87+
.filter(|cnum| tcx.is_user_visible_dep(*cnum))
88+
.chain(std::iter::once(LOCAL_CRATE))
89+
{
8490
for (&name, &def_id) in &tcx.diagnostic_items(cnum).name_to_id {
8591
collect_item(tcx, &mut items, name, def_id);
8692
}

Diff for: compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2189,7 +2189,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
21892189
let required_trait_path = self.tcx.def_path_str(trait_ref.def_id());
21902190
let traits_with_same_path: UnordSet<_> = self
21912191
.tcx
2192-
.all_traits()
2192+
.visible_traits()
21932193
.filter(|trait_def_id| *trait_def_id != trait_ref.def_id())
21942194
.map(|trait_def_id| (self.tcx.def_path_str(trait_def_id), trait_def_id))
21952195
.filter(|(p, _)| *p == required_trait_path)

0 commit comments

Comments
 (0)