Skip to content

Commit 2f39595

Browse files
authored
Rollup merge of rust-lang#143038 - Qelxiros:142676-private-dependency-traits, r=tgross35
avoid suggesting traits from private dependencies fixes rust-lang#142676 fixes rust-lang#138191 r? `@tgross35`
2 parents f7231b3 + 51aa76f commit 2f39595

File tree

12 files changed

+90
-11
lines changed

12 files changed

+90
-11
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(
@@ -3481,9 +3481,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
34813481
&self,
34823482
err: &mut Diag<'_>,
34833483
item_name: Ident,
3484-
valid_out_of_scope_traits: Vec<DefId>,
3484+
mut valid_out_of_scope_traits: Vec<DefId>,
34853485
explain: bool,
34863486
) -> bool {
3487+
valid_out_of_scope_traits.retain(|id| self.tcx.is_user_visible_dep(id.krate));
34873488
if !valid_out_of_scope_traits.is_empty() {
34883489
let mut candidates = valid_out_of_scope_traits;
34893490
candidates.sort_by_key(|id| self.tcx.def_path_str(id));
@@ -4388,7 +4389,7 @@ pub(crate) struct TraitInfo {
43884389
/// Retrieves all traits in this crate and any dependent crates,
43894390
/// and wraps them into `TraitInfo` for custom sorting.
43904391
pub(crate) fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {
4391-
tcx.all_traits().map(|def_id| TraitInfo { def_id }).collect()
4392+
tcx.all_traits_including_private().map(|def_id| TraitInfo { def_id }).collect()
43924393
}
43934394

43944395
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
@@ -2288,7 +2288,7 @@ impl<'tcx> TyCtxt<'tcx> {
22882288
}
22892289

22902290
/// All traits in the crate graph, including those not visible to the user.
2291-
pub fn all_traits(self) -> impl Iterator<Item = DefId> {
2291+
pub fn all_traits_including_private(self) -> impl Iterator<Item = DefId> {
22922292
iter::once(LOCAL_CRATE)
22932293
.chain(self.crates(()).iter().copied())
22942294
.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
@@ -1855,7 +1855,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
18551855
let trait_def_id = trait_pred.def_id();
18561856
let trait_name = self.tcx.item_name(trait_def_id);
18571857
let crate_name = self.tcx.crate_name(trait_def_id.krate);
1858-
if let Some(other_trait_def_id) = self.tcx.all_traits().find(|def_id| {
1858+
if let Some(other_trait_def_id) = self.tcx.all_traits_including_private().find(|def_id| {
18591859
trait_name == self.tcx.item_name(trait_def_id)
18601860
&& trait_def_id.krate != def_id.krate
18611861
&& crate_name == self.tcx.crate_name(def_id.krate)

library/rustc-std-workspace-core/Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
cargo-features = ["public-dependency"]
2+
13
[package]
24
name = "rustc-std-workspace-core"
35
version = "1.99.0"
@@ -11,5 +13,7 @@ edition = "2024"
1113
path = "lib.rs"
1214

1315
[dependencies]
14-
core = { path = "../core" }
15-
compiler_builtins = { path = "../compiler-builtins/compiler-builtins", features = ["compiler-builtins"] }
16+
core = { path = "../core", public = true }
17+
compiler_builtins = { path = "../compiler-builtins/compiler-builtins", features = [
18+
"compiler-builtins",
19+
] }

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,
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
pub trait A {
2+
fn foo() {}
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ aux-crate:priv:private_dep=private-dep.rs
2+
//@ compile-flags: -Zunstable-options
3+
4+
extern crate private_dep;
5+
use private_dep::A;
6+
7+
pub struct B;
8+
9+
impl A for B {
10+
fn foo() {}
11+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Don't suggest importing a function from a private dependency.
2+
// Issues: #138191, #142676
3+
4+
// Avoid suggesting traits from std-private deps
5+
//@ forbid-output: compiler_builtins
6+
//@ forbid-output: object
7+
8+
// Check a custom trait to withstand changes in above crates
9+
//@ aux-crate:public_dep=public-dep.rs
10+
//@ compile-flags: -Zunstable-options
11+
//@ forbid-output: private_dep
12+
13+
struct VecReader(Vec<u8>);
14+
15+
impl std::io::Read for VecReader {
16+
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
17+
self.0.read(buf)
18+
//~^ ERROR no method named `read` found for struct `Vec<u8>`
19+
}
20+
}
21+
22+
extern crate public_dep;
23+
use public_dep::B;
24+
25+
fn main() {
26+
let _ = u8::cast_from_lossy(9);
27+
//~^ ERROR no function or associated item named `cast_from_lossy` found for type `u8`
28+
let _ = B::foo();
29+
//~^ ERROR no function or associated item named `foo` found for struct `B`
30+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0599]: no method named `read` found for struct `Vec<u8>` in the current scope
2+
--> $DIR/dont-suggest-private-dependencies.rs:17:16
3+
|
4+
LL | self.0.read(buf)
5+
| ^^^^
6+
|
7+
help: there is a method `read_at` with a similar name
8+
|
9+
LL | self.0.read_at(buf)
10+
| +++
11+
12+
error[E0599]: no function or associated item named `cast_from_lossy` found for type `u8` in the current scope
13+
--> $DIR/dont-suggest-private-dependencies.rs:26:17
14+
|
15+
LL | let _ = u8::cast_from_lossy(9);
16+
| ^^^^^^^^^^^^^^^ function or associated item not found in `u8`
17+
18+
error[E0599]: no function or associated item named `foo` found for struct `B` in the current scope
19+
--> $DIR/dont-suggest-private-dependencies.rs:28:16
20+
|
21+
LL | let _ = B::foo();
22+
| ^^^ function or associated item not found in `B`
23+
24+
error: aborting due to 3 previous errors
25+
26+
For more information about this error, try `rustc --explain E0599`.

0 commit comments

Comments
 (0)