]> git.lizzy.rs Git - rust.git/commitdiff
Don't pollute docs/suggestions with libstd deps
authorAlex Crichton <alex@alexcrichton.com>
Wed, 17 Jun 2020 22:08:37 +0000 (15:08 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Fri, 26 Jun 2020 16:23:00 +0000 (09:23 -0700)
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.

src/librustc_typeck/check/method/suggest.rs
src/librustdoc/clean/inline.rs

index 67bdd04d3715c8fc17ae2a2c2011034f71e5058d..9c4873bccb299397c0f400b4a33ebb3543d0b1fd 100644 (file)
@@ -937,6 +937,12 @@ fn suggest_traits_to_import<'b>(
         // legal to implement.
         let mut candidates = all_traits(self.tcx)
             .into_iter()
+            // Don't issue suggestions for unstable traits since they're
+            // unlikely to be implementable anyway
+            .filter(|info| match self.tcx.lookup_stability(info.def_id) {
+                Some(attr) => attr.level.is_stable(),
+                None => true,
+            })
             .filter(|info| {
                 // We approximate the coherence rules to only suggest
                 // traits that are legal to implement by requiring that
index 78628b198a3c3f73ab6740774273aecf31825f73..ee1c17bad9f938862eaf0ca5faa33fb4712d4eb7 100644 (file)
@@ -339,6 +339,16 @@ pub fn build_impl(
                 return;
             }
         }
+
+        // Skip foreign unstable traits from lists of trait implementations and
+        // such. This helps prevent dependencies of the standard library, for
+        // example, from getting documented as "traits `u32` implements" which
+        // isn't really too helpful.
+        if let Some(stab) = cx.tcx.lookup_stability(did) {
+            if stab.level.is_unstable() {
+                return;
+            }
+        }
     }
 
     let for_ = if let Some(did) = did.as_local() {