1
Fork 0

Update find_nearest_parent_module

This commit is contained in:
Camelid 2020-12-30 16:38:25 -08:00
parent 478cbb0095
commit 75705ab3a9
3 changed files with 15 additions and 12 deletions

View file

@ -625,13 +625,11 @@ where
r
}
crate fn find_closest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
if item.is_fake() {
// FIXME: is this correct?
None
// If we're documenting the crate root itself, it has no parent. Use the root instead.
} else if item.def_id.is_top_level_module() {
Some(item.def_id)
/// Find the nearest parent module of a [`DefId`].
crate fn find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
if def_id.is_top_level_module() {
// The crate root has no parent. Use it as the root instead.
Some(def_id)
} else {
let mut current = def_id;
// The immediate parent might not always be a module.

View file

@ -15,7 +15,7 @@ use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
use rustc_target::spec::abi::Abi;
use crate::clean::{self, utils::find_closest_parent_module, PrimitiveType};
use crate::clean::{self, utils::find_nearest_parent_module, PrimitiveType};
use crate::formats::cache::cache;
use crate::formats::item_type::ItemType;
use crate::html::escape::Escape;
@ -1097,7 +1097,7 @@ impl clean::Visibility {
clean::Inherited => Ok(()),
clean::Visibility::Restricted(vis_did) => {
let parent_module = find_closest_parent_module(tcx, item_did);
let parent_module = find_nearest_parent_module(tcx, item_did);
if vis_did.index == CRATE_DEF_INDEX {
write!(f, "pub(crate) ")
@ -1106,7 +1106,7 @@ impl clean::Visibility {
// is the same as no visibility modifier
Ok(())
} else if parent_module
.map(|parent| find_closest_parent_module(tcx, parent))
.map(|parent| find_nearest_parent_module(tcx, parent))
.flatten()
== Some(vis_did)
{

View file

@ -31,7 +31,7 @@ use std::cell::Cell;
use std::mem;
use std::ops::Range;
use crate::clean::{self, utils::find_closest_parent_module, Crate, Item, ItemLink, PrimitiveType};
use crate::clean::{self, utils::find_nearest_parent_module, Crate, Item, ItemLink, PrimitiveType};
use crate::core::DocContext;
use crate::fold::DocFolder;
use crate::html::markdown::markdown_links;
@ -767,7 +767,12 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
fn fold_item(&mut self, mut item: Item) -> Option<Item> {
use rustc_middle::ty::DefIdTree;
let parent_node = find_closest_parent_module(self.cx.tcx, item.def_id);
let parent_node = if item.is_fake() {
// FIXME: is this correct?
None
} else {
find_nearest_parent_module(self.cx.tcx, item.def_id)
};
if parent_node.is_some() {
trace!("got parent node for {:?} {:?}, id {:?}", item.type_(), item.name, item.def_id);