Skip to content

Commit

Permalink
Fix signature by adding parens when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed May 30, 2017
1 parent f89d8d1 commit 171d285
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/librustdoc/html/format.rs
Expand Up @@ -469,7 +469,8 @@ 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, 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 {
name: String::new(),
params: clean::PathParameters::Parenthesized {
Expand Down Expand Up @@ -534,7 +535,7 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
} else {
format!("{}", HRef::new(did, &last.name))
};
write!(w, "{}{}", path, last.params)?;
write!(w, "{}{}{}", if need_paren { "(" } else { "" }, path, last.params)?;
} else {
let path = if use_absolute {
match href(did) {
Expand All @@ -547,7 +548,7 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
} else {
format!("{:?}", HRef::new(did, &last.name))
};
write!(w, "{}{}", path, last.params)?;
write!(w, "{}{}{}", if need_paren { "(" } else { "" }, path, last.params)?;
}
}
Ok(())
Expand Down Expand Up @@ -599,13 +600,17 @@ fn primitive_link(f: &mut fmt::Formatter,

/// Helper to render type parameters
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 {
Some(ref params) => {
for param in params {
write!(w, " + ")?;
fmt::Display::fmt(param, w)?;
}
if need_paren {
write!(w, ")")?;
}
Ok(())
}
None => Ok(())
Expand Down Expand Up @@ -639,15 +644,19 @@ impl<'a> fmt::Debug for HRef<'a> {
}

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 {
clean::Generic(ref name) => {
f.write_str(name)
}
clean::ResolvedPath{ did, ref typarams, ref path, is_generic } => {
// 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)?;
tybounds(f, typarams)
let need_paren = match *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::Primitive(prim) if is_not_debug => primitive_link(f, prim, prim.as_str()),
Expand Down Expand Up @@ -788,14 +797,14 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
_ => {
if f.alternate() {
write!(f, "&{}{}", lt, m)?;
fmt_type(&ty, f, use_absolute, is_not_debug)
fmt_type(&ty, f, use_absolute, is_not_debug, true)
} else {
if is_not_debug {
write!(f, "&amp;{}{}", lt, m)?;
} else {
write!(f, "&{}{}", lt, m)?;
}
fmt_type(&ty, f, use_absolute, is_not_debug)
fmt_type(&ty, f, use_absolute, is_not_debug, true)
}
}
}
Expand Down Expand Up @@ -865,7 +874,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
// look at).
box clean::ResolvedPath { did, ref typarams, .. } => {
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?
drop(typarams);
Expand All @@ -884,13 +893,13 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,

impl fmt::Display for clean::Type {
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 {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt_type(self, f, false, false)
fmt_type(self, f, false, false, false)
}
}

Expand Down Expand Up @@ -924,7 +933,7 @@ fn fmt_impl(i: &clean::Impl,
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)?;
Ok(())
Expand Down Expand Up @@ -1130,7 +1139,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, true),
Some(did) => resolved_path(f, did, &self.path, true, false, true, false),
_ => {
for (i, seg) in self.path.segments.iter().enumerate() {
if i > 0 {
Expand Down
15 changes: 15 additions & 0 deletions src/test/rustdoc/test-parens.rs
@@ -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)) {}

0 comments on commit 171d285

Please sign in to comment.