Skip to content

Commit a04ed09

Browse files
committed
Use visible_crates rather than crates for solving and diagnostics
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: #135232
1 parent a59eea6 commit a04ed09

File tree

5 files changed

+14
-12
lines changed

5 files changed

+14
-12
lines changed

compiler/rustc_middle/src/ty/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2078,9 +2078,10 @@ impl<'tcx> TyCtxt<'tcx> {
20782078
self.limits(()).move_size_limit
20792079
}
20802080

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

compiler/rustc_passes/src/diagnostic_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn all_diagnostic_items(tcx: TyCtxt<'_>, (): ()) -> DiagnosticItems {
8080
let mut items = DiagnosticItems::default();
8181

8282
// Collect diagnostic items in other crates.
83-
for &cnum in tcx.crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
83+
for &cnum in tcx.visible_crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
8484
for (&name, &def_id) in &tcx.diagnostic_items(cnum).name_to_id {
8585
collect_item(tcx, &mut items, name, def_id);
8686
}

tests/ui/std/sysroot-private.default.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0405]: cannot find trait `Equivalent` in this scope
2-
--> $DIR/sysroot-private.rs:24:18
2+
--> $DIR/sysroot-private.rs:25:18
33
|
44
LL | trait Trait2<K>: Equivalent<K> {}
55
| ^^^^^^^^^^ not found in this scope
66

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

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

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

tests/ui/std/sysroot-private.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@
1616
trait Trait { type Bar; }
1717

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

2324
// Attempt to get a suggestion for `hashbrown::Equivalent`
2425
trait Trait2<K>: Equivalent<K> {}

tests/ui/std/sysroot-private.rustc_private_enabled.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0405]: cannot find trait `Equivalent` in this scope
2-
--> $DIR/sysroot-private.rs:24:18
2+
--> $DIR/sysroot-private.rs:25:18
33
|
44
LL | trait Trait2<K>: Equivalent<K> {}
55
| ^^^^^^^^^^ not found in this scope
66

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

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

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

0 commit comments

Comments
 (0)