Skip to content

Commit 4aa142e

Browse files
Prevent duplicated imports
1 parent a9dba83 commit 4aa142e

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
7777
// This covers the case where somebody does an import which should pull in an item,
7878
// but there's already an item with the same namespace and same name. Rust gives
7979
// priority to the not-imported one, so we should, too.
80-
items.extend(doc.items.iter().flat_map(|(item, renamed, import_id)| {
80+
items.extend(doc.items.values().flat_map(|(item, renamed, import_id)| {
8181
// First, lower everything other than imports.
8282
if matches!(item.kind, hir::ItemKind::Use(_, hir::UseKind::Glob)) {
8383
return Vec::new();
@@ -90,7 +90,7 @@ pub(crate) fn clean_doc_module<'tcx>(doc: &DocModule<'tcx>, cx: &mut DocContext<
9090
}
9191
v
9292
}));
93-
items.extend(doc.items.iter().flat_map(|(item, renamed, _)| {
93+
items.extend(doc.items.values().flat_map(|(item, renamed, _)| {
9494
// Now we actually lower the imports, skipping everything else.
9595
if let hir::ItemKind::Use(path, hir::UseKind::Glob) = item.kind {
9696
let name = renamed.unwrap_or_else(|| cx.tcx.hir().name(item.hir_id()));

src/librustdoc/visit_ast.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! The Rust AST Visitor. Extracts useful information and massages it into a form
22
//! usable for `clean`.
33
4-
use rustc_data_structures::fx::FxHashSet;
4+
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
55
use rustc_hir as hir;
66
use rustc_hir::def::{DefKind, Res};
77
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LocalDefIdSet};
@@ -26,8 +26,12 @@ pub(crate) struct Module<'hir> {
2626
pub(crate) where_inner: Span,
2727
pub(crate) mods: Vec<Module<'hir>>,
2828
pub(crate) def_id: LocalDefId,
29-
// (item, renamed, import_id)
30-
pub(crate) items: Vec<(&'hir hir::Item<'hir>, Option<Symbol>, Option<LocalDefId>)>,
29+
/// The key is the item `ItemId` and the value is: (item, renamed, import_id).
30+
/// We use `FxIndexMap` to keep the insert order.
31+
pub(crate) items: FxIndexMap<
32+
(LocalDefId, Option<Symbol>),
33+
(&'hir hir::Item<'hir>, Option<Symbol>, Option<LocalDefId>),
34+
>,
3135
pub(crate) foreigns: Vec<(&'hir hir::ForeignItem<'hir>, Option<Symbol>)>,
3236
}
3337

@@ -38,7 +42,7 @@ impl Module<'_> {
3842
def_id,
3943
where_inner,
4044
mods: Vec::new(),
41-
items: Vec::new(),
45+
items: FxIndexMap::default(),
4246
foreigns: Vec::new(),
4347
}
4448
}
@@ -136,7 +140,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
136140
inserted.insert(def_id)
137141
{
138142
let item = self.cx.tcx.hir().expect_item(local_def_id);
139-
top_level_module.items.push((item, None, None));
143+
top_level_module.items.insert((local_def_id, Some(item.ident.name)), (item, None, None));
140144
}
141145
}
142146

@@ -294,7 +298,11 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
294298
renamed: Option<Symbol>,
295299
parent_id: Option<LocalDefId>,
296300
) {
297-
self.modules.last_mut().unwrap().items.push((item, renamed, parent_id))
301+
self.modules
302+
.last_mut()
303+
.unwrap()
304+
.items
305+
.insert((item.owner_id.def_id, renamed), (item, renamed, parent_id));
298306
}
299307

300308
fn visit_item_inner(

0 commit comments

Comments
 (0)