Skip to content

Commit b8efee5

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

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src/librustdoc/html/render.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -2159,8 +2159,9 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
21592159
write!(w, "<h2 class='fields'>Fields</h2>\n<table>")?;
21602160
for field in fields {
21612161
write!(w, "<tr class='stab {stab}'>
2162-
<td id='structfield.{name}'>\
2162+
<td id='{shortty}.{name}'>\
21632163
<code>{name}</code></td><td>",
2164+
shortty = ItemType::StructField,
21642165
stab = field.stability_class(),
21652166
name = field.name.as_ref().unwrap())?;
21662167
document(w, cx, field)?;
@@ -2230,7 +2231,8 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
22302231
if !e.variants.is_empty() {
22312232
write!(w, "<h2 class='variants'>Variants</h2>\n<table class='variants_table'>")?;
22322233
for variant in &e.variants {
2233-
write!(w, "<tr><td id='variant.{name}'><code>{name}</code></td><td>",
2234+
write!(w, "<tr><td id='{shortty}.{name}'><code>{name}</code></td><td>",
2235+
shortty = ItemType::Variant,
22342236
name = variant.name.as_ref().unwrap())?;
22352237
document(w, cx, variant)?;
22362238

@@ -2246,8 +2248,9 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
22462248
<table>")?;
22472249
for field in fields {
22482250
write!(w, "<tr><td \
2249-
id='variant.{v}.field.{f}'>\
2251+
id='{shortty}.{v}.field.{f}'>\
22502252
<code>{f}</code></td><td>",
2253+
shortty = ItemType::Variant,
22512254
v = variant.name.as_ref().unwrap(),
22522255
f = field.name.as_ref().unwrap())?;
22532256
document(w, cx, field)?;
@@ -2465,6 +2468,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
24652468
fn doctraititem(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item,
24662469
link: AssocItemLink, render_static: bool,
24672470
outer_version: Option<&str>) -> fmt::Result {
2471+
let shortty = shortty(item);
24682472
let name = item.name.as_ref().unwrap();
24692473

24702474
let is_static = match item.inner {
@@ -2477,35 +2481,35 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
24772481
clean::MethodItem(..) | clean::TyMethodItem(..) => {
24782482
// Only render when the method is not static or we allow static methods
24792483
if !is_static || render_static {
2480-
let id = derive_id(format!("method.{}", name));
2481-
write!(w, "<h4 id='{}' class='{}'>", id, shortty(item))?;
2484+
let id = derive_id(format!("{}.{}", shortty, name));
2485+
write!(w, "<h4 id='{}' class='{}'>", id, shortty)?;
24822486
render_stability_since_raw(w, item.stable_since(), outer_version)?;
24832487
write!(w, "<code>")?;
24842488
render_assoc_item(w, item, link)?;
24852489
write!(w, "</code></h4>\n")?;
24862490
}
24872491
}
24882492
clean::TypedefItem(ref tydef, _) => {
2489-
let id = derive_id(format!("associatedtype.{}", name));
2490-
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))?;
2493+
let id = derive_id(format!("{}.{}", ItemType::AssociatedType, name));
2494+
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
24912495
write!(w, "type {} = {}", name, tydef.type_)?;
24922496
write!(w, "</code></h4>\n")?;
24932497
}
24942498
clean::AssociatedConstItem(ref ty, ref default) => {
2495-
let id = derive_id(format!("associatedconstant.{}", name));
2496-
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))?;
2499+
let id = derive_id(format!("{}.{}", shortty, name));
2500+
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
24972501
assoc_const(w, item, ty, default.as_ref())?;
24982502
write!(w, "</code></h4>\n")?;
24992503
}
25002504
clean::ConstantItem(ref c) => {
2501-
let id = derive_id(format!("associatedconstant.{}", name));
2502-
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))?;
2505+
let id = derive_id(format!("{}.{}", shortty, name));
2506+
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
25032507
assoc_const(w, item, &c.type_, Some(&c.expr))?;
25042508
write!(w, "</code></h4>\n")?;
25052509
}
25062510
clean::AssociatedTypeItem(ref bounds, ref default) => {
2507-
let id = derive_id(format!("associatedtype.{}", name));
2508-
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty(item))?;
2511+
let id = derive_id(format!("{}.{}", shortty, name));
2512+
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
25092513
assoc_type(w, item, bounds, default)?;
25102514
write!(w, "</code></h4>\n")?;
25112515
}

0 commit comments

Comments
 (0)