Skip to content
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

rustdoc: Disambiguate anchors #32985

Merged
merged 1 commit into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
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
40 changes: 26 additions & 14 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1903,34 +1903,35 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
if t.items.is_empty() {
write!(w, "{{ }}")?;
} else {
// FIXME: we should be using a derived_id for the Anchors here
write!(w, "{{\n")?;
for t in &types {
write!(w, " ")?;
render_assoc_item(w, t, AssocItemLink::Anchor)?;
render_assoc_item(w, t, AssocItemLink::Anchor(None))?;
write!(w, ";\n")?;
}
if !types.is_empty() && !consts.is_empty() {
w.write_str("\n")?;
}
for t in &consts {
write!(w, " ")?;
render_assoc_item(w, t, AssocItemLink::Anchor)?;
render_assoc_item(w, t, AssocItemLink::Anchor(None))?;
write!(w, ";\n")?;
}
if !consts.is_empty() && !required.is_empty() {
w.write_str("\n")?;
}
for m in &required {
write!(w, " ")?;
render_assoc_item(w, m, AssocItemLink::Anchor)?;
render_assoc_item(w, m, AssocItemLink::Anchor(None))?;
write!(w, ";\n")?;
}
if !required.is_empty() && !provided.is_empty() {
w.write_str("\n")?;
}
for m in &provided {
write!(w, " ")?;
render_assoc_item(w, m, AssocItemLink::Anchor)?;
render_assoc_item(w, m, AssocItemLink::Anchor(None))?;
write!(w, " {{ ... }}\n")?;
}
write!(w, "}}")?;
Expand All @@ -1947,7 +1948,7 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
write!(w, "<h3 id='{id}' class='method stab {stab}'><code>",
id = id,
stab = m.stability_class())?;
render_assoc_item(w, m, AssocItemLink::Anchor)?;
render_assoc_item(w, m, AssocItemLink::Anchor(Some(&id)))?;
write!(w, "</code>")?;
render_stability_since(w, m, t)?;
write!(w, "</h3>")?;
Expand Down Expand Up @@ -2042,7 +2043,8 @@ fn naive_assoc_href(it: &clean::Item, link: AssocItemLink) -> String {

let anchor = format!("#{}.{}", ty, name);
match link {
AssocItemLink::Anchor => anchor,
AssocItemLink::Anchor(Some(ref id)) => format!("#{}", id),
AssocItemLink::Anchor(None) => anchor,
AssocItemLink::GotoSource(did, _) => {
href(did).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor)
}
Expand Down Expand Up @@ -2117,7 +2119,8 @@ fn render_assoc_item(w: &mut fmt::Formatter,
let name = meth.name.as_ref().unwrap();
let anchor = format!("#{}.{}", shortty(meth), name);
let href = match link {
AssocItemLink::Anchor => anchor,
AssocItemLink::Anchor(Some(ref id)) => format!("#{}", id),
AssocItemLink::Anchor(None) => anchor,
AssocItemLink::GotoSource(did, provided_methods) => {
// We're creating a link from an impl-item to the corresponding
// trait-item and need to map the anchored type accordingly.
Expand Down Expand Up @@ -2378,10 +2381,19 @@ fn render_struct(w: &mut fmt::Formatter, it: &clean::Item,

#[derive(Copy, Clone)]
enum AssocItemLink<'a> {
Anchor,
Anchor(Option<&'a str>),
GotoSource(DefId, &'a HashSet<String>),
}

impl<'a> AssocItemLink<'a> {
fn anchor(&self, id: &'a String) -> Self {
match *self {
AssocItemLink::Anchor(_) => { AssocItemLink::Anchor(Some(&id)) },
ref other => *other,
}
}
}

enum AssocItemRender<'a> {
All,
DerefFor { trait_: &'a clean::Type, type_: &'a clean::Type },
Expand Down Expand Up @@ -2413,7 +2425,7 @@ fn render_assoc_items(w: &mut fmt::Formatter,
}
};
for i in &non_trait {
render_impl(w, cx, i, AssocItemLink::Anchor, render_header,
render_impl(w, cx, i, AssocItemLink::Anchor(None), render_header,
containing_item.stable_since())?;
}
}
Expand Down Expand Up @@ -2509,32 +2521,32 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
write!(w, "<h4 id='{}' class='{}'>", id, shortty)?;
render_stability_since_raw(w, item.stable_since(), outer_version)?;
write!(w, "<code>")?;
render_assoc_item(w, item, link)?;
render_assoc_item(w, item, link.anchor(&id))?;
write!(w, "</code></h4>\n")?;
}
}
clean::TypedefItem(ref tydef, _) => {
let id = derive_id(format!("{}.{}", ItemType::AssociatedType, name));
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link)?;
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id))?;
write!(w, "</code></h4>\n")?;
}
clean::AssociatedConstItem(ref ty, ref default) => {
let id = derive_id(format!("{}.{}", shortty, name));
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
assoc_const(w, item, ty, default.as_ref(), link)?;
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id))?;
write!(w, "</code></h4>\n")?;
}
clean::ConstantItem(ref c) => {
let id = derive_id(format!("{}.{}", shortty, name));
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
assoc_const(w, item, &c.type_, Some(&c.expr), link)?;
assoc_const(w, item, &c.type_, Some(&c.expr), link.anchor(&id))?;
write!(w, "</code></h4>\n")?;
}
clean::AssociatedTypeItem(ref bounds, ref default) => {
let id = derive_id(format!("{}.{}", shortty, name));
write!(w, "<h4 id='{}' class='{}'><code>", id, shortty)?;
assoc_type(w, item, bounds, default.as_ref(), link)?;
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id))?;
write!(w, "</code></h4>\n")?;
}
clean::StrippedItem(..) => return Ok(()),
Expand Down
27 changes: 27 additions & 0 deletions src/test/rustdoc/issue-32890.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// @has issue_32890/struct.Foo.html
pub struct Foo<T>(T);

impl Foo<u8> {
// @has - '//a[@href="#method.pass"]' 'pass'
pub fn pass() {}
}

impl Foo<u16> {
// @has - '//a[@href="#method.pass-1"]' 'pass'
pub fn pass() {}
}

impl Foo<u32> {
// @has - '//a[@href="#method.pass-2"]' 'pass'
pub fn pass() {}
}