Skip to content

avoid suggesting traits from private dependencies #143038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
&infcx_
};

tcx.all_traits()
tcx.all_traits_including_private()
.filter(|trait_def_id| {
// Consider only traits with the associated type
tcx.associated_items(*trait_def_id)
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if unsatisfied_predicates.is_empty()
// ...or if we already suggested that name because of `rustc_confusable` annotation
&& Some(similar_candidate.name()) != confusable_suggested
// and if the we aren't in an expansion.
// and if we aren't in an expansion.
&& !span.from_expansion()
{
self.find_likely_intended_associated_item(
Expand Down Expand Up @@ -3477,9 +3477,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self,
err: &mut Diag<'_>,
item_name: Ident,
valid_out_of_scope_traits: Vec<DefId>,
mut valid_out_of_scope_traits: Vec<DefId>,
explain: bool,
) -> bool {
valid_out_of_scope_traits.retain(|id| self.tcx.is_user_visible_dep(id.krate));
if !valid_out_of_scope_traits.is_empty() {
let mut candidates = valid_out_of_scope_traits;
candidates.sort_by_key(|id| self.tcx.def_path_str(id));
Expand Down Expand Up @@ -4384,7 +4385,7 @@ pub(crate) struct TraitInfo {
/// Retrieves all traits in this crate and any dependent crates,
/// and wraps them into `TraitInfo` for custom sorting.
pub(crate) fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {
tcx.all_traits().map(|def_id| TraitInfo { def_id }).collect()
tcx.all_traits_including_private().map(|def_id| TraitInfo { def_id }).collect()
}

fn print_disambiguation_help<'tcx>(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2318,7 +2318,7 @@ impl<'tcx> TyCtxt<'tcx> {
}

/// All traits in the crate graph, including those not visible to the user.
pub fn all_traits(self) -> impl Iterator<Item = DefId> {
pub fn all_traits_including_private(self) -> impl Iterator<Item = DefId> {
iter::once(LOCAL_CRATE)
.chain(self.crates(()).iter().copied())
.flat_map(move |cnum| self.traits(cnum).iter().copied())
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_smir/src/rustc_smir/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,11 @@ impl<'tcx> SmirCtxt<'tcx> {

pub fn all_trait_decls(&self) -> stable_mir::TraitDecls {
let mut tables = self.0.borrow_mut();
tables.tcx.all_traits().map(|trait_def_id| tables.trait_def(trait_def_id)).collect()
tables
.tcx
.all_traits_including_private()
.map(|trait_def_id| tables.trait_def(trait_def_id))
.collect()
}

pub fn trait_decls(&self, crate_num: CrateNum) -> stable_mir::TraitDecls {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1850,7 +1850,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
let trait_def_id = trait_pred.def_id();
let trait_name = self.tcx.item_name(trait_def_id);
let crate_name = self.tcx.crate_name(trait_def_id.krate);
if let Some(other_trait_def_id) = self.tcx.all_traits().find(|def_id| {
if let Some(other_trait_def_id) = self.tcx.all_traits_including_private().find(|def_id| {
trait_name == self.tcx.item_name(trait_def_id)
&& trait_def_id.krate != def_id.krate
&& crate_name == self.tcx.crate_name(def_id.krate)
Expand Down
2 changes: 2 additions & 0 deletions library/rustc-std-workspace-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cargo-features = ["public-dependency"]

[package]
name = "rustc-std-workspace-core"
version = "1.99.0"
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/blanket_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) fn synthesize_blanket_impls(
let ty = tcx.type_of(item_def_id);

let mut blanket_impls = Vec::new();
for trait_def_id in tcx.all_traits() {
for trait_def_id in tcx.visible_traits() {
if !cx.cache.effective_visibilities.is_reachable(tcx, trait_def_id)
|| cx.generated_synthetics.contains(&(ty.skip_binder(), trait_def_id))
{
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ pub(crate) fn run_global_ctxt(
rustc_passes::stability::check_unused_or_stable_features(tcx);

let auto_traits =
tcx.all_traits().filter(|&trait_def_id| tcx.trait_is_auto(trait_def_id)).collect();
tcx.visible_traits().filter(|&trait_def_id| tcx.trait_is_auto(trait_def_id)).collect();

let mut ctxt = DocContext {
tcx,
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/typeck/auxiliary/private-dep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub trait A {
fn foo() {}
}
11 changes: 11 additions & 0 deletions tests/ui/typeck/auxiliary/public-dep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@ aux-crate:priv:private_dep=private-dep.rs
//@ compile-flags: -Zunstable-options

extern crate private_dep;
use private_dep::A;

pub struct B;

impl A for B {
fn foo() {}
}
30 changes: 30 additions & 0 deletions tests/ui/typeck/dont-suggest-private-dependencies.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Don't suggest importing a function from a private dependency.
// Issues: #138191, #142676

// Avoid suggesting traits from std-private deps
//@ forbid-output: compiler_builtins
//@ forbid-output: object

// Check a custom trait to withstand changes in above crates
//@ aux-crate:public_dep=public-dep.rs
//@ compile-flags: -Zunstable-options
//@ forbid-output: private_dep

struct VecReader(Vec<u8>);

impl std::io::Read for VecReader {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.0.read(buf)
//~^ ERROR no method named `read` found for struct `Vec<u8>`
}
}

extern crate public_dep;
use public_dep::B;

fn main() {
let _ = u8::cast_from_lossy(9);
//~^ ERROR no function or associated item named `cast_from_lossy` found for type `u8`
let _ = B::foo();
//~^ ERROR no function or associated item named `foo` found for struct `B`
}
26 changes: 26 additions & 0 deletions tests/ui/typeck/dont-suggest-private-dependencies.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error[E0599]: no method named `read` found for struct `Vec<u8>` in the current scope
--> $DIR/dont-suggest-private-dependencies.rs:17:16
|
LL | self.0.read(buf)
| ^^^^
|
help: there is a method `read_at` with a similar name
|
LL | self.0.read_at(buf)
| +++

error[E0599]: no function or associated item named `cast_from_lossy` found for type `u8` in the current scope
--> $DIR/dont-suggest-private-dependencies.rs:26:17
|
LL | let _ = u8::cast_from_lossy(9);
| ^^^^^^^^^^^^^^^ function or associated item not found in `u8`

error[E0599]: no function or associated item named `foo` found for struct `B` in the current scope
--> $DIR/dont-suggest-private-dependencies.rs:28:16
|
LL | let _ = B::foo();
| ^^^ function or associated item not found in `B`

error: aborting due to 3 previous errors

For more information about this error, try `rustc --explain E0599`.
Loading