Skip to content

Commit

Permalink
Pass tcx when push_item instead of store namespace
Browse files Browse the repository at this point in the history
Signed-off-by: longfangsong <longfangsong@icloud.com>
  • Loading branch information
longfangsong committed Apr 7, 2021
1 parent 4076106 commit 7ee35c6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 37 deletions.
23 changes: 10 additions & 13 deletions src/librustdoc/doctree.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! This module is used to store stuff from Rust's AST in a more convenient
//! manner (and with prettier names) before cleaning.
use rustc_span::{self, Span, Symbol};

use crate::core::DocContext;
use rustc_hir as hir;
use rustc_span::{self, Span, Symbol};

/// A wrapper around a [`hir::Item`].
#[derive(Debug)]
Expand All @@ -11,8 +11,6 @@ crate struct Item<'hir> {
crate hir_item: &'hir hir::Item<'hir>,
/// the explicit renamed name
crate renamed_name: Option<Symbol>,
/// the [`Namespace`] this Item belongs to
crate namespace: Option<hir::def::Namespace>,
/// whether the item is from a glob import
/// if `from_glob` is true and we see another item with same name and namespace,
/// then this item can be replaced with that one
Expand All @@ -23,10 +21,9 @@ impl<'hir> Item<'hir> {
pub(crate) fn new(
hir_item: &'hir hir::Item<'hir>,
renamed_name: Option<Symbol>,
namespace: Option<hir::def::Namespace>,
from_glob: bool,
) -> Self {
Self { hir_item, renamed_name, namespace, from_glob }
Self { hir_item, renamed_name, from_glob }
}

pub(crate) fn name(&self) -> Symbol {
Expand Down Expand Up @@ -66,13 +63,13 @@ impl Module<'hir> {
}
}

pub(crate) fn push_item(&mut self, new_item: Item<'hir>) {
if !new_item.name().is_empty() && new_item.namespace.is_some() {
if let Some(existing_item) = self
.items
.iter_mut()
.find(|item| item.name() == new_item.name() && item.namespace == new_item.namespace)
{
pub(crate) fn push_item(&mut self, ctx: &DocContext<'_>, new_item: Item<'hir>) {
let new_item_ns = ctx.tcx.def_kind(new_item.hir_item.def_id).ns();
if !new_item.name().is_empty() && new_item_ns.is_some() {
if let Some(existing_item) = self.items.iter_mut().find(|item| {
item.name() == new_item.name()
&& ctx.tcx.def_kind(item.hir_item.def_id).ns() == new_item_ns
}) {
match (existing_item.from_glob, new_item.from_glob) {
(true, _) => {
// `existing_item` is from glob, no matter whether `new_item` is from glob,
Expand Down
30 changes: 6 additions & 24 deletions src/librustdoc/visit_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,7 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
}
}

om.push_item(Item::new(
item,
renamed,
self.cx.tcx.def_kind(item.def_id).ns(),
from_glob,
))
om.push_item(self.cx, Item::new(item, renamed, from_glob))
}
hir::ItemKind::Mod(ref m) => {
om.push_mod(self.visit_mod_contents(
Expand All @@ -339,34 +334,21 @@ impl<'a, 'tcx> RustdocVisitor<'a, 'tcx> {
| hir::ItemKind::OpaqueTy(..)
| hir::ItemKind::Static(..)
| hir::ItemKind::Trait(..)
| hir::ItemKind::TraitAlias(..) => om.push_item(Item::new(
item,
renamed,
self.cx.tcx.def_kind(item.def_id).ns(),
from_glob,
)),
| hir::ItemKind::TraitAlias(..) => {
om.push_item(self.cx, Item::new(item, renamed, from_glob))
}
hir::ItemKind::Const(..) => {
// Underscore constants do not correspond to a nameable item and
// so are never useful in documentation.
if name != kw::Underscore {
om.push_item(Item::new(
item,
renamed,
self.cx.tcx.def_kind(item.def_id).ns(),
from_glob,
));
om.push_item(self.cx, Item::new(item, renamed, from_glob));
}
}
hir::ItemKind::Impl(ref impl_) => {
// Don't duplicate impls when inlining or if it's implementing a trait, we'll pick
// them up regardless of where they're located.
if !self.inlining && impl_.of_trait.is_none() {
om.push_item(Item::new(
item,
None,
self.cx.tcx.def_kind(item.def_id).ns(),
from_glob,
));
om.push_item(self.cx, Item::new(item, None, from_glob));
}
}
}
Expand Down

0 comments on commit 7ee35c6

Please sign in to comment.