Skip to content

Commit 8d1e432

Browse files
committed
Rollup merge of rust-lang#58409 - euclio:impl-trait-wrapping, r=QuietMisdreavus
rustdoc: respect alternate flag when formatting impl trait Fixes rust-lang#58226. Before: <img width="963" alt="screen shot 2019-02-12 at 3 23 30 pm" src="https://user-images.githubusercontent.com/1372438/52665732-4496ea00-2eda-11e9-9e29-efffe43b2abf.png"> After: <img width="964" alt="screen shot 2019-02-12 at 3 23 51 pm" src="https://user-images.githubusercontent.com/1372438/52665733-452f8080-2eda-11e9-999a-dd1fb28dee16.png">
2 parents 98a6e72 + e827c9a commit 8d1e432

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

src/librustdoc/html/format.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ pub struct AbiSpace(pub Abi);
4949
pub struct Function<'a> {
5050
/// The declaration to emit.
5151
pub decl: &'a clean::FnDecl,
52-
/// The length of the function's "name", used to determine line-wrapping.
53-
pub name_len: usize,
52+
/// The length of the function header and name. In other words, the number of characters in the
53+
/// function declaration up to but not including the parentheses.
54+
///
55+
/// Used to determine line-wrapping.
56+
pub header_len: usize,
5457
/// The number of spaces to indent each successive line with, if line-wrapping is necessary.
5558
pub indent: usize,
5659
/// Whether the function is async or not.
@@ -675,7 +678,11 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
675678
}
676679
}
677680
clean::ImplTrait(ref bounds) => {
678-
write!(f, "impl {}", GenericBounds(bounds))
681+
if f.alternate() {
682+
write!(f, "impl {:#}", GenericBounds(bounds))
683+
} else {
684+
write!(f, "impl {}", GenericBounds(bounds))
685+
}
679686
}
680687
clean::QPath { ref name, ref self_type, ref trait_ } => {
681688
let should_show_cast = match *trait_ {
@@ -844,7 +851,7 @@ impl fmt::Display for clean::FnDecl {
844851

845852
impl<'a> fmt::Display for Function<'a> {
846853
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
847-
let &Function { decl, name_len, indent, asyncness } = self;
854+
let &Function { decl, header_len, indent, asyncness } = self;
848855
let amp = if f.alternate() { "&" } else { "&amp;" };
849856
let mut args = String::new();
850857
let mut args_plain = String::new();
@@ -899,6 +906,8 @@ impl<'a> fmt::Display for Function<'a> {
899906
}
900907
}
901908

909+
let mut args_plain = format!("({})", args_plain);
910+
902911
if decl.variadic {
903912
args.push_str(",<br> ...");
904913
args_plain.push_str(", ...");
@@ -917,13 +926,8 @@ impl<'a> fmt::Display for Function<'a> {
917926
output.to_string()
918927
};
919928

920-
let pad = " ".repeat(name_len);
921-
let plain = format!("{pad}({args}){arrow}",
922-
pad = pad,
923-
args = args_plain,
924-
arrow = arrow_plain);
925-
926-
let output = if plain.len() > 80 {
929+
let declaration_len = header_len + args_plain.len() + arrow_plain.len();
930+
let output = if declaration_len > 80 {
927931
let full_pad = format!("<br>{}", "&nbsp;".repeat(indent + 4));
928932
let close_pad = format!("<br>{}", "&nbsp;".repeat(indent));
929933
format!("({args}{close}){arrow}",

src/librustdoc/html/render.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2984,14 +2984,16 @@ fn item_static(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
29842984

29852985
fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
29862986
f: &clean::Function) -> fmt::Result {
2987-
let name_len = format!("{}{}{}{}{:#}fn {}{:#}",
2988-
VisSpace(&it.visibility),
2989-
ConstnessSpace(f.header.constness),
2990-
UnsafetySpace(f.header.unsafety),
2991-
AsyncSpace(f.header.asyncness),
2992-
AbiSpace(f.header.abi),
2993-
it.name.as_ref().unwrap(),
2994-
f.generics).len();
2987+
let header_len = format!(
2988+
"{}{}{}{}{:#}fn {}{:#}",
2989+
VisSpace(&it.visibility),
2990+
ConstnessSpace(f.header.constness),
2991+
UnsafetySpace(f.header.unsafety),
2992+
AsyncSpace(f.header.asyncness),
2993+
AbiSpace(f.header.abi),
2994+
it.name.as_ref().unwrap(),
2995+
f.generics
2996+
).len();
29952997
write!(w, "{}<pre class='rust fn'>", render_spotlight_traits(it)?)?;
29962998
render_attributes(w, it)?;
29972999
write!(w,
@@ -3007,7 +3009,7 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
30073009
where_clause = WhereClause { gens: &f.generics, indent: 0, end_newline: true },
30083010
decl = Function {
30093011
decl: &f.decl,
3010-
name_len,
3012+
header_len,
30113013
indent: 0,
30123014
asyncness: f.header.asyncness,
30133015
})?;
@@ -3422,16 +3424,18 @@ fn render_assoc_item(w: &mut fmt::Formatter,
34223424
href(did).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor)
34233425
}
34243426
};
3425-
let mut head_len = format!("{}{}{}{}{:#}fn {}{:#}",
3426-
VisSpace(&meth.visibility),
3427-
ConstnessSpace(header.constness),
3428-
UnsafetySpace(header.unsafety),
3429-
AsyncSpace(header.asyncness),
3430-
AbiSpace(header.abi),
3431-
name,
3432-
*g).len();
3427+
let mut header_len = format!(
3428+
"{}{}{}{}{:#}fn {}{:#}",
3429+
VisSpace(&meth.visibility),
3430+
ConstnessSpace(header.constness),
3431+
UnsafetySpace(header.unsafety),
3432+
AsyncSpace(header.asyncness),
3433+
AbiSpace(header.abi),
3434+
name,
3435+
*g
3436+
).len();
34333437
let (indent, end_newline) = if parent == ItemType::Trait {
3434-
head_len += 4;
3438+
header_len += 4;
34353439
(4, false)
34363440
} else {
34373441
(0, true)
@@ -3449,7 +3453,7 @@ fn render_assoc_item(w: &mut fmt::Formatter,
34493453
generics = *g,
34503454
decl = Function {
34513455
decl: d,
3452-
name_len: head_len,
3456+
header_len,
34533457
indent,
34543458
asyncness: header.asyncness,
34553459
},

src/test/rustdoc/wrapping.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use std::fmt::Debug;
2+
3+
// @has 'wrapping/fn.foo.html' '//pre[@class="rust fn"]' 'pub fn foo() -> impl Debug'
4+
// @count - '//pre[@class="rust fn"]/br' 0
5+
pub fn foo() -> impl Debug {}

0 commit comments

Comments
 (0)