Skip to content

Rustdoc: disambiguate Implementors when the type name is not unique #38414

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

Merged
merged 5 commits into from
Jan 4, 2017
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
4 changes: 2 additions & 2 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2205,8 +2205,8 @@ impl Path {
}
}

pub fn last_name(&self) -> String {
self.segments.last().unwrap().name.clone()
pub fn last_name(&self) -> &str {
self.segments.last().unwrap().name.as_str()
}
}

Expand Down
45 changes: 32 additions & 13 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ pub fn href(did: DefId) -> Option<(String, ItemType, Vec<String>)> {
/// Used when rendering a `ResolvedPath` structure. This invokes the `path`
/// rendering function with the necessary arguments for linking to a local path.
fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
print_all: bool) -> fmt::Result {
print_all: bool, use_absolute: bool) -> fmt::Result {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you go through the patch and use the same name for this parameter everywhere (the use_absolute here)? That's one step that makes a bool parameter more bearable.

let last = path.segments.last().unwrap();
let rel_root = match &*path.segments[0].name {
"self" => Some("./".to_string()),
Expand Down Expand Up @@ -467,7 +467,17 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
if w.alternate() {
write!(w, "{:#}{:#}", HRef::new(did, &last.name), last.params)?;
} else {
write!(w, "{}{}", HRef::new(did, &last.name), last.params)?;
let path = if use_absolute {
match href(did) {
Some((_, _, fqp)) => format!("{}::{}",
fqp[..fqp.len()-1].join("::"),
HRef::new(did, fqp.last().unwrap())),
None => format!("{}", HRef::new(did, &last.name)),
}
} else {
format!("{}", HRef::new(did, &last.name))
};
write!(w, "{}{}", path, last.params)?;
}
Ok(())
}
Expand Down Expand Up @@ -551,15 +561,14 @@ impl<'a> fmt::Display for HRef<'a> {
}
}

impl fmt::Display for clean::Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt::Result {
match *t {
clean::Generic(ref name) => {
f.write_str(name)
}
clean::ResolvedPath{ did, ref typarams, ref path, is_generic } => {
// Paths like T::Output and Self::Output should be rendered with all segments
resolved_path(f, did, path, is_generic)?;
resolved_path(f, did, path, is_generic, use_absolute)?;
tybounds(f, typarams)
}
clean::Infer => write!(f, "_"),
Expand Down Expand Up @@ -718,7 +727,7 @@ impl fmt::Display for clean::Type {
write!(f, "{}::", self_type)?;
}
let path = clean::Path::singleton(name.clone());
resolved_path(f, did, &path, false)?;
resolved_path(f, did, &path, true, use_absolute)?;

// FIXME: `typarams` are not rendered, and this seems bad?
drop(typarams);
Expand All @@ -736,9 +745,17 @@ impl fmt::Display for clean::Type {
}
}
}

impl fmt::Display for clean::Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt_type(self, f, false)
}
}

fn fmt_impl(i: &clean::Impl, f: &mut fmt::Formatter, link_trait: bool) -> fmt::Result {
fn fmt_impl(i: &clean::Impl,
f: &mut fmt::Formatter,
link_trait: bool,
use_absolute: bool) -> fmt::Result {
let mut plain = String::new();

if f.alternate() {
Expand Down Expand Up @@ -772,7 +789,7 @@ fn fmt_impl(i: &clean::Impl, f: &mut fmt::Formatter, link_trait: bool) -> fmt::R
plain.push_str(" for ");
}

fmt::Display::fmt(&i.for_, f)?;
fmt_type(&i.for_, f, use_absolute)?;
plain.push_str(&format!("{:#}", i.for_));

fmt::Display::fmt(&WhereClause(&i.generics, plain.len() + 1), f)?;
Expand All @@ -781,13 +798,15 @@ fn fmt_impl(i: &clean::Impl, f: &mut fmt::Formatter, link_trait: bool) -> fmt::R

impl fmt::Display for clean::Impl {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt_impl(self, f, true)
fmt_impl(self, f, true, false)
}
}

// The difference from above is that trait is not hyperlinked.
pub fn fmt_impl_for_trait_page(i: &clean::Impl, f: &mut fmt::Formatter) -> fmt::Result {
fmt_impl(i, f, false)
pub fn fmt_impl_for_trait_page(i: &clean::Impl,
f: &mut fmt::Formatter,
use_absolute: bool) -> fmt::Result {
fmt_impl(i, f, false, use_absolute)
}

impl fmt::Display for clean::Arguments {
Expand Down Expand Up @@ -978,7 +997,7 @@ impl fmt::Display for clean::Import {
impl fmt::Display for clean::ImportSource {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.did {
Some(did) => resolved_path(f, did, &self.path, true),
Some(did) => resolved_path(f, did, &self.path, true, false),
_ => {
for (i, seg) in self.path.segments.iter().enumerate() {
if i > 0 {
Expand Down
20 changes: 18 additions & 2 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2111,9 +2111,25 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
<ul class='item-list' id='implementors-list'>
")?;
if let Some(implementors) = cache.implementors.get(&it.def_id) {
for i in implementors {
let mut implementor_count: FxHashMap<&str, usize> = FxHashMap();
for implementor in implementors {
if let clean::Type::ResolvedPath {ref path, ..} = implementor.impl_.for_ {
*implementor_count.entry(path.last_name()).or_insert(0) += 1;
}
}

for implementor in implementors {
write!(w, "<li><code>")?;
fmt_impl_for_trait_page(&i.impl_, w)?;
// If there's already another implementor that has the same abbridged name, use the
// full path, for example in `std::iter::ExactSizeIterator`
let use_absolute = if let clean::Type::ResolvedPath {
ref path, ..
} = implementor.impl_.for_ {
implementor_count[path.last_name()] > 1
} else {
false
};
fmt_impl_for_trait_page(&implementor.impl_, w, use_absolute)?;
writeln!(w, "</code></li>")?;
}
}
Expand Down