Rollup merge of #148543 - GuillaumeGomez:fix-import_trait_associated_functions, r=lolbinarycat

Correctly link to associated trait items in reexports

Fixes rust-lang/rust#148008.

Issue was that we didn't add anchors in this case.

r? ``````@lolbinarycat``````
This commit is contained in:
Stuart Cook 2025-11-14 13:13:59 +11:00 committed by GitHub
commit 89902f436f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 34 additions and 1 deletions

View file

@ -838,9 +838,23 @@ fn print_higher_ranked_params_with_space(
pub(crate) fn print_anchor(did: DefId, text: Symbol, cx: &Context<'_>) -> impl Display {
fmt::from_fn(move |f| {
if let Ok(HrefInfo { url, kind, rust_path }) = href(did, cx) {
let tcx = cx.tcx();
let def_kind = tcx.def_kind(did);
let anchor = if matches!(
def_kind,
DefKind::AssocTy | DefKind::AssocFn | DefKind::AssocConst | DefKind::Variant
) {
let parent_def_id = tcx.parent(did);
let item_type =
ItemType::from_def_kind(def_kind, Some(tcx.def_kind(parent_def_id)));
format!("#{}.{}", item_type.as_str(), tcx.item_name(did))
} else {
String::new()
};
write!(
f,
r#"<a class="{kind}" href="{url}" title="{kind} {path}">{text}</a>"#,
r#"<a class="{kind}" href="{url}{anchor}" title="{kind} {path}">{text}</a>"#,
path = join_path_syms(rust_path),
text = EscapeBodyText(text.as_str()),
)

View file

@ -0,0 +1,19 @@
// This test ensures that reexports of associated items links to the associated items.
// Regression test for <https://github.com/rust-lang/rust/issues/148008>.
#![feature(import_trait_associated_functions)]
#![crate_name = "foo"]
//@ has 'foo/index.html'
pub trait Test {
fn method();
const CONST: u8;
type Type;
}
//@ has - '//*[@id="reexport.method"]//a[@href="trait.Test.html#tymethod.method"]' 'method'
//@ has - '//*[@id="reexport.CONST"]//a[@href="trait.Test.html#associatedconstant.CONST"]' 'CONST'
//@ has - '//*[@id="reexport.Type"]//a[@href="trait.Test.html#associatedtype.Type"]' 'Type'
pub use self::Test::{method, CONST, Type};