Skip to content

Commit 7ee35c6

Browse files
committed
Pass tcx when push_item instead of store namespace
Signed-off-by: longfangsong <[email protected]>
1 parent 4076106 commit 7ee35c6

File tree

2 files changed

+16
-37
lines changed

2 files changed

+16
-37
lines changed

src/librustdoc/doctree.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! This module is used to store stuff from Rust's AST in a more convenient
22
//! manner (and with prettier names) before cleaning.
3-
use rustc_span::{self, Span, Symbol};
4-
3+
use crate::core::DocContext;
54
use rustc_hir as hir;
5+
use rustc_span::{self, Span, Symbol};
66

77
/// A wrapper around a [`hir::Item`].
88
#[derive(Debug)]
@@ -11,8 +11,6 @@ crate struct Item<'hir> {
1111
crate hir_item: &'hir hir::Item<'hir>,
1212
/// the explicit renamed name
1313
crate renamed_name: Option<Symbol>,
14-
/// the [`Namespace`] this Item belongs to
15-
crate namespace: Option<hir::def::Namespace>,
1614
/// whether the item is from a glob import
1715
/// if `from_glob` is true and we see another item with same name and namespace,
1816
/// then this item can be replaced with that one
@@ -23,10 +21,9 @@ impl<'hir> Item<'hir> {
2321
pub(crate) fn new(
2422
hir_item: &'hir hir::Item<'hir>,
2523
renamed_name: Option<Symbol>,
26-
namespace: Option<hir::def::Namespace>,
2724
from_glob: bool,
2825
) -> Self {
29-
Self { hir_item, renamed_name, namespace, from_glob }
26+
Self { hir_item, renamed_name, from_glob }
3027
}
3128

3229
pub(crate) fn name(&self) -> Symbol {
@@ -66,13 +63,13 @@ impl Module<'hir> {
6663
}
6764
}
6865

69-
pub(crate) fn push_item(&mut self, new_item: Item<'hir>) {
70-
if !new_item.name().is_empty() && new_item.namespace.is_some() {
71-
if let Some(existing_item) = self
72-
.items
73-
.iter_mut()
74-
.find(|item| item.name() == new_item.name() && item.namespace == new_item.namespace)
75-
{
66+
pub(crate) fn push_item(&mut self, ctx: &DocContext<'_>, new_item: Item<'hir>) {
67+
let new_item_ns = ctx.tcx.def_kind(new_item.hir_item.def_id).ns();
68+
if !new_item.name().is_empty() && new_item_ns.is_some() {
69+
if let Some(existing_item) = self.items.iter_mut().find(|item| {
70+
item.name() == new_item.name()
71+
&& ctx.tcx.def_kind(item.hir_item.def_id).ns() == new_item_ns
72+
}) {
7673
match (existing_item.from_glob, new_item.from_glob) {
7774
(true, _) => {
7875
// `existing_item` is from glob, no matter whether `new_item` is from glob,

src/librustdoc/visit_ast.rs

+6-24
Original file line numberDiff line numberDiff line change
@@ -313,12 +313,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
313313
}
314314
}
315315

316-
om.push_item(Item::new(
317-
item,
318-
renamed,
319-
self.cx.tcx.def_kind(item.def_id).ns(),
320-
from_glob,
321-
))
316+
om.push_item(self.cx, Item::new(item, renamed, from_glob))
322317
}
323318
hir::ItemKind::Mod(ref m) => {
324319
om.push_mod(self.visit_mod_contents(
@@ -339,34 +334,21 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
339334
| hir::ItemKind::OpaqueTy(..)
340335
| hir::ItemKind::Static(..)
341336
| hir::ItemKind::Trait(..)
342-
| hir::ItemKind::TraitAlias(..) => om.push_item(Item::new(
343-
item,
344-
renamed,
345-
self.cx.tcx.def_kind(item.def_id).ns(),
346-
from_glob,
347-
)),
337+
| hir::ItemKind::TraitAlias(..) => {
338+
om.push_item(self.cx, Item::new(item, renamed, from_glob))
339+
}
348340
hir::ItemKind::Const(..) => {
349341
// Underscore constants do not correspond to a nameable item and
350342
// so are never useful in documentation.
351343
if name != kw::Underscore {
352-
om.push_item(Item::new(
353-
item,
354-
renamed,
355-
self.cx.tcx.def_kind(item.def_id).ns(),
356-
from_glob,
357-
));
344+
om.push_item(self.cx, Item::new(item, renamed, from_glob));
358345
}
359346
}
360347
hir::ItemKind::Impl(ref impl_) => {
361348
// Don't duplicate impls when inlining or if it's implementing a trait, we'll pick
362349
// them up regardless of where they're located.
363350
if !self.inlining && impl_.of_trait.is_none() {
364-
om.push_item(Item::new(
365-
item,
366-
None,
367-
self.cx.tcx.def_kind(item.def_id).ns(),
368-
from_glob,
369-
));
351+
om.push_item(self.cx, Item::new(item, None, from_glob));
370352
}
371353
}
372354
}

0 commit comments

Comments
 (0)