Skip to content

rustdoc: rename the type in an impl to match its containing page #56013

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

Closed
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
55 changes: 36 additions & 19 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
@@ -440,28 +440,29 @@ 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, use_absolute: bool) -> fmt::Result {
print_all: bool, use_absolute: bool, rename: Option<&str>) -> fmt::Result {
let last = path.segments.last().unwrap();
let last_name = rename.unwrap_or(&last.name);

if print_all {
for seg in &path.segments[..path.segments.len() - 1] {
write!(w, "{}::", seg.name)?;
}
}
if w.alternate() {
write!(w, "{:#}{:#}", HRef::new(did, &last.name), last.args)?;
write!(w, "{:#}{:#}", HRef::new(did, last_name), last.args)?;
} else {
let path = if use_absolute {
match href(did) {
Some((_, _, fqp)) => {
format!("{}::{}",
fqp[..fqp.len() - 1].join("::"),
HRef::new(did, fqp.last().unwrap()))
HRef::new(did, rename.or(fqp.last().map(|n| &**n)).unwrap()))
}
None => HRef::new(did, &last.name).to_string(),
None => HRef::new(did, last_name).to_string(),
}
} else {
HRef::new(did, &last.name).to_string()
HRef::new(did, last_name).to_string()
};
write!(w, "{}{}", path, last.args)?;
}
@@ -547,7 +548,14 @@ impl<'a> fmt::Display for HRef<'a> {
}
}

fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt::Result {
/// Writes the given type into the given formatter, optionally printing its full path or using the
/// given name instead.
fn fmt_type(
t: &clean::Type,
f: &mut fmt::Formatter,
use_absolute: bool,
rename: Option<&str>,
) -> fmt::Result {
match *t {
clean::Generic(ref name) => {
f.write_str(name)
@@ -557,7 +565,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
f.write_str("dyn ")?;
}
// Paths like T::Output and Self::Output should be rendered with all segments
resolved_path(f, did, path, is_generic, use_absolute)?;
resolved_path(f, did, path, is_generic, use_absolute, rename)?;
tybounds(f, typarams)
}
clean::Infer => write!(f, "_"),
@@ -657,17 +665,17 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
}
clean::ResolvedPath { typarams: Some(ref v), .. } if !v.is_empty() => {
write!(f, "{}{}{}(", amp, lt, m)?;
fmt_type(&ty, f, use_absolute)?;
fmt_type(&ty, f, use_absolute, rename)?;
write!(f, ")")
}
clean::Generic(..) => {
primitive_link(f, PrimitiveType::Reference,
&format!("{}{}{}", amp, lt, m))?;
fmt_type(&ty, f, use_absolute)
fmt_type(&ty, f, use_absolute, rename)
}
_ => {
write!(f, "{}{}{}", amp, lt, m)?;
fmt_type(&ty, f, use_absolute)
fmt_type(&ty, f, use_absolute, rename)
}
}
}
@@ -736,14 +744,15 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:

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

fn fmt_impl(i: &clean::Impl,
f: &mut fmt::Formatter,
link_trait: bool,
use_absolute: bool) -> fmt::Result {
use_absolute: bool,
rename: Option<&str>) -> fmt::Result {
if f.alternate() {
write!(f, "impl{:#} ", i.generics)?;
} else {
@@ -771,9 +780,9 @@ fn fmt_impl(i: &clean::Impl,
}

if let Some(ref ty) = i.blanket_impl {
fmt_type(ty, f, use_absolute)?;
fmt_type(ty, f, use_absolute, rename)?;
} else {
fmt_type(&i.for_, f, use_absolute)?;
fmt_type(&i.for_, f, use_absolute, rename)?;
}

fmt::Display::fmt(&WhereClause { gens: &i.generics, indent: 0, end_newline: true }, f)?;
@@ -782,15 +791,23 @@ fn fmt_impl(i: &clean::Impl,

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

// The difference from above is that trait is not hyperlinked.
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)
use_absolute: bool,
rename: Option<&str>) -> fmt::Result {
fmt_impl(i, f, false, use_absolute, rename)
}

/// Renders an `impl` block signature, but with the `for` type renamed to the given name.
pub fn fmt_impl_renamed(i: &clean::Impl,
f: &mut fmt::Formatter,
rename: Option<&str>) -> fmt::Result {
fmt_impl(i, f, true, false, rename)
}

impl fmt::Display for clean::Arguments {
@@ -946,7 +963,7 @@ impl<'a> fmt::Display for VisSpace<'a> {
{
f.write_str("in ")?;
}
resolved_path(f, did, path, true, false)?;
resolved_path(f, did, path, true, false, None)?;
f.write_str(") ")
}
}
@@ -1004,7 +1021,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, false),
Some(did) => resolved_path(f, did, &self.path, true, false, None),
_ => {
for (i, seg) in self.path.segments.iter().enumerate() {
if i > 0 {
25 changes: 15 additions & 10 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ use html::escape::Escape;
use html::format::{AsyncSpace, ConstnessSpace};
use html::format::{GenericBounds, WhereClause, href, AbiSpace};
use html::format::{VisSpace, Method, UnsafetySpace, MutableSpace};
use html::format::fmt_impl_for_trait_page;
use html::format::{fmt_impl_for_trait_page, fmt_impl_renamed};
use html::item_type::ItemType;
use html::markdown::{self, Markdown, MarkdownHtml, MarkdownSummaryLine, ErrorCodes, IdMap};
use html::{highlight, layout, static_files};
@@ -2796,7 +2796,7 @@ fn render_implementor(cx: &Context, implementor: &Impl, w: &mut fmt::Formatter,
_ => false,
};
render_impl(w, cx, implementor, AssocItemLink::Anchor(None), RenderMode::Normal,
implementor.impl_item.stable_since(), false, Some(use_absolute))?;
implementor.impl_item.stable_since(), false, Some(use_absolute), None)?;
Ok(())
}

@@ -2806,8 +2806,9 @@ fn render_impls(cx: &Context, w: &mut fmt::Formatter,
for i in traits {
let did = i.trait_did().unwrap();
let assoc_link = AssocItemLink::GotoSource(did, &i.inner_impl().provided_trait_methods);
render_impl(w, cx, i, assoc_link,
RenderMode::Normal, containing_item.stable_since(), true, None)?;
let rename = containing_item.name.deref();
render_impl(w, cx, i, assoc_link, RenderMode::Normal,
containing_item.stable_since(), true, None, rename)?;
}
Ok(())
}
@@ -3065,7 +3066,7 @@ fn item_trait(
);
render_impl(w, cx, &implementor, assoc_link,
RenderMode::Normal, implementor.impl_item.stable_since(), false,
None)?;
None, None)?;
}
}

@@ -3713,9 +3714,10 @@ fn render_assoc_items(w: &mut fmt::Formatter,
RenderMode::ForDeref { mut_: deref_mut_ }
}
};
let rename = containing_item.name.deref();
for i in &non_trait {
render_impl(w, cx, i, AssocItemLink::Anchor(None), render_mode,
containing_item.stable_since(), true, None)?;
containing_item.stable_since(), true, None, rename)?;
}
}
if let AssocItemRender::DerefFor { .. } = what {
@@ -3896,7 +3898,8 @@ fn spotlight_decl(decl: &clean::FnDecl) -> Result<String, fmt::Error> {

fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLink,
render_mode: RenderMode, outer_version: Option<&str>,
show_def_docs: bool, use_absolute: Option<bool>) -> fmt::Result {
show_def_docs: bool, use_absolute: Option<bool>, rename: Option<&str>,
) -> fmt::Result {
if render_mode == RenderMode::Normal {
let id = cx.derive_id(match i.inner_impl().trait_ {
Some(ref t) => format!("impl-{}", small_url_encode(&format!("{:#}", t))),
@@ -3905,7 +3908,7 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
if let Some(use_absolute) = use_absolute {
write!(w, "<h3 id='{}' class='impl'><span class='in-band'><table class='table-display'>\
<tbody><tr><td><code>", id)?;
fmt_impl_for_trait_page(&i.inner_impl(), w, use_absolute)?;
fmt_impl_for_trait_page(&i.inner_impl(), w, use_absolute, rename)?;
if show_def_docs {
for it in &i.inner_impl().items {
if let clean::TypedefItem(ref tydef, _) = it.inner {
@@ -3919,8 +3922,10 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
write!(w, "</code>")?;
} else {
write!(w, "<h3 id='{}' class='impl'><span class='in-band'><table class='table-display'>\
<tbody><tr><td><code>{}</code>",
id, i.inner_impl())?;
<tbody><tr><td><code>",
id)?;
fmt_impl_renamed(i.inner_impl(), w, rename)?;
write!(w, "</code>")?;
}
write!(w, "<a href='#{}' class='anchor'></a>", id)?;
write!(w, "</span></td><td><span class='out-of-band'>")?;
1 change: 1 addition & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
#![feature(crate_visibility_modifier)]
#![feature(const_fn)]
#![feature(drain_filter)]
#![feature(inner_deref)]

#![recursion_limit="256"]

24 changes: 24 additions & 0 deletions src/test/rustdoc/inline_local/rename.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2018 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.

mod inner {
pub struct SomeStruct;

impl SomeStruct {
pub fn new() -> SomeStruct { SomeStruct }
}
}

// @has rename/index.html
// @has - '//a/@href' 'struct.MyStruct.html'
// @has rename/struct.MyStruct.html
// @has - '//code' 'impl MyStruct'
// @!has - '//code' 'impl SomeStruct'
pub use inner::SomeStruct as MyStruct;