Skip to content

Commit fd7b44b

Browse files
committed
Auto merge of #42318 - GuillaumeGomez:rustdoc-fix-signature, r=QuietMisdreavus
Fix signature by adding parens when needed Fixes #42299.
2 parents f89d8d1 + 171d285 commit fd7b44b

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

src/librustdoc/html/format.rs

+23-14
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ pub fn href(did: DefId) -> Option<(String, ItemType, Vec<String>)> {
469469
/// Used when rendering a `ResolvedPath` structure. This invokes the `path`
470470
/// rendering function with the necessary arguments for linking to a local path.
471471
fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
472-
print_all: bool, use_absolute: bool, is_not_debug: bool) -> fmt::Result {
472+
print_all: bool, use_absolute: bool, is_not_debug: bool,
473+
need_paren: bool) -> fmt::Result {
473474
let empty = clean::PathSegment {
474475
name: String::new(),
475476
params: clean::PathParameters::Parenthesized {
@@ -534,7 +535,7 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
534535
} else {
535536
format!("{}", HRef::new(did, &last.name))
536537
};
537-
write!(w, "{}{}", path, last.params)?;
538+
write!(w, "{}{}{}", if need_paren { "(" } else { "" }, path, last.params)?;
538539
} else {
539540
let path = if use_absolute {
540541
match href(did) {
@@ -547,7 +548,7 @@ fn resolved_path(w: &mut fmt::Formatter, did: DefId, path: &clean::Path,
547548
} else {
548549
format!("{:?}", HRef::new(did, &last.name))
549550
};
550-
write!(w, "{}{}", path, last.params)?;
551+
write!(w, "{}{}{}", if need_paren { "(" } else { "" }, path, last.params)?;
551552
}
552553
}
553554
Ok(())
@@ -599,13 +600,17 @@ fn primitive_link(f: &mut fmt::Formatter,
599600

600601
/// Helper to render type parameters
601602
fn tybounds(w: &mut fmt::Formatter,
602-
typarams: &Option<Vec<clean::TyParamBound> >) -> fmt::Result {
603+
typarams: &Option<Vec<clean::TyParamBound>>,
604+
need_paren: bool) -> fmt::Result {
603605
match *typarams {
604606
Some(ref params) => {
605607
for param in params {
606608
write!(w, " + ")?;
607609
fmt::Display::fmt(param, w)?;
608610
}
611+
if need_paren {
612+
write!(w, ")")?;
613+
}
609614
Ok(())
610615
}
611616
None => Ok(())
@@ -639,15 +644,19 @@ impl<'a> fmt::Debug for HRef<'a> {
639644
}
640645

641646
fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
642-
is_not_debug: bool) -> fmt::Result {
647+
is_not_debug: bool, is_ref: bool) -> fmt::Result {
643648
match *t {
644649
clean::Generic(ref name) => {
645650
f.write_str(name)
646651
}
647652
clean::ResolvedPath{ did, ref typarams, ref path, is_generic } => {
648653
// Paths like T::Output and Self::Output should be rendered with all segments
649-
resolved_path(f, did, path, is_generic, use_absolute, is_not_debug)?;
650-
tybounds(f, typarams)
654+
let need_paren = match *typarams {
655+
Some(ref v) => !v.is_empty(),
656+
_ => false,
657+
} && is_ref;
658+
resolved_path(f, did, path, is_generic, use_absolute, is_not_debug, need_paren)?;
659+
tybounds(f, typarams, need_paren)
651660
}
652661
clean::Infer => write!(f, "_"),
653662
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,
788797
_ => {
789798
if f.alternate() {
790799
write!(f, "&{}{}", lt, m)?;
791-
fmt_type(&ty, f, use_absolute, is_not_debug)
800+
fmt_type(&ty, f, use_absolute, is_not_debug, true)
792801
} else {
793802
if is_not_debug {
794803
write!(f, "&amp;{}{}", lt, m)?;
795804
} else {
796805
write!(f, "&{}{}", lt, m)?;
797806
}
798-
fmt_type(&ty, f, use_absolute, is_not_debug)
807+
fmt_type(&ty, f, use_absolute, is_not_debug, true)
799808
}
800809
}
801810
}
@@ -865,7 +874,7 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
865874
// look at).
866875
box clean::ResolvedPath { did, ref typarams, .. } => {
867876
let path = clean::Path::singleton(name.clone());
868-
resolved_path(f, did, &path, true, use_absolute, is_not_debug)?;
877+
resolved_path(f, did, &path, true, use_absolute, is_not_debug, false)?;
869878

870879
// FIXME: `typarams` are not rendered, and this seems bad?
871880
drop(typarams);
@@ -884,13 +893,13 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool,
884893

885894
impl fmt::Display for clean::Type {
886895
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
887-
fmt_type(self, f, false, true)
896+
fmt_type(self, f, false, true, false)
888897
}
889898
}
890899

891900
impl fmt::Debug for clean::Type {
892901
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
893-
fmt_type(self, f, false, false)
902+
fmt_type(self, f, false, false, false)
894903
}
895904
}
896905

@@ -924,7 +933,7 @@ fn fmt_impl(i: &clean::Impl,
924933
write!(f, " for ")?;
925934
}
926935

927-
fmt_type(&i.for_, f, use_absolute, true)?;
936+
fmt_type(&i.for_, f, use_absolute, true, false)?;
928937

929938
fmt::Display::fmt(&WhereClause { gens: &i.generics, indent: 0, end_newline: true }, f)?;
930939
Ok(())
@@ -1130,7 +1139,7 @@ impl fmt::Display for clean::Import {
11301139
impl fmt::Display for clean::ImportSource {
11311140
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
11321141
match self.did {
1133-
Some(did) => resolved_path(f, did, &self.path, true, false, true),
1142+
Some(did) => resolved_path(f, did, &self.path, true, false, true, false),
11341143
_ => {
11351144
for (i, seg) in self.path.segments.iter().enumerate() {
11361145
if i > 0 {

src/test/rustdoc/test-parens.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2017 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+
#![crate_name = "foo"]
12+
13+
// @has foo/fn.foo.html
14+
// @has - '//*[@class="rust fn"]' "_: &(ToString + 'static)"
15+
pub fn foo(_: &(ToString + 'static)) {}

0 commit comments

Comments
 (0)