Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }| {
Expand Down
7 changes: 6 additions & 1 deletion src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/rustdoc/intra-doc/macro-caching-144965.rs
Original file line number Diff line number Diff line change
@@ -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!();
}
Loading