Skip to content

Make rustdoc method HTML anchor IDs/links unique (#20700) #21967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 42 additions & 25 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1704,23 +1704,23 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
try!(write!(w, "{{\n"));
for t in types.iter() {
try!(write!(w, " "));
try!(render_method(w, t.item()));
try!(render_method(w, t.item(), cx.current.connect(",").as_slice()));
try!(write!(w, ";\n"));
}
if types.len() > 0 && required.len() > 0 {
try!(w.write_str("\n"));
}
for m in required.iter() {
try!(write!(w, " "));
try!(render_method(w, m.item()));
try!(render_method(w, m.item(), cx.current.connect(",").as_slice()));
try!(write!(w, ";\n"));
}
if required.len() > 0 && provided.len() > 0 {
try!(w.write_str("\n"));
}
for m in provided.iter() {
try!(write!(w, " "));
try!(render_method(w, m.item()));
try!(render_method(w, m.item(), cx.current.connect(",").as_slice()));
try!(write!(w, " {{ ... }}\n"));
}
try!(write!(w, "}}"));
Expand All @@ -1732,11 +1732,16 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,

fn trait_item(w: &mut fmt::Formatter, m: &clean::TraitMethod)
-> fmt::Result {
let ctx_s: &str = match m.item().name {
Some(ref name) => name.as_slice(),
None => ""
};

try!(write!(w, "<h3 id='{}.{}' class='method'>{}<code>",
shortty(m.item()),
*m.item().name.as_ref().unwrap(),
ConciseStability(&m.item().stability)));
try!(render_method(w, m.item()));
try!(render_method(w, m.item(), ctx_s));
try!(write!(w, "</code></h3>"));
try!(document(w, m.item()));
Ok(())
Expand Down Expand Up @@ -1806,7 +1811,8 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
Ok(())
}

fn assoc_type(w: &mut fmt::Formatter, it: &clean::Item,
fn assoc_type(w: &mut fmt::Formatter,
it: &clean::Item,
typ: &clean::TyParam) -> fmt::Result {
try!(write!(w, "type {}", it.name.as_ref().unwrap()));
if typ.bounds.len() > 0 {
Expand All @@ -1818,16 +1824,17 @@ fn assoc_type(w: &mut fmt::Formatter, it: &clean::Item,
Ok(())
}

fn render_method(w: &mut fmt::Formatter, meth: &clean::Item) -> fmt::Result {
fn method(w: &mut fmt::Formatter, it: &clean::Item, unsafety: ast::Unsafety,
g: &clean::Generics, selfty: &clean::SelfTy,
fn render_method(w: &mut fmt::Formatter, meth: &clean::Item, ctx_s: &str) -> fmt::Result {
fn method(w: &mut fmt::Formatter, it: &clean::Item, ctx_s: &str,
unsafety: ast::Unsafety, g: &clean::Generics, selfty: &clean::SelfTy,
d: &clean::FnDecl) -> fmt::Result {
write!(w, "{}fn <a href='#{ty}.{name}' class='fnname'>{name}</a>\
write!(w, "{}fn <a href='#{ctx}:{ty}.{name}' class='fnname'>{name}</a>\
{generics}{decl}{where_clause}",
match unsafety {
ast::Unsafety::Unsafe => "unsafe ",
_ => "",
},
ctx = ctx_s,
ty = shortty(it),
name = it.name.as_ref().unwrap().as_slice(),
generics = *g,
Expand All @@ -1836,10 +1843,10 @@ fn render_method(w: &mut fmt::Formatter, meth: &clean::Item) -> fmt::Result {
}
match meth.inner {
clean::TyMethodItem(ref m) => {
method(w, meth, m.unsafety, &m.generics, &m.self_, &m.decl)
method(w, meth, ctx_s, m.unsafety, &m.generics, &m.self_, &m.decl)
}
clean::MethodItem(ref m) => {
method(w, meth, m.unsafety, &m.generics, &m.self_, &m.decl)
method(w, meth, ctx_s, m.unsafety, &m.generics, &m.self_, &m.decl)
}
clean::AssociatedTypeItem(ref typ) => {
assoc_type(w, meth, typ)
Expand Down Expand Up @@ -2085,10 +2092,13 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {
try!(write!(w, "<h3 class='impl'>{}<code>impl{} ",
ConciseStability(&i.stability),
i.impl_.generics));
match i.impl_.trait_ {
Some(ref ty) => try!(write!(w, "{} for ", *ty)),
None => {}
}
let method_context = format!("{}:{}",
i.impl_.for_,
match i.impl_.trait_ {
Some(ref ty) => {try!(write!(w, "{} for ", *ty));
format!("{}", *ty)},
None => String::from_str("")
});
try!(write!(w, "{}{}</code></h3>", i.impl_.for_, WhereClause(&i.impl_.generics)));
match i.dox {
Some(ref dox) => {
Expand All @@ -2098,15 +2108,18 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {
None => {}
}

fn doctraititem(w: &mut fmt::Formatter, item: &clean::Item, dox: bool)
-> fmt::Result {
fn doctraititem(w: &mut fmt::Formatter,
item: &clean::Item,
dox: bool,
method_context: &str) -> fmt::Result {
match item.inner {
clean::MethodItem(..) | clean::TyMethodItem(..) => {
try!(write!(w, "<h4 id='method.{}' class='{}'>{}<code>",
try!(write!(w, "<h4 id='{}:method.{}' class='{}'>{}<code>",
method_context,
*item.name.as_ref().unwrap(),
shortty(item),
ConciseStability(&item.stability)));
try!(render_method(w, item));
try!(render_method(w, item, method_context));
try!(write!(w, "</code></h4>\n"));
}
clean::TypedefItem(ref tydef) => {
Expand Down Expand Up @@ -2140,20 +2153,21 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {

try!(write!(w, "<div class='impl-items'>"));
for trait_item in i.impl_.items.iter() {
try!(doctraititem(w, trait_item, true));
try!(doctraititem(w, trait_item, true, method_context.as_slice()));
}

fn render_default_methods(w: &mut fmt::Formatter,
t: &clean::Trait,
i: &clean::Impl) -> fmt::Result {
fn render_default_methods(w: &mut fmt::Formatter,
t: &clean::Trait,
i: &clean::Impl,
method_context: &str) -> fmt::Result {
for trait_item in t.items.iter() {
let n = trait_item.item().name.clone();
match i.items.iter().find(|m| { m.name == n }) {
Some(..) => continue,
None => {}
}

try!(doctraititem(w, trait_item.item(), false));
try!(doctraititem(w, trait_item.item(), false, method_context.as_slice()));
}
Ok(())
}
Expand All @@ -2166,7 +2180,10 @@ fn render_impl(w: &mut fmt::Formatter, i: &Impl) -> fmt::Result {
Some(clean::ResolvedPath { did, .. }) => {
try!({
match cache().traits.get(&did) {
Some(t) => try!(render_default_methods(w, t, &i.impl_)),
Some(t) => try!(render_default_methods(w,
t,
&i.impl_,
method_context.as_slice())),
None => {}
}
Ok(())
Expand Down