Skip to content

Commit 0f45d85

Browse files
committed
rustdoc: Factor out some doc link resolution code into a separate function
1 parent d3a5541 commit 0f45d85

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+27-24
Original file line numberDiff line numberDiff line change
@@ -771,30 +771,7 @@ fn is_derive_trait_collision<T>(ns: &PerNS<Result<(Res, T), ResolutionFailure<'_
771771

772772
impl<'a, 'tcx> DocVisitor for LinkCollector<'a, 'tcx> {
773773
fn visit_item(&mut self, item: &Item) {
774-
// We want to resolve in the lexical scope of the documentation.
775-
// In the presence of re-exports, this is not the same as the module of the item.
776-
// Rather than merging all documentation into one, resolve it one attribute at a time
777-
// so we know which module it came from.
778-
for (item_id, doc) in prepare_to_doc_link_resolution(&item.attrs.doc_strings) {
779-
if !may_have_doc_links(&doc) {
780-
continue;
781-
}
782-
debug!("combined_docs={}", doc);
783-
// NOTE: if there are links that start in one crate and end in another, this will not resolve them.
784-
// This is a degenerate case and it's not supported by rustdoc.
785-
let item_id = item_id.unwrap_or_else(|| item.item_id.expect_def_id());
786-
let module_id = match self.cx.tcx.def_kind(item_id) {
787-
DefKind::Mod if item.inner_docs(self.cx.tcx) => item_id,
788-
_ => find_nearest_parent_module(self.cx.tcx, item_id).unwrap(),
789-
};
790-
for md_link in preprocessed_markdown_links(&doc) {
791-
let link = self.resolve_link(item, item_id, module_id, &doc, &md_link);
792-
if let Some(link) = link {
793-
self.cx.cache.intra_doc_links.entry(item.item_id).or_default().push(link);
794-
}
795-
}
796-
}
797-
774+
self.resolve_links(item);
798775
self.visit_item_recur(item)
799776
}
800777
}
@@ -921,6 +898,32 @@ fn preprocessed_markdown_links(s: &str) -> Vec<PreprocessedMarkdownLink> {
921898
}
922899

923900
impl LinkCollector<'_, '_> {
901+
fn resolve_links(&mut self, item: &Item) {
902+
// We want to resolve in the lexical scope of the documentation.
903+
// In the presence of re-exports, this is not the same as the module of the item.
904+
// Rather than merging all documentation into one, resolve it one attribute at a time
905+
// so we know which module it came from.
906+
for (item_id, doc) in prepare_to_doc_link_resolution(&item.attrs.doc_strings) {
907+
if !may_have_doc_links(&doc) {
908+
continue;
909+
}
910+
debug!("combined_docs={}", doc);
911+
// NOTE: if there are links that start in one crate and end in another, this will not resolve them.
912+
// This is a degenerate case and it's not supported by rustdoc.
913+
let item_id = item_id.unwrap_or_else(|| item.item_id.expect_def_id());
914+
let module_id = match self.cx.tcx.def_kind(item_id) {
915+
DefKind::Mod if item.inner_docs(self.cx.tcx) => item_id,
916+
_ => find_nearest_parent_module(self.cx.tcx, item_id).unwrap(),
917+
};
918+
for md_link in preprocessed_markdown_links(&doc) {
919+
let link = self.resolve_link(item, item_id, module_id, &doc, &md_link);
920+
if let Some(link) = link {
921+
self.cx.cache.intra_doc_links.entry(item.item_id).or_default().push(link);
922+
}
923+
}
924+
}
925+
}
926+
924927
/// This is the entry point for resolving an intra-doc link.
925928
///
926929
/// FIXME(jynelson): this is way too many arguments

0 commit comments

Comments
 (0)