]> git.lizzy.rs Git - rust.git/commitdiff
Remove duplicates in rustdoc
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Thu, 17 Aug 2017 22:08:12 +0000 (00:08 +0200)
committerGuillaume Gomez <guillaume1.gomez@gmail.com>
Thu, 24 Aug 2017 09:38:58 +0000 (11:38 +0200)
src/librustdoc/clean/mod.rs
src/librustdoc/html/render.rs

index f4aef8ab377c01f53d2421b4e61769386d730368..1df0ac337a700902688391fb6d3637a84c32a0bb 100644 (file)
@@ -323,6 +323,10 @@ pub fn is_primitive(&self) -> bool {
     pub fn is_union(&self) -> bool {
         self.type_() == ItemType::Union
     }
+    pub fn is_import(&self) -> bool {
+        self.type_() == ItemType::Import
+    }
+
     pub fn is_stripped(&self) -> bool {
         match self.inner { StrippedItem(..) => true, _ => false }
     }
index 6593d6dfd6cff7ff105ff7cf425b5bd0b9bc725a..e113165b9ab0c838c561c53b0bb97861740ef88c 100644 (file)
@@ -1764,6 +1764,37 @@ fn cmp(i1: &clean::Item, i2: &clean::Item, idx1: usize, idx2: usize) -> Ordering
     }
 
     indices.sort_by(|&i1, &i2| cmp(&items[i1], &items[i2], i1, i2));
+    // This call is to remove reexport duplicates in cases such as:
+    //
+    // ```
+    // pub mod foo {
+    //     pub mod bar {
+    //         pub trait Double { fn foo(); }
+    //     }
+    // }
+    //
+    // pub use foo::bar::*;
+    // pub use foo::*;
+    // ```
+    //
+    // `Double` will appear twice in the generated docs.
+    //
+    // FIXME: This code is quite ugly and could be improved. Small issue: DefId
+    // can be identical even if the elements are different (mostly in imports).
+    // So in case this is an import, we keep everything by adding a "unique id"
+    // (which is the position in the vector).
+    indices.dedup_by_key(|i| (items[*i].def_id,
+                              if items[*i].name.as_ref().is_some() {
+                                  Some(full_path(cx, &items[*i]).clone())
+                              } else {
+                                  None
+                              },
+                              items[*i].type_(),
+                              if items[*i].is_import() {
+                                  *i
+                              } else {
+                                  0
+                              }));
 
     debug!("{:?}", indices);
     let mut curty = None;