Skip to content

Commit 147a678

Browse files
committed
Rustdoc: include crate name in links for local primitives
It makes the link easier to use in cases in which the path of the page where it will be embedded is not known beforehand such as when we generate impls dynamically from `register_type_impls` method in `main.js` Earlier for local primitives we would generate a path that was relative to the current page depth passed in `cx.current` . e.g if the current page was `std::simd::prelude::Simd` the generated path would be `../../primitive.<prim>.html` After this change the path will first take you to the the wesite root and add the crate name. e.g. for `std::simd::prelude::Simd` the path now will be `../../../std/primitive.<prim>.html`
1 parent a28d221 commit 147a678

File tree

1 file changed

+38
-41
lines changed

1 file changed

+38
-41
lines changed

Diff for: src/librustdoc/html/format.rs

+38-41
Original file line numberDiff line numberDiff line change
@@ -875,49 +875,46 @@ fn primitive_link_fragment(
875875
) -> fmt::Result {
876876
let m = &cx.cache();
877877
let mut needs_termination = false;
878-
if !f.alternate() {
879-
match m.primitive_locations.get(&prim) {
880-
Some(&def_id) if def_id.is_local() => {
881-
let len = cx.current.len();
882-
let len = if len == 0 { 0 } else { len - 1 };
883-
write!(
884-
f,
885-
"<a class=\"primitive\" href=\"{}primitive.{}.html{fragment}\">",
886-
"../".repeat(len),
887-
prim.as_sym()
888-
)?;
889-
needs_termination = true;
890-
}
891-
Some(&def_id) => {
892-
let loc = match m.extern_locations[&def_id.krate] {
893-
ExternalLocation::Remote(ref s) => {
894-
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
895-
let builder: UrlPartsBuilder =
896-
[s.as_str().trim_end_matches('/'), cname_sym.as_str()]
897-
.into_iter()
898-
.collect();
899-
Some(builder)
900-
}
901-
ExternalLocation::Local => {
902-
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
903-
Some(if cx.current.first() == Some(&cname_sym) {
904-
iter::repeat(sym::dotdot).take(cx.current.len() - 1).collect()
905-
} else {
906-
iter::repeat(sym::dotdot)
907-
.take(cx.current.len())
908-
.chain(iter::once(cname_sym))
909-
.collect()
910-
})
911-
}
912-
ExternalLocation::Unknown => None,
913-
};
914-
if let Some(mut loc) = loc {
915-
loc.push_fmt(format_args!("primitive.{}.html", prim.as_sym()));
916-
write!(f, "<a class=\"primitive\" href=\"{}{fragment}\">", loc.finish())?;
917-
needs_termination = true;
878+
879+
if !f.alternate()
880+
&& let Some(&def_id) = m.primitive_locations.get(&prim)
881+
{
882+
let cname_sym = ExternalCrate { crate_num: def_id.krate }.name(cx.tcx());
883+
if def_id.is_local() {
884+
let len = cx.current.len();
885+
write!(
886+
f,
887+
"<a class=\"primitive\" href=\"{}{}/primitive.{}.html{fragment}\">",
888+
"../".repeat(len),
889+
cname_sym,
890+
prim.as_sym()
891+
)?;
892+
needs_termination = true;
893+
} else {
894+
let loc = match m.extern_locations[&def_id.krate] {
895+
ExternalLocation::Remote(ref s) => {
896+
let builder: UrlPartsBuilder =
897+
[s.as_str().trim_end_matches('/'), cname_sym.as_str()]
898+
.into_iter()
899+
.collect();
900+
Some(builder)
918901
}
902+
ExternalLocation::Local => Some(if cx.current.first() == Some(&cname_sym) {
903+
iter::repeat(sym::dotdot).take(cx.current.len() - 1).collect()
904+
} else {
905+
iter::repeat(sym::dotdot)
906+
.take(cx.current.len())
907+
.chain(iter::once(cname_sym))
908+
.collect()
909+
}),
910+
ExternalLocation::Unknown => None,
911+
};
912+
913+
if let Some(mut loc) = loc {
914+
loc.push_fmt(format_args!("primitive.{}.html", prim.as_sym()));
915+
write!(f, "<a class=\"primitive\" href=\"{}{fragment}\">", loc.finish())?;
916+
needs_termination = true;
919917
}
920-
None => {}
921918
}
922919
}
923920
Display::fmt(&name, f)?;

0 commit comments

Comments
 (0)