Skip to content

Commit

Permalink
Use visible_crates rather than crates for solving and diagnostics
Browse files Browse the repository at this point in the history
For places where we don't want stdlib-private crates to be accessible,
use `.visible_crates()` rather than `.crates()`. The current effect is
that items in stdlib-private crates will no longer show up in
diagnostics related to trait selection.

Fixes: rust-lang#135232
  • Loading branch information
tgross35 committed Jan 13, 2025
1 parent a59eea6 commit a04ed09
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2078,9 +2078,10 @@ impl<'tcx> TyCtxt<'tcx> {
self.limits(()).move_size_limit
}

/// All traits that are visible within the crate graph (i.e. excluding private dependencies).
pub fn all_traits(self) -> impl Iterator<Item = DefId> + 'tcx {
iter::once(LOCAL_CRATE)
.chain(self.crates(()).iter().copied())
.chain(self.visible_crates(()).iter().copied())
.flat_map(move |cnum| self.traits(cnum).iter().copied())
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/diagnostic_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn all_diagnostic_items(tcx: TyCtxt<'_>, (): ()) -> DiagnosticItems {
let mut items = DiagnosticItems::default();

// Collect diagnostic items in other crates.
for &cnum in tcx.crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
for &cnum in tcx.visible_crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
for (&name, &def_id) in &tcx.diagnostic_items(cnum).name_to_id {
collect_item(tcx, &mut items, name, def_id);
}
Expand Down
10 changes: 5 additions & 5 deletions tests/ui/std/sysroot-private.default.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0405]: cannot find trait `Equivalent` in this scope
--> $DIR/sysroot-private.rs:24:18
--> $DIR/sysroot-private.rs:25:18
|
LL | trait Trait2<K>: Equivalent<K> {}
| ^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `K` in this scope
--> $DIR/sysroot-private.rs:29:35
--> $DIR/sysroot-private.rs:30:35
|
LL | fn trait_member<T>(val: &T, key: &K) -> bool {
| - ^
Expand All @@ -22,13 +22,13 @@ LL | fn trait_member<T, K>(val: &T, key: &K) -> bool {
| +++

error[E0220]: associated type `Buf` not found for `Trait`
--> $DIR/sysroot-private.rs:19:31
--> $DIR/sysroot-private.rs:20:31
|
LL | type AssociatedTy = dyn Trait<Buf = i32, Bar = i32>;
| ^^^ there is an associated type `Buf` in the trait `addr2line::lookup::LookupContinuation`
| ^^^ help: `Trait` has the following associated type: `Bar`

error[E0425]: cannot find function `memchr2` in this scope
--> $DIR/sysroot-private.rs:37:5
--> $DIR/sysroot-private.rs:38:5
|
LL | memchr2(b'a', b'b', buf)
| ^^^^^^^ not found in this scope
Expand Down
3 changes: 2 additions & 1 deletion tests/ui/std/sysroot-private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
trait Trait { type Bar; }

// Attempt to get a suggestion for `addr2line::LookupContinuation`, which has member `Buf`
// Note that the suggestion only happens when `rustc_private` is enabled.
type AssociatedTy = dyn Trait<Buf = i32, Bar = i32>;
//~^ ERROR associated type `Buf` not found
//~| NOTE there is an associated type `Buf` in the trait `addr2line::lookup::LookupContinuation`
//[rustc_private_enabled]~| NOTE there is an associated type `Buf` in the trait `addr2line::lookup::LookupContinuation`

// Attempt to get a suggestion for `hashbrown::Equivalent`
trait Trait2<K>: Equivalent<K> {}
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/std/sysroot-private.rustc_private_enabled.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0405]: cannot find trait `Equivalent` in this scope
--> $DIR/sysroot-private.rs:24:18
--> $DIR/sysroot-private.rs:25:18
|
LL | trait Trait2<K>: Equivalent<K> {}
| ^^^^^^^^^^ not found in this scope

error[E0412]: cannot find type `K` in this scope
--> $DIR/sysroot-private.rs:29:35
--> $DIR/sysroot-private.rs:30:35
|
LL | fn trait_member<T>(val: &T, key: &K) -> bool {
| - ^
Expand All @@ -22,13 +22,13 @@ LL | fn trait_member<T, K>(val: &T, key: &K) -> bool {
| +++

error[E0220]: associated type `Buf` not found for `Trait`
--> $DIR/sysroot-private.rs:19:31
--> $DIR/sysroot-private.rs:20:31
|
LL | type AssociatedTy = dyn Trait<Buf = i32, Bar = i32>;
| ^^^ there is an associated type `Buf` in the trait `addr2line::lookup::LookupContinuation`

error[E0425]: cannot find function `memchr2` in this scope
--> $DIR/sysroot-private.rs:37:5
--> $DIR/sysroot-private.rs:38:5
|
LL | memchr2(b'a', b'b', buf)
| ^^^^^^^ not found in this scope
Expand Down

0 comments on commit a04ed09

Please sign in to comment.