Skip to content

Commit b308a8c

Browse files
committed
Avoid linking to itself in implementors section of trait page
1 parent a111297 commit b308a8c

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

src/librustdoc/clean/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1505,6 +1505,13 @@ impl Type {
15051505
_ => None,
15061506
}
15071507
}
1508+
1509+
pub fn trait_name(&self) -> Option<String> {
1510+
match *self {
1511+
ResolvedPath { ref path, .. } => Some(path.last_name()),
1512+
_ => None,
1513+
}
1514+
}
15081515
}
15091516

15101517
impl GetDefId for Type {
@@ -1994,6 +2001,10 @@ impl Path {
19942001
}]
19952002
}
19962003
}
2004+
2005+
pub fn last_name(&self) -> String {
2006+
self.segments.last().unwrap().name.clone()
2007+
}
19972008
}
19982009

19992010
impl Clean<Path> for hir::Path {

src/librustdoc/html/format.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -561,19 +561,33 @@ impl fmt::Display for clean::Type {
561561
}
562562
}
563563

564+
fn fmt_impl(i: &clean::Impl, f: &mut fmt::Formatter, link_trait: bool) -> fmt::Result {
565+
write!(f, "impl{} ", i.generics)?;
566+
if let Some(ref ty) = i.trait_ {
567+
write!(f, "{}",
568+
if i.polarity == Some(clean::ImplPolarity::Negative) { "!" } else { "" })?;
569+
if link_trait {
570+
write!(f, "{}", *ty)?;
571+
} else {
572+
write!(f, "{}", ty.trait_name().unwrap())?;
573+
}
574+
write!(f, " for ")?;
575+
}
576+
write!(f, "{}{}", i.for_, WhereClause(&i.generics))?;
577+
Ok(())
578+
}
579+
564580
impl fmt::Display for clean::Impl {
565581
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
566-
write!(f, "impl{} ", self.generics)?;
567-
if let Some(ref ty) = self.trait_ {
568-
write!(f, "{}{} for ",
569-
if self.polarity == Some(clean::ImplPolarity::Negative) { "!" } else { "" },
570-
*ty)?;
571-
}
572-
write!(f, "{}{}", self.for_, WhereClause(&self.generics))?;
573-
Ok(())
582+
fmt_impl(self, f, true)
574583
}
575584
}
576585

586+
// The difference from above is that trait is not hyperlinked.
587+
pub fn fmt_impl_for_trait_page(i: &clean::Impl, f: &mut fmt::Formatter) -> fmt::Result {
588+
fmt_impl(i, f, false)
589+
}
590+
577591
impl fmt::Display for clean::Arguments {
578592
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
579593
for (i, input) in self.values.iter().enumerate() {
@@ -667,7 +681,7 @@ impl fmt::Display for clean::Import {
667681
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
668682
match *self {
669683
clean::SimpleImport(ref name, ref src) => {
670-
if *name == src.path.segments.last().unwrap().name {
684+
if *name == src.path.last_name() {
671685
write!(f, "use {};", *src)
672686
} else {
673687
write!(f, "use {} as {};", *src, *name)

src/librustdoc/html/render.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ use html::escape::Escape;
6969
use html::format::{ConstnessSpace};
7070
use html::format::{TyParamBounds, WhereClause, href, AbiSpace};
7171
use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace};
72+
use html::format::fmt_impl_for_trait_page;
7273
use html::item_type::ItemType;
7374
use html::markdown::{self, Markdown};
7475
use html::{highlight, layout};
@@ -2006,7 +2007,9 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
20062007
match cache.implementors.get(&it.def_id) {
20072008
Some(implementors) => {
20082009
for i in implementors {
2009-
writeln!(w, "<li><code>{}</code></li>", i.impl_)?;
2010+
write!(w, "<li><code>")?;
2011+
fmt_impl_for_trait_page(&i.impl_, w)?;
2012+
writeln!(w, "</code></li>")?;
20102013
}
20112014
}
20122015
None => {}

0 commit comments

Comments
 (0)