diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 4843c20c758e6..b7f6d84ea36c2 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -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#"{text}"#, + r#"{text}"#, path = join_path_syms(rust_path), text = EscapeBodyText(text.as_str()), ) diff --git a/tests/rustdoc/import_trait_associated_functions.rs b/tests/rustdoc/import_trait_associated_functions.rs new file mode 100644 index 0000000000000..84f6f8009701d --- /dev/null +++ b/tests/rustdoc/import_trait_associated_functions.rs @@ -0,0 +1,19 @@ +// This test ensures that reexports of associated items links to the associated items. +// Regression test for . + +#![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};