Skip to content

Commit 5d0dca3

Browse files
committed
Rollup merge of rust-lang#32558 - sanxiyn:rustdoc-self-link, r=steveklabnik
Avoid linking to itself in implementors section of trait page Fix rust-lang#32474.
2 parents 63760ac + 87030f6 commit 5d0dca3

File tree

4 files changed

+54
-10
lines changed

4 files changed

+54
-10
lines changed

src/librustdoc/clean/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,13 @@ impl Type {
15181518
_ => None,
15191519
}
15201520
}
1521+
1522+
pub fn trait_name(&self) -> Option<String> {
1523+
match *self {
1524+
ResolvedPath { ref path, .. } => Some(path.last_name()),
1525+
_ => None,
1526+
}
1527+
}
15211528
}
15221529

15231530
impl GetDefId for Type {
@@ -2009,6 +2016,10 @@ impl Path {
20092016
}]
20102017
}
20112018
}
2019+
2020+
pub fn last_name(&self) -> String {
2021+
self.segments.last().unwrap().name.clone()
2022+
}
20122023
}
20132024

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

src/librustdoc/html/format.rs

Lines changed: 23 additions & 9 deletions
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

Lines changed: 4 additions & 1 deletion
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};
@@ -2010,7 +2011,9 @@ fn item_trait(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
20102011
match cache.implementors.get(&it.def_id) {
20112012
Some(implementors) => {
20122013
for i in implementors {
2013-
writeln!(w, "<li><code>{}</code></li>", i.impl_)?;
2014+
write!(w, "<li><code>")?;
2015+
fmt_impl_for_trait_page(&i.impl_, w)?;
2016+
writeln!(w, "</code></li>")?;
20142017
}
20152018
}
20162019
None => {}

src/test/rustdoc/trait-self-link.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// @!has trait_self_link/trait.Foo.html //a/@href ../trait_self_link/trait.Foo.html
12+
pub trait Foo {}
13+
14+
pub struct Bar;
15+
16+
impl Foo for Bar {}

0 commit comments

Comments
 (0)