Skip to content

Commit e2955bf

Browse files
authored
Rollup merge of #60307 - dima74:fix/56018, r=GuillaumeGomez
Make "Implementations on Foreign Types" items in sidebar link to specific impls This solves #56018 for most cases (though not work for foreign impls with same names)
2 parents 04cf752 + d2b4f4d commit e2955bf

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/librustdoc/html/render.rs

+19-11
Original file line numberDiff line numberDiff line change
@@ -3060,7 +3060,7 @@ fn render_implementor(cx: &Context, implementor: &Impl, w: &mut fmt::Formatter<'
30603060
_ => false,
30613061
};
30623062
render_impl(w, cx, implementor, AssocItemLink::Anchor(None), RenderMode::Normal,
3063-
implementor.impl_item.stable_since(), false, Some(use_absolute))?;
3063+
implementor.impl_item.stable_since(), false, Some(use_absolute), false)?;
30643064
Ok(())
30653065
}
30663066

@@ -3071,7 +3071,7 @@ fn render_impls(cx: &Context, w: &mut fmt::Formatter<'_>,
30713071
let did = i.trait_did().unwrap();
30723072
let assoc_link = AssocItemLink::GotoSource(did, &i.inner_impl().provided_trait_methods);
30733073
render_impl(w, cx, i, assoc_link,
3074-
RenderMode::Normal, containing_item.stable_since(), true, None)?;
3074+
RenderMode::Normal, containing_item.stable_since(), true, None, false)?;
30753075
}
30763076
Ok(())
30773077
}
@@ -3301,7 +3301,7 @@ fn item_trait(
33013301
);
33023302
render_impl(w, cx, &implementor, assoc_link,
33033303
RenderMode::Normal, implementor.impl_item.stable_since(), false,
3304-
None)?;
3304+
None, true)?;
33053305
}
33063306
write_loading_content(w, "")?;
33073307
}
@@ -3958,7 +3958,7 @@ fn render_assoc_items(w: &mut fmt::Formatter<'_>,
39583958
};
39593959
for i in &non_trait {
39603960
render_impl(w, cx, i, AssocItemLink::Anchor(None), render_mode,
3961-
containing_item.stable_since(), true, None)?;
3961+
containing_item.stable_since(), true, None, false)?;
39623962
}
39633963
}
39643964
if let AssocItemRender::DerefFor { .. } = what {
@@ -4138,11 +4138,15 @@ fn spotlight_decl(decl: &clean::FnDecl) -> Result<String, fmt::Error> {
41384138
}
41394139

41404140
fn render_impl(w: &mut fmt::Formatter<'_>, cx: &Context, i: &Impl, link: AssocItemLink<'_>,
4141-
render_mode: RenderMode, outer_version: Option<&str>,
4142-
show_def_docs: bool, use_absolute: Option<bool>) -> fmt::Result {
4141+
render_mode: RenderMode, outer_version: Option<&str>, show_def_docs: bool,
4142+
use_absolute: Option<bool>, is_on_foreign_type: bool) -> fmt::Result {
41434143
if render_mode == RenderMode::Normal {
41444144
let id = cx.derive_id(match i.inner_impl().trait_ {
4145-
Some(ref t) => format!("impl-{}", small_url_encode(&format!("{:#}", t))),
4145+
Some(ref t) => if is_on_foreign_type {
4146+
get_id_for_impl_on_foreign_type(&i.inner_impl().for_, t)
4147+
} else {
4148+
format!("impl-{}", small_url_encode(&format!("{:#}", t)))
4149+
},
41464150
None => "impl".to_string(),
41474151
});
41484152
if let Some(use_absolute) = use_absolute {
@@ -4688,11 +4692,15 @@ fn sidebar_struct(fmt: &mut fmt::Formatter<'_>, it: &clean::Item,
46884692
Ok(())
46894693
}
46904694

4695+
fn get_id_for_impl_on_foreign_type(for_: &clean::Type, trait_: &clean::Type) -> String {
4696+
small_url_encode(&format!("impl-{:#}-for-{:#}", trait_, for_))
4697+
}
4698+
46914699
fn extract_for_impl_name(item: &clean::Item) -> Option<(String, String)> {
46924700
match item.inner {
46934701
clean::ItemEnum::ImplItem(ref i) => {
46944702
if let Some(ref trait_) = i.trait_ {
4695-
Some((format!("{:#}", i.for_), format!("{:#}", trait_)))
4703+
Some((format!("{:#}", i.for_), get_id_for_impl_on_foreign_type(&i.for_, trait_)))
46964704
} else {
46974705
None
46984706
}
@@ -4788,9 +4796,9 @@ fn sidebar_trait(fmt: &mut fmt::Formatter<'_>, it: &clean::Item,
47884796
.map_or(false, |d| !c.paths.contains_key(&d)))
47894797
.filter_map(|i| {
47904798
match extract_for_impl_name(&i.impl_item) {
4791-
Some((ref name, ref url)) => {
4792-
Some(format!("<a href=\"#impl-{}\">{}</a>",
4793-
small_url_encode(url),
4799+
Some((ref name, ref id)) => {
4800+
Some(format!("<a href=\"#{}\">{}</a>",
4801+
id,
47944802
Escape(name)))
47954803
}
47964804
_ => None,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// issue #56018: "Implementations on Foreign Types" sidebar items should link to specific impls
2+
3+
#![crate_name = "foo"]
4+
5+
// @has foo/trait.Foo.html
6+
// @has - '//*[@class="sidebar-title"][@href="#foreign-impls"]' 'Implementations on Foreign Types'
7+
// @has - '//h2[@id="foreign-impls"]' 'Implementations on Foreign Types'
8+
// @has - '//*[@class="sidebar-links"]/a[@href="#impl-Foo-for-u32"]' 'u32'
9+
// @has - '//h3[@id="impl-Foo-for-u32"]//code' 'impl Foo for u32'
10+
// @has - '//*[@class="sidebar-links"]/a[@href="#impl-Foo-for-%26%27a%20str"]' "&'a str"
11+
// @has - '//h3[@id="impl-Foo-for-%26%27a%20str"]//code' "impl<'a> Foo for &'a str"
12+
pub trait Foo {}
13+
14+
impl Foo for u32 {}
15+
16+
impl<'a> Foo for &'a str {}

0 commit comments

Comments
 (0)