Auto merge of #42318 - GuillaumeGomez:rustdoc-fix-signature, r=QuietMisdreavus
Fix signature by adding parens when needed Fixes #42299.
This commit is contained in:
commit
fd7b44b78e
@ -469,7 +469,8 @@ pub fn href(did: DefId) -> Option<(String, ItemType, Vec<String>)> {
|
|||||||
/// Used when rendering a `ResolvedPath` structure. This invokes the `path`
|
/// Used when rendering a `ResolvedPath` structure. This invokes the `path`
|
||||||
/// rendering function with the necessary arguments for linking to a local 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,
|
fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
|
||||||
print_all: bool, use_absolute: bool, is_not_debug: bool) -> fmt::Result {
|
print_all: bool, use_absolute: bool, is_not_debug: bool,
|
||||||
|
need_paren: bool) -> fmt::Result {
|
||||||
let empty = clean::PathSegment {
|
let empty = clean::PathSegment {
|
||||||
name: String::new(),
|
name: String::new(),
|
||||||
params: clean::PathParameters::Parenthesized {
|
params: clean::PathParameters::Parenthesized {
|
||||||
@ -534,7 +535,7 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
|
|||||||
} else {
|
} else {
|
||||||
format!("{}", HRef::new(did, &last.name))
|
format!("{}", HRef::new(did, &last.name))
|
||||||
};
|
};
|
||||||
write!(w, "{}{}", path, last.params)?;
|
write!(w, "{}{}{}", if need_paren { "(" } else { "" }, path, last.params)?;
|
||||||
} else {
|
} else {
|
||||||
let path = if use_absolute {
|
let path = if use_absolute {
|
||||||
match href(did) {
|
match href(did) {
|
||||||
@ -547,7 +548,7 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
|
|||||||
} else {
|
} else {
|
||||||
format!("{:?}", HRef::new(did, &last.name))
|
format!("{:?}", HRef::new(did, &last.name))
|
||||||
};
|
};
|
||||||
write!(w, "{}{}", path, last.params)?;
|
write!(w, "{}{}{}", if need_paren { "(" } else { "" }, path, last.params)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -599,13 +600,17 @@ fn primitive_link(f: &mut fmt::Formatter,
|
|||||||
|
|
||||||
/// Helper to render type parameters
|
/// Helper to render type parameters
|
||||||
fn tybounds(w: &mut fmt::Formatter,
|
fn tybounds(w: &mut fmt::Formatter,
|
||||||
typarams: &Option<Vec<clean::TyParamBound> >) -> fmt::Result {
|
typarams: &Option<Vec<clean::TyParamBound>>,
|
||||||
|
need_paren: bool) -> fmt::Result {
|
||||||
match *typarams {
|
match *typarams {
|
||||||
Some(ref params) => {
|
Some(ref params) => {
|
||||||
for param in params {
|
for param in params {
|
||||||
write!(w, " + ")?;
|
write!(w, " + ")?;
|
||||||
fmt::Display::fmt(param, w)?;
|
fmt::Display::fmt(param, w)?;
|
||||||
}
|
}
|
||||||
|
if need_paren {
|
||||||
|
write!(w, ")")?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
None => Ok(())
|
None => Ok(())
|
||||||
@ -639,15 +644,19 @@ impl<'a> fmt::Debug for HRef<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
|
fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
|
||||||
is_not_debug: bool) -> fmt::Result {
|
is_not_debug: bool, is_ref: bool) -> fmt::Result {
|
||||||
match *t {
|
match *t {
|
||||||
clean::Generic(ref name) => {
|
clean::Generic(ref name) => {
|
||||||
f.write_str(name)
|
f.write_str(name)
|
||||||
}
|
}
|
||||||
clean::ResolvedPath{ did, ref typarams, ref path, is_generic } => {
|
clean::ResolvedPath{ did, ref typarams, ref path, is_generic } => {
|
||||||
// Paths like T::Output and Self::Output should be rendered with all segments
|
// Paths like T::Output and Self::Output should be rendered with all segments
|
||||||
resolved_path(f, did, path, is_generic, use_absolute, is_not_debug)?;
|
let need_paren = match *typarams {
|
||||||
tybounds(f, typarams)
|
Some(ref v) => !v.is_empty(),
|
||||||
|
_ => false,
|
||||||
|
} && is_ref;
|
||||||
|
resolved_path(f, did, path, is_generic, use_absolute, is_not_debug, need_paren)?;
|
||||||
|
tybounds(f, typarams, need_paren)
|
||||||
}
|
}
|
||||||
clean::Infer => write!(f, "_"),
|
clean::Infer => write!(f, "_"),
|
||||||
clean::Primitive(prim) if is_not_debug => primitive_link(f, prim, prim.as_str()),
|
clean::Primitive(prim) if is_not_debug => primitive_link(f, prim, prim.as_str()),
|
||||||
@ -788,14 +797,14 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
|
|||||||
_ => {
|
_ => {
|
||||||
if f.alternate() {
|
if f.alternate() {
|
||||||
write!(f, "&{}{}", lt, m)?;
|
write!(f, "&{}{}", lt, m)?;
|
||||||
fmt_type(&ty, f, use_absolute, is_not_debug)
|
fmt_type(&ty, f, use_absolute, is_not_debug, true)
|
||||||
} else {
|
} else {
|
||||||
if is_not_debug {
|
if is_not_debug {
|
||||||
write!(f, "&{}{}", lt, m)?;
|
write!(f, "&{}{}", lt, m)?;
|
||||||
} else {
|
} else {
|
||||||
write!(f, "&{}{}", lt, m)?;
|
write!(f, "&{}{}", lt, m)?;
|
||||||
}
|
}
|
||||||
fmt_type(&ty, f, use_absolute, is_not_debug)
|
fmt_type(&ty, f, use_absolute, is_not_debug, true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -865,7 +874,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
|
|||||||
// look at).
|
// look at).
|
||||||
box clean::ResolvedPath { did, ref typarams, .. } => {
|
box clean::ResolvedPath { did, ref typarams, .. } => {
|
||||||
let path = clean::Path::singleton(name.clone());
|
let path = clean::Path::singleton(name.clone());
|
||||||
resolved_path(f, did, &path, true, use_absolute, is_not_debug)?;
|
resolved_path(f, did, &path, true, use_absolute, is_not_debug, false)?;
|
||||||
|
|
||||||
// FIXME: `typarams` are not rendered, and this seems bad?
|
// FIXME: `typarams` are not rendered, and this seems bad?
|
||||||
drop(typarams);
|
drop(typarams);
|
||||||
@ -884,13 +893,13 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
|
|||||||
|
|
||||||
impl fmt::Display for clean::Type {
|
impl fmt::Display for clean::Type {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt_type(self, f, false, true)
|
fmt_type(self, f, false, true, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for clean::Type {
|
impl fmt::Debug for clean::Type {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt_type(self, f, false, false)
|
fmt_type(self, f, false, false, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -924,7 +933,7 @@ fn fmt_impl(i: &clean::Impl,
|
|||||||
write!(f, " for ")?;
|
write!(f, " for ")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt_type(&i.for_, f, use_absolute, true)?;
|
fmt_type(&i.for_, f, use_absolute, true, false)?;
|
||||||
|
|
||||||
fmt::Display::fmt(&WhereClause { gens: &i.generics, indent: 0, end_newline: true }, f)?;
|
fmt::Display::fmt(&WhereClause { gens: &i.generics, indent: 0, end_newline: true }, f)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -1130,7 +1139,7 @@ impl fmt::Display for clean::Import {
|
|||||||
impl fmt::Display for clean::ImportSource {
|
impl fmt::Display for clean::ImportSource {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self.did {
|
match self.did {
|
||||||
Some(did) => resolved_path(f, did, &self.path, true, false, true),
|
Some(did) => resolved_path(f, did, &self.path, true, false, true, false),
|
||||||
_ => {
|
_ => {
|
||||||
for (i, seg) in self.path.segments.iter().enumerate() {
|
for (i, seg) in self.path.segments.iter().enumerate() {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
|
15
src/test/rustdoc/test-parens.rs
Normal file
15
src/test/rustdoc/test-parens.rs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
// Copyright 2017 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.
|
||||||
|
|
||||||
|
#![crate_name = "foo"]
|
||||||
|
|
||||||
|
// @has foo/fn.foo.html
|
||||||
|
// @has - '//*[@class="rust fn"]' "_: &(ToString + 'static)"
|
||||||
|
pub fn foo(_: &(ToString + 'static)) {}
|
Loading…
Reference in New Issue
Block a user