From 002b000e324325e24ed3bfd6c20f9b6db511c4d5 Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Fri, 16 Nov 2018 18:31:21 -0600 Subject: [PATCH] rename the type in an impl to match its containing page --- src/librustdoc/html/format.rs | 55 ++++++++++++++++--------- src/librustdoc/html/render.rs | 25 ++++++----- src/librustdoc/lib.rs | 1 + src/test/rustdoc/inline_local/rename.rs | 24 +++++++++++ 4 files changed, 76 insertions(+), 29 deletions(-) create mode 100644 src/test/rustdoc/inline_local/rename.rs diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 445fc2e833a3f..ca60ca927d452 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -440,8 +440,9 @@ pub fn href(did: DefId) -> Option<(String, ItemType, Vec)> { /// 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] { @@ -449,19 +450,19 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path, } } 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 { diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index b46efb20d8f6b..2c29a95c51dd9 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -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 { 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) -> fmt::Result { + show_def_docs: bool, use_absolute: Option, 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, "

\
", 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, "")?; } else { write!(w, "

\ -
{}", - id, i.inner_impl())?; +
", + id)?; + fmt_impl_renamed(i.inner_impl(), w, rename)?; + write!(w, "")?; } write!(w, "", id)?; write!(w, "")?; diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 4b043b26d8650..f7b621bd9428a 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -25,6 +25,7 @@ #![feature(crate_visibility_modifier)] #![feature(const_fn)] #![feature(drain_filter)] +#![feature(inner_deref)] #![recursion_limit="256"] diff --git a/src/test/rustdoc/inline_local/rename.rs b/src/test/rustdoc/inline_local/rename.rs new file mode 100644 index 0000000000000..c280d69678bbd --- /dev/null +++ b/src/test/rustdoc/inline_local/rename.rs @@ -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 or the MIT license +// , 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;