]> git.lizzy.rs Git - rust.git/commitdiff
Fix ?Sized where bound not being displayed at the correct place
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Sun, 17 Dec 2017 13:08:09 +0000 (14:08 +0100)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Mon, 18 Dec 2017 09:44:26 +0000 (10:44 +0100)
src/librustdoc/clean/inline.rs
src/librustdoc/clean/mod.rs
src/librustdoc/clean/simplify.rs
src/test/rustdoc/where-sized.rs [new file with mode: 0644]

index 60592cc186ed862a608eee18f68ece274ba50e11..914365b003e18472a7a909020138e70daca7bd61 100644 (file)
@@ -448,8 +448,7 @@ fn build_static(cx: &DocContext, did: DefId, mutable: bool) -> clean::Static {
 ///
 /// The inverse of this filtering logic can be found in the `Clean`
 /// implementation for `AssociatedType`
-fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics)
-                             -> clean::Generics {
+fn filter_non_trait_generics(trait_did: DefId, mut g: clean::Generics) -> clean::Generics {
     for pred in &mut g.where_predicates {
         match *pred {
             clean::WherePredicate::BoundPredicate {
index 91908de98a65d89a1117adb9218cbd3a22e45c82..fe7dfc4477f96ce38c3e279e5d1a89dc4d8d44c1 100644 (file)
@@ -1190,16 +1190,36 @@ fn clean(&self, cx: &DocContext) -> Type {
 pub struct Generics {
     pub lifetimes: Vec<Lifetime>,
     pub type_params: Vec<TyParam>,
-    pub where_predicates: Vec<WherePredicate>
+    pub where_predicates: Vec<WherePredicate>,
 }
 
 impl Clean<Generics> for hir::Generics {
     fn clean(&self, cx: &DocContext) -> Generics {
-        Generics {
+        let mut g = Generics {
             lifetimes: self.lifetimes.clean(cx),
             type_params: self.ty_params.clean(cx),
             where_predicates: self.where_clause.predicates.clean(cx)
+        };
+
+        // Some duplicates are generated for ?Sized bounds between type params and where
+        // predicates. The point in here is to move the bounds definitions from type params
+        // to where predicates when such cases occur.
+        for where_pred in &mut g.where_predicates {
+            match *where_pred {
+                WherePredicate::BoundPredicate { ty: Generic(ref name), ref mut bounds } => {
+                    if bounds.is_empty() {
+                        for type_params in &mut g.type_params {
+                            if &type_params.name == name {
+                                mem::swap(bounds, &mut type_params.bounds);
+                                break
+                            }
+                        }
+                    }
+                }
+                _ => continue,
+            }
         }
+        g
     }
 }
 
@@ -1225,7 +1245,7 @@ fn clean(&self, cx: &DocContext) -> Generics {
         let mut where_predicates = preds.predicates.to_vec().clean(cx);
 
         // Type parameters and have a Sized bound by default unless removed with
-        // ?Sized.  Scan through the predicates and mark any type parameter with
+        // ?Sized. Scan through the predicates and mark any type parameter with
         // a Sized bound, removing the bounds as we find them.
         //
         // Note that associated types also have a sized bound by default, but we
index dd36b28bb39acd6dcf0b9b4f360aa0624bf80648..63ebb16e5e0096a961e4574829d2a14040cda39a 100644 (file)
@@ -38,6 +38,7 @@ pub fn where_clauses(cx: &DocContext, clauses: Vec<WP>) -> Vec<WP> {
     let mut lifetimes = Vec::new();
     let mut equalities = Vec::new();
     let mut tybounds = Vec::new();
+
     for clause in clauses {
         match clause {
             WP::BoundPredicate { ty, bounds } => {
diff --git a/src/test/rustdoc/where-sized.rs b/src/test/rustdoc/where-sized.rs
new file mode 100644 (file)
index 0000000..c175c83
--- /dev/null
@@ -0,0 +1,16 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+#![crate_name = "foo"]
+
+// @has foo/fn.foo.html
+// @has - '//*[@class="rust fn"]' 'pub fn foo<X, Y: ?Sized>(_: &X)'
+// @has - '//*[@class="rust fn"]' 'where X: ?Sized,'
+pub fn foo<X, Y: ?Sized>(_: &X) where X: ?Sized {}