Skip to content

Commit fde8d11

Browse files
committed
Don't pollute docs/suggestions with libstd deps
Currently dependency crates of the standard library can sometimes leak into error messages such as when traits to import are suggested. Additionally they can leak into documentation such as in the list of "all traits implemented by `u32`". The dependencies of the standard library, however, are intended to be private. The dependencies of the standard library can't actually be stabl-y imported nor is the documentation that relevant since you can't import them on stable either. This commit updates both the compiler and rustdoc to ignore unstable traits in these two scenarios. Specifically the suggestion for traits to import ignore unstable traits, and similarly the list of traits implemented by a type excludes unstable traits. This commit is extracted from #73441 where the addition of some new dependencies to the standard library was showed to leak into various error messages and documentation. The intention here is to go ahead and land these changes ahead of that since it will likely take some time to land.
1 parent 9672b5e commit fde8d11

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/librustc_typeck/check/method/suggest.rs

+6
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
937937
// legal to implement.
938938
let mut candidates = all_traits(self.tcx)
939939
.into_iter()
940+
// Don't issue suggestions for unstable traits since they're
941+
// unlikely to be implementable anyway
942+
.filter(|info| match self.tcx.lookup_stability(info.def_id) {
943+
Some(attr) => attr.level.is_stable(),
944+
None => true,
945+
})
940946
.filter(|info| {
941947
// We approximate the coherence rules to only suggest
942948
// traits that are legal to implement by requiring that

src/librustdoc/clean/inline.rs

+10
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ pub fn build_impl(
339339
return;
340340
}
341341
}
342+
343+
// Skip foreign unstable traits from lists of trait implementations and
344+
// such. This helps prevent dependencies of the standard library, for
345+
// example, from getting documented as "traits `u32` implements" which
346+
// isn't really too helpful.
347+
if let Some(stab) = cx.tcx.lookup_stability(did) {
348+
if stab.level.is_unstable() {
349+
return;
350+
}
351+
}
342352
}
343353

344354
let for_ = if let Some(did) = did.as_local() {

0 commit comments

Comments
 (0)