From 34b358d52aa6842e512dacbf97c7d3a9e18795e6 Mon Sep 17 00:00:00 2001 From: binarycat Date: Tue, 5 Aug 2025 15:23:14 -0500 Subject: [PATCH] rustdoc: fix caching of intra-doc links on reexports --- src/librustdoc/clean/types.rs | 20 ++++++++++- .../passes/collect_intra_doc_links.rs | 7 +++- .../rustdoc/intra-doc/macro-caching-144965.rs | 35 +++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 tests/rustdoc/intra-doc/macro-caching-144965.rs diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 782311e593bb4..26b087feb1684 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -480,10 +480,28 @@ impl Item { } } + /// If the item has doc comments from a reexport, returns the item id of that reexport, + /// otherwise returns returns the item id. + /// + /// This is used as a key for caching intra-doc link resolution, + /// to prevent two reexports of the same item from using the same cache. + pub(crate) fn item_or_reexport_id(&self) -> ItemId { + // added documentation on a reexport is always prepended. + self.attrs + .doc_strings + .first() + .map(|x| x.item_id) + .flatten() + .map(ItemId::from) + .unwrap_or(self.item_id) + } + pub(crate) fn links(&self, cx: &Context<'_>) -> Vec { use crate::html::format::{href, link_tooltip}; - let Some(links) = cx.cache().intra_doc_links.get(&self.item_id) else { return vec![] }; + let Some(links) = cx.cache().intra_doc_links.get(&self.item_or_reexport_id()) else { + return vec![]; + }; links .iter() .filter_map(|ItemLink { link: s, link_text, page_id: id, fragment }| { diff --git a/src/librustdoc/passes/collect_intra_doc_links.rs b/src/librustdoc/passes/collect_intra_doc_links.rs index c9fa3a4837f29..bcb676cd1f17e 100644 --- a/src/librustdoc/passes/collect_intra_doc_links.rs +++ b/src/librustdoc/passes/collect_intra_doc_links.rs @@ -1082,7 +1082,12 @@ impl LinkCollector<'_, '_> { for md_link in preprocessed_markdown_links(&doc) { let link = self.resolve_link(&doc, item, item_id, module_id, &md_link); if let Some(link) = link { - self.cx.cache.intra_doc_links.entry(item.item_id).or_default().insert(link); + self.cx + .cache + .intra_doc_links + .entry(item.item_or_reexport_id()) + .or_default() + .insert(link); } } } diff --git a/tests/rustdoc/intra-doc/macro-caching-144965.rs b/tests/rustdoc/intra-doc/macro-caching-144965.rs new file mode 100644 index 0000000000000..e14b465aeea6d --- /dev/null +++ b/tests/rustdoc/intra-doc/macro-caching-144965.rs @@ -0,0 +1,35 @@ +// regression test for https://github.com/rust-lang/rust/issues/144965 + +#![crate_name = "foo"] +#![no_std] + +#[doc(hidden)] +pub struct MyStruct; + +macro_rules! my_macro { + () => { + pub fn my_function() {} + + /// Incorrect: [`my_function()`]. + #[doc(inline)] + pub use $crate::MyStruct; + + /// Correct: [`my_function`]. + pub struct AnotherStruct; + }; +} + + +pub mod one { + //@ has 'foo/one/index.html' + //@ has - '//dl[@class="item-table"]/dd[1]/a[@href="fn.my_function.html"]/code' 'my_function' + //@ has - '//dl[@class="item-table"]/dd[2]/a[@href="fn.my_function.html"]/code' 'my_function()' + my_macro!(); +} + +pub mod two { + //@ has 'foo/two/index.html' + //@ has - '//dl[@class="item-table"]/dd[1]/a[@href="fn.my_function.html"]/code' 'my_function' + //@ has - '//dl[@class="item-table"]/dd[2]/a[@href="fn.my_function.html"]/code' 'my_function()' + my_macro!(); +}