Skip to content

Commit ae47810

Browse files
committedMar 19, 2023
rustdoc: Factor out some doc link resolution code into a separate function
1 parent f7a9702 commit ae47810

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
@@ -773,30 +773,7 @@ fn is_derive_trait_collision<T>(ns: &PerNS<Result<(Res, T), ResolutionFailure<'_
773773

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

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

0 commit comments

Comments
 (0)