rustdoc: fix caching of intra-doc links on reexports

This commit is contained in:
binarycat 2025-08-05 15:23:14 -05:00
parent 213d946a38
commit 34b358d52a
3 changed files with 60 additions and 2 deletions

View file

@ -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<RenderedLink> {
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 }| {

View file

@ -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);
}
}
}

View file

@ -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!();
}