Skip to content

Commit 859a707

Browse files
committed
Move rustdoc::clean::utils::find_nearest_parent_module to TyCtxt
`find_nearest_parent_module` was extracted from `rustdoc::passes::collect_intra_doc_links` in rust-lang#80368. We (me and Joshua Nelson) thought at the time that it should be in rustc instead, and Joshua suggested it be a method on `TyCtxt`. However, since rust-lang#80368 was fixing a significant regression that was about to land on beta, we agreed that to be able to fix this quickly I should leave the code in rustdoc. This is a followup PR to move it to `TyCtxt`.
1 parent da305a2 commit 859a707

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

compiler/rustc_middle/src/ty/context.rs

+27
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,33 @@ impl<'tcx> TyCtxt<'tcx> {
12851285
)
12861286
}
12871287

1288+
/// Find the nearest parent module of a [`DefId`].
1289+
/// This is different from [`TyCtxt::parent()`] because this returns
1290+
/// the nearest parent `mod`, whereas [`TyCtxt::parent()`] returns
1291+
/// the nearest module-like item (e.g., the enum that a variant belongs to).
1292+
///
1293+
/// Returns `def_id` if it's the crate root.
1294+
///
1295+
/// Note for users in rustdoc: **panics if the item it belongs to is fake**
1296+
/// (see `rustdoc::clean::types::Item::is_fake()`).
1297+
pub fn find_nearest_parent_module(self, def_id: DefId) -> Option<DefId> {
1298+
if def_id.is_top_level_module() {
1299+
// The crate root has no parent. Use it as the root instead.
1300+
Some(def_id)
1301+
} else {
1302+
let mut current = def_id;
1303+
// The immediate parent might not always be a module.
1304+
// Find the first parent which is.
1305+
while let Some(parent) = self.parent(current) {
1306+
if self.def_kind(parent) == DefKind::Mod {
1307+
return Some(parent);
1308+
}
1309+
current = parent;
1310+
}
1311+
None
1312+
}
1313+
}
1314+
12881315
pub fn metadata_encoding_version(self) -> Vec<u8> {
12891316
self.cstore.metadata_encoding_version().to_vec()
12901317
}

src/librustdoc/clean/utils.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_hir::def::{DefKind, Res};
1414
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1515
use rustc_middle::mir::interpret::ConstValue;
1616
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
17-
use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt};
17+
use rustc_middle::ty::{self, DefIdTree, Ty};
1818
use rustc_span::symbol::{kw, sym, Symbol};
1919
use std::mem;
2020

@@ -623,24 +623,3 @@ where
623623
*cx.impl_trait_bounds.borrow_mut() = old_bounds;
624624
r
625625
}
626-
627-
/// Find the nearest parent module of a [`DefId`].
628-
///
629-
/// **Panics if the item it belongs to [is fake][Item::is_fake].**
630-
crate fn find_nearest_parent_module(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
631-
if def_id.is_top_level_module() {
632-
// The crate root has no parent. Use it as the root instead.
633-
Some(def_id)
634-
} else {
635-
let mut current = def_id;
636-
// The immediate parent might not always be a module.
637-
// Find the first parent which is.
638-
while let Some(parent) = tcx.parent(current) {
639-
if tcx.def_kind(parent) == DefKind::Mod {
640-
return Some(parent);
641-
}
642-
current = parent;
643-
}
644-
None
645-
}
646-
}

src/librustdoc/html/format.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::ty::TyCtxt;
1515
use rustc_span::def_id::{DefId, CRATE_DEF_INDEX};
1616
use rustc_target::spec::abi::Abi;
1717

18-
use crate::clean::{self, utils::find_nearest_parent_module, PrimitiveType};
18+
use crate::clean::{self, PrimitiveType};
1919
use crate::formats::cache::cache;
2020
use crate::formats::item_type::ItemType;
2121
use crate::html::escape::Escape;
@@ -1100,7 +1100,7 @@ impl clean::Visibility {
11001100
// FIXME(camelid): This may not work correctly if `item_did` is a module.
11011101
// However, rustdoc currently never displays a module's
11021102
// visibility, so it shouldn't matter.
1103-
let parent_module = find_nearest_parent_module(tcx, item_did);
1103+
let parent_module = tcx.find_nearest_parent_module(item_did);
11041104

11051105
if vis_did.index == CRATE_DEF_INDEX {
11061106
write!(f, "pub(crate) ")
@@ -1109,7 +1109,7 @@ impl clean::Visibility {
11091109
// is the same as no visibility modifier
11101110
Ok(())
11111111
} else if parent_module
1112-
.map(|parent| find_nearest_parent_module(tcx, parent))
1112+
.map(|parent| tcx.find_nearest_parent_module(parent))
11131113
.flatten()
11141114
== Some(vis_did)
11151115
{

src/librustdoc/passes/collect_intra_doc_links.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use std::convert::{TryFrom, TryInto};
3333
use std::mem;
3434
use std::ops::Range;
3535

36-
use crate::clean::{self, utils::find_nearest_parent_module, Crate, Item, ItemLink, PrimitiveType};
36+
use crate::clean::{self, Crate, Item, ItemLink, PrimitiveType};
3737
use crate::core::DocContext;
3838
use crate::fold::DocFolder;
3939
use crate::html::markdown::{markdown_links, MarkdownLink};
@@ -832,11 +832,8 @@ impl<'a, 'tcx> DocFolder for LinkCollector<'a, 'tcx> {
832832
fn fold_item(&mut self, mut item: Item) -> Option<Item> {
833833
use rustc_middle::ty::DefIdTree;
834834

835-
let parent_node = if item.is_fake() {
836-
None
837-
} else {
838-
find_nearest_parent_module(self.cx.tcx, item.def_id)
839-
};
835+
let parent_node =
836+
if item.is_fake() { None } else { self.cx.tcx.find_nearest_parent_module(item.def_id) };
840837

841838
if parent_node.is_some() {
842839
trace!("got parent node for {:?} {:?}, id {:?}", item.type_(), item.name, item.def_id);

0 commit comments

Comments
 (0)