Skip to content

Commit 51aa76f

Browse files
committed
avoid suggesting traits from private dependencies
1 parent 1e6e4bb commit 51aa76f

File tree

8 files changed

+14
-26
lines changed

8 files changed

+14
-26
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
15881588
&infcx_
15891589
};
15901590

1591-
tcx.all_traits()
1591+
tcx.all_traits_including_private()
15921592
.filter(|trait_def_id| {
15931593
// Consider only traits with the associated type
15941594
tcx.associated_items(*trait_def_id)

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1725,7 +1725,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17251725
if unsatisfied_predicates.is_empty()
17261726
// ...or if we already suggested that name because of `rustc_confusable` annotation
17271727
&& Some(similar_candidate.name()) != confusable_suggested
1728-
// and if the we aren't in an expansion.
1728+
// and if we aren't in an expansion.
17291729
&& !span.from_expansion()
17301730
{
17311731
self.find_likely_intended_associated_item(
@@ -3477,9 +3477,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34773477
&self,
34783478
err: &mut Diag<'_>,
34793479
item_name: Ident,
3480-
valid_out_of_scope_traits: Vec<DefId>,
3480+
mut valid_out_of_scope_traits: Vec<DefId>,
34813481
explain: bool,
34823482
) -> bool {
3483+
valid_out_of_scope_traits.retain(|id| self.tcx.is_user_visible_dep(id.krate));
34833484
if !valid_out_of_scope_traits.is_empty() {
34843485
let mut candidates = valid_out_of_scope_traits;
34853486
candidates.sort_by_key(|id| self.tcx.def_path_str(id));
@@ -4384,7 +4385,7 @@ pub(crate) struct TraitInfo {
43844385
/// Retrieves all traits in this crate and any dependent crates,
43854386
/// and wraps them into `TraitInfo` for custom sorting.
43864387
pub(crate) fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {
4387-
tcx.all_traits().map(|def_id| TraitInfo { def_id }).collect()
4388+
tcx.all_traits_including_private().map(|def_id| TraitInfo { def_id }).collect()
43884389
}
43894390

43904391
fn print_disambiguation_help<'tcx>(

compiler/rustc_middle/src/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2318,7 +2318,7 @@ impl<'tcx> TyCtxt<'tcx> {
23182318
}
23192319

23202320
/// All traits in the crate graph, including those not visible to the user.
2321-
pub fn all_traits(self) -> impl Iterator<Item = DefId> {
2321+
pub fn all_traits_including_private(self) -> impl Iterator<Item = DefId> {
23222322
iter::once(LOCAL_CRATE)
23232323
.chain(self.crates(()).iter().copied())
23242324
.flat_map(move |cnum| self.traits(cnum).iter().copied())

compiler/rustc_smir/src/rustc_smir/context.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ impl<'tcx> SmirCtxt<'tcx> {
130130

131131
pub fn all_trait_decls(&self) -> stable_mir::TraitDecls {
132132
let mut tables = self.0.borrow_mut();
133-
tables.tcx.all_traits().map(|trait_def_id| tables.trait_def(trait_def_id)).collect()
133+
tables
134+
.tcx
135+
.all_traits_including_private()
136+
.map(|trait_def_id| tables.trait_def(trait_def_id))
137+
.collect()
134138
}
135139

136140
pub fn trait_decls(&self, crate_num: CrateNum) -> stable_mir::TraitDecls {

compiler/rustc_trait_selection/src/error_reporting/traits/fulfillment_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1850,7 +1850,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
18501850
let trait_def_id = trait_pred.def_id();
18511851
let trait_name = self.tcx.item_name(trait_def_id);
18521852
let crate_name = self.tcx.crate_name(trait_def_id.krate);
1853-
if let Some(other_trait_def_id) = self.tcx.all_traits().find(|def_id| {
1853+
if let Some(other_trait_def_id) = self.tcx.all_traits_including_private().find(|def_id| {
18541854
trait_name == self.tcx.item_name(trait_def_id)
18551855
&& trait_def_id.krate != def_id.krate
18561856
&& crate_name == self.tcx.crate_name(def_id.krate)

src/librustdoc/clean/blanket_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) fn synthesize_blanket_impls(
2323
let ty = tcx.type_of(item_def_id);
2424

2525
let mut blanket_impls = Vec::new();
26-
for trait_def_id in tcx.all_traits() {
26+
for trait_def_id in tcx.visible_traits() {
2727
if !cx.cache.effective_visibilities.is_reachable(tcx, trait_def_id)
2828
|| cx.generated_synthetics.contains(&(ty.skip_binder(), trait_def_id))
2929
{

src/librustdoc/core.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ pub(crate) fn run_global_ctxt(
353353
rustc_passes::stability::check_unused_or_stable_features(tcx);
354354

355355
let auto_traits =
356-
tcx.all_traits().filter(|&trait_def_id| tcx.trait_is_auto(trait_def_id)).collect();
356+
tcx.visible_traits().filter(|&trait_def_id| tcx.trait_is_auto(trait_def_id)).collect();
357357

358358
let mut ctxt = DocContext {
359359
tcx,

tests/ui/typeck/dont-suggest-private-dependencies.stderr

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ error[E0599]: no method named `read` found for struct `Vec<u8>` in the current s
44
LL | self.0.read(buf)
55
| ^^^^
66
|
7-
= help: items from traits can only be used if the trait is in scope
8-
help: trait `ReadRef` which provides `read` is implemented but not in scope; perhaps you want to import it
9-
|
10-
LL + use object::read::read_ref::ReadRef;
11-
|
127
help: there is a method `read_at` with a similar name
138
|
149
LL | self.0.read_at(buf)
@@ -19,24 +14,12 @@ error[E0599]: no function or associated item named `cast_from_lossy` found for t
1914
|
2015
LL | let _ = u8::cast_from_lossy(9);
2116
| ^^^^^^^^^^^^^^^ function or associated item not found in `u8`
22-
|
23-
= help: items from traits can only be used if the trait is in scope
24-
help: trait `CastFrom` which provides `cast_from_lossy` is implemented but not in scope; perhaps you want to import it
25-
|
26-
LL + use compiler_builtins::math::libm_math::support::int_traits::CastFrom;
27-
|
2817

2918
error[E0599]: no function or associated item named `foo` found for struct `B` in the current scope
3019
--> $DIR/dont-suggest-private-dependencies.rs:28:16
3120
|
3221
LL | let _ = B::foo();
3322
| ^^^ function or associated item not found in `B`
34-
|
35-
= help: items from traits can only be used if the trait is in scope
36-
help: trait `A` which provides `foo` is implemented but not in scope; perhaps you want to import it
37-
|
38-
LL + use private_dep::A;
39-
|
4023

4124
error: aborting due to 3 previous errors
4225

0 commit comments

Comments
 (0)