about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSteve Klabnik <steve@steveklabnik.com>2016-04-18 14:50:34 -0400
committerSteve Klabnik <steve@steveklabnik.com>2016-04-18 14:50:34 -0400
commit5d0dca363fb451d36b50af3a215bf96ed6021403 (patch)
treedc76659a8ca26553a5489f8051ddedfa8f36f1cb
parent63760acf31e605c8a91bb5cbcbcb4bfb95435e48 (diff)
parent87030f603ba3a0b66387daa87b8b2f836661e51e (diff)
downloadrust-5d0dca363fb451d36b50af3a215bf96ed6021403.tar.gz
rust-5d0dca363fb451d36b50af3a215bf96ed6021403.zip
Rollup merge of #32558 - sanxiyn:rustdoc-self-link, r=steveklabnik
Avoid linking to itself in implementors section of trait page

Fix #32474.
-rw-r--r--src/librustdoc/clean/mod.rs11
-rw-r--r--src/librustdoc/html/format.rs32
-rw-r--r--src/librustdoc/html/render.rs5
-rw-r--r--src/test/rustdoc/trait-self-link.rs16
4 files changed, 54 insertions, 10 deletions
diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs
index 42db93e1803..023da1612e6 100644
--- a/src/librustdoc/clean/mod.rs
+++ b/src/librustdoc/clean/mod.rs
@@ -1518,6 +1518,13 @@ impl Type {
             _ => None,
         }
     }
+
+    pub fn trait_name(&self) -> Option<String> {
+        match *self {
+            ResolvedPath { ref path, .. } => Some(path.last_name()),
+            _ => None,
+        }
+    }
 }
 
 impl GetDefId for Type {
@@ -2009,6 +2016,10 @@ impl Path {
             }]
         }
     }
+
+    pub fn last_name(&self) -> String {
+        self.segments.last().unwrap().name.clone()
+    }
 }
 
 impl Clean<Path> for hir::Path {
diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs
index d2b43ae5bda..1a228b4af8b 100644
--- a/src/librustdoc/html/format.rs
+++ b/src/librustdoc/html/format.rs
@@ -561,19 +561,33 @@ impl fmt::Display for clean::Type {
     }
 }
 
+fn fmt_impl(i: &clean::Impl, f: &mut fmt::Formatter, link_trait: bool) -> fmt::Result {
+    write!(f, "impl{} ", i.generics)?;
+    if let Some(ref ty) = i.trait_ {
+        write!(f, "{}",
+               if i.polarity == Some(clean::ImplPolarity::Negative) { "!" } else { "" })?;
+        if link_trait {
+            write!(f, "{}", *ty)?;
+        } else {
+            write!(f, "{}", ty.trait_name().unwrap())?;
+        }
+        write!(f, " for ")?;
+    }
+    write!(f, "{}{}", i.for_, WhereClause(&i.generics))?;
+    Ok(())
+}
+
 impl fmt::Display for clean::Impl {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "impl{} ", self.generics)?;
-        if let Some(ref ty) = self.trait_ {
-            write!(f, "{}{} for ",
-                   if self.polarity == Some(clean::ImplPolarity::Negative) { "!" } else { "" },
-                   *ty)?;
-        }
-        write!(f, "{}{}", self.for_, WhereClause(&self.generics))?;
-        Ok(())
+        fmt_impl(self, f, true)
     }
 }
 
+// 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)
+}
+
 impl fmt::Display for clean::Arguments {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         for (i, input) in self.values.iter().enumerate() {
@@ -667,7 +681,7 @@ impl fmt::Display for clean::Import {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
         match *self {
             clean::SimpleImport(ref name, ref src) => {
-                if *name == src.path.segments.last().unwrap().name {
+                if *name == src.path.last_name() {
                     write!(f, "use {};", *src)
                 } else {
                     write!(f, "use {} as {};", *src, *name)
diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs
index 864e10407d5..31071cdc3ca 100644
--- a/src/librustdoc/html/render.rs
+++ b/src/librustdoc/html/render.rs
@@ -69,6 +69,7 @@ use html::escape::Escape;
 use html::format::{ConstnessSpace};
 use html::format::{TyParamBounds, WhereClause, href, AbiSpace};
 use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace};
+use html::format::fmt_impl_for_trait_page;
 use html::item_type::ItemType;
 use html::markdown::{self, Markdown};
 use html::{highlight, layout};
@@ -2010,7 +2011,9 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
     match cache.implementors.get(&it.def_id) {
         Some(implementors) => {
             for i in implementors {
-                writeln!(w, "<li><code>{}</code></li>", i.impl_)?;
+                write!(w, "<li><code>")?;
+                fmt_impl_for_trait_page(&i.impl_, w)?;
+                writeln!(w, "</code></li>")?;
             }
         }
         None => {}
diff --git a/src/test/rustdoc/trait-self-link.rs b/src/test/rustdoc/trait-self-link.rs
new file mode 100644
index 00000000000..3233fb96c5c
--- /dev/null
+++ b/src/test/rustdoc/trait-self-link.rs
@@ -0,0 +1,16 @@
+// 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 trait_self_link/trait.Foo.html //a/@href ../trait_self_link/trait.Foo.html
+pub trait Foo {}
+
+pub struct Bar;
+
+impl Foo for Bar {}