Skip to content

Commit 1bd8183

Browse files
committed
Don't hardcode item-type anchor ids
These should always correspond to the values in `ItemType::to_static_str`
1 parent 3e33ef4 commit 1bd8183

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/librustdoc/html/render.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,8 +2160,9 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
21602160
write!(w, "<h2 class='fields'>Fields</h2>\n<table>")?;
21612161
for field in fields {
21622162
write!(w, "<tr class='stab {stab}'>
2163-
<td id='structfield.{name}'>\
2163+
<td id='{shortty}.{name}'>\
21642164
<code>{name}</code></td><td>",
2165+
shortty = ItemType::StructField,
21652166
stab = field.stability_class(),
21662167
name = field.name.as_ref().unwrap())?;
21672168
document(w, cx, field)?;
@@ -2231,7 +2232,8 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
22312232
if !e.variants.is_empty() {
22322233
write!(w, "<h2 class='variants'>Variants</h2>\n<table class='variants_table'>")?;
22332234
for variant in &e.variants {
2234-
write!(w, "<tr><td id='variant.{name}'><code>{name}</code></td><td>",
2235+
write!(w, "<tr><td id='{shortty}.{name}'><code>{name}</code></td><td>",
2236+
shortty = ItemType::Variant,
22352237
name = variant.name.as_ref().unwrap())?;
22362238
document(w, cx, variant)?;
22372239

@@ -2247,8 +2249,9 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
22472249
<table>")?;
22482250
for field in fields {
22492251
write!(w, "<tr><td \
2250-
id='variant.{v}.field.{f}'>\
2252+
id='{shortty}.{v}.field.{f}'>\
22512253
<code>{f}</code></td><td>",
2254+
shortty = ItemType::Variant,
22522255
v = variant.name.as_ref().unwrap(),
22532256
f = field.name.as_ref().unwrap())?;
22542257
document(w, cx, field)?;
@@ -2460,6 +2463,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
24602463
fn doctraititem(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item,
24612464
link: AssocItemLink, render_static: bool,
24622465
outer_version: Option<&str>) -> fmt::Result {
2466+
let shortty = shortty(item);
24632467
let name = item.name.as_ref().unwrap();
24642468

24652469
let is_static = match item.inner {
@@ -2472,35 +2476,35 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
24722476
clean::MethodItem(..) | clean::TyMethodItem(..) => {
24732477
// Only render when the method is not static or we allow static methods
24742478
if !is_static || render_static {
2475-
let id = derive_id(format!("method.{}", name));
2476-
write!(w, "<h4 id='{}' class='{}'>", id, shortty(item))?;
2479+
let id = derive_id(format!("{}.{}", shortty, name));
2480+
write!(w, "<h4 id='{}' class='{}'>", id, shortty)?;
24772481
render_stability_since_raw(w, item.stable_since(), outer_version)?;
24782482
write!(w, "<code>")?;
24792483
render_assoc_item(w, item, link)?;
24802484
write!(w, "</code></h4>\n")?;
24812485
}
24822486
}
24832487
clean::TypedefItem(ref tydef, _) => {
2484-
let id = derive_id(format!("associatedtype.{}", name));
2485-
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))?;
2488+
let id = derive_id(format!("{}.{}", ItemType::AssociatedType, name));
2489+
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
24862490
write!(w, "type {} = {}", name, tydef.type_)?;
24872491
write!(w, "</code></h4>\n")?;
24882492
}
24892493
clean::AssociatedConstItem(ref ty, ref default) => {
2490-
let id = derive_id(format!("associatedconstant.{}", name));
2491-
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))?;
2494+
let id = derive_id(format!("{}.{}", shortty, name));
2495+
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
24922496
assoc_const(w, item, ty, default.as_ref())?;
24932497
write!(w, "</code></h4>\n")?;
24942498
}
24952499
clean::ConstantItem(ref c) => {
2496-
let id = derive_id(format!("associatedconstant.{}", name));
2497-
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))?;
2500+
let id = derive_id(format!("{}.{}", shortty, name));
2501+
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
24982502
assoc_const(w, item, &c.type_, Some(&c.expr))?;
24992503
write!(w, "</code></h4>\n")?;
25002504
}
25012505
clean::AssociatedTypeItem(ref bounds, ref default) => {
2502-
let id = derive_id(format!("associatedtype.{}", name));
2503-
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))?;
2506+
let id = derive_id(format!("{}.{}", shortty, name));
2507+
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
25042508
assoc_type(w, item, bounds, default)?;
25052509
write!(w, "</code></h4>\n")?;
25062510
}

0 commit comments

Comments
 (0)