Skip to content

Commit e9a6dcc

Browse files
authored
Rollup merge of rust-lang#43966 - GuillaumeGomez:remove-dup, r=QuietMisdreavus
Remove duplicates in rustdoc Fixes rust-lang#43934. Two things however: 1. I'm not happy with the current check. It seems completely overkill and unsatisfying. 2. I have no idea how to test if there is only one element and not two. r? @rust-lang/docs
2 parents 1412ff5 + 5d71280 commit e9a6dcc

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ impl Item {
323323
pub fn is_union(&self) -> bool {
324324
self.type_() == ItemType::Union
325325
}
326+
pub fn is_import(&self) -> bool {
327+
self.type_() == ItemType::Import
328+
}
329+
326330
pub fn is_stripped(&self) -> bool {
327331
match self.inner { StrippedItem(..) => true, _ => false }
328332
}

src/librustdoc/html/render.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,6 +1764,37 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
17641764
}
17651765

17661766
indices.sort_by(|&i1, &i2| cmp(&items[i1], &items[i2], i1, i2));
1767+
// This call is to remove reexport duplicates in cases such as:
1768+
//
1769+
// ```
1770+
// pub mod foo {
1771+
// pub mod bar {
1772+
// pub trait Double { fn foo(); }
1773+
// }
1774+
// }
1775+
//
1776+
// pub use foo::bar::*;
1777+
// pub use foo::*;
1778+
// ```
1779+
//
1780+
// `Double` will appear twice in the generated docs.
1781+
//
1782+
// FIXME: This code is quite ugly and could be improved. Small issue: DefId
1783+
// can be identical even if the elements are different (mostly in imports).
1784+
// So in case this is an import, we keep everything by adding a "unique id"
1785+
// (which is the position in the vector).
1786+
indices.dedup_by_key(|i| (items[*i].def_id,
1787+
if items[*i].name.as_ref().is_some() {
1788+
Some(full_path(cx, &items[*i]).clone())
1789+
} else {
1790+
None
1791+
},
1792+
items[*i].type_(),
1793+
if items[*i].is_import() {
1794+
*i
1795+
} else {
1796+
0
1797+
}));
17671798

17681799
debug!("{:?}", indices);
17691800
let mut curty = None;

src/test/rustdoc/remove-duplicates.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_name = "foo"]
12+
13+
mod foo {
14+
pub use bar::*;
15+
pub mod bar {
16+
pub trait Foo {
17+
fn foo();
18+
}
19+
}
20+
}
21+
22+
// @count foo/index.html '//*[@class="trait"]' 1
23+
pub use foo::bar::*;
24+
pub use foo::*;

0 commit comments

Comments
 (0)