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 r
} }
crate fn find_closest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> { /// Find the nearest parent module of a [`DefId`].
if item.is_fake() { crate fn find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
// FIXME: is this correct? if def_id.is_top_level_module() {
None // The crate root has no parent. Use it as the root instead.
// If we're documenting the crate root itself, it has no parent. Use the root instead. Some(def_id)
} else if item.def_id.is_top_level_module() {
Some(item.def_id)
} else { } else {
let mut current = def_id; let mut current = def_id;
// The immediate parent might not always be a module. // 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_span::def_id::{DefId, CRATE_DEF_INDEX};
use rustc_target::spec::abi::Abi; 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::cache::cache;
use crate::formats::item_type::ItemType; use crate::formats::item_type::ItemType;
use crate::html::escape::Escape; use crate::html::escape::Escape;
@ -1097,7 +1097,7 @@ impl clean::Visibility {
clean::Inherited => Ok(()), clean::Inherited => Ok(()),
clean::Visibility::Restricted(vis_did) => { 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 { if vis_did.index == CRATE_DEF_INDEX {
write!(f, "pub(crate) ") write!(f, "pub(crate) ")
@ -1106,7 +1106,7 @@ impl clean::Visibility {
// is the same as no visibility modifier // is the same as no visibility modifier
Ok(()) Ok(())
} else if parent_module } else if parent_module
.map(|parent| find_closest_parent_module(tcx, parent)) .map(|parent| find_nearest_parent_module(tcx, parent))
.flatten() .flatten()
== Some(vis_did) == Some(vis_did)
{ {

View file

@ -31,7 +31,7 @@ use std::cell::Cell;
use std::mem; use std::mem;
use std::ops::Range; 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::core::DocContext;
use crate::fold::DocFolder; use crate::fold::DocFolder;
use crate::html::markdown::markdown_links; 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> { fn fold_item(&mut self, mut item: Item) -> Option<Item> {
use rustc_middle::ty::DefIdTree; 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() { if parent_node.is_some() {
trace!("got parent node for {:?} {:?}, id {:?}", item.type_(), item.name, item.def_id); trace!("got parent node for {:?} {:?}, id {:?}", item.type_(), item.name, item.def_id);