Skip to content

Commit e827c9a

Browse files
committed
respect alternate flag when formatting impl trait
1 parent 57d7cfc commit e827c9a

File tree

3 files changed

+43
-30
lines changed

3 files changed

+43
-30
lines changed

src/librustdoc/html/format.rs

+15-11
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.
@@ -665,7 +668,11 @@ fn fmt_type(t: &clean::Type, f: &mut fmt::Formatter, use_absolute: bool) -> fmt:
665668
}
666669
}
667670
clean::ImplTrait(ref bounds) => {
668-
write!(f, "impl {}", GenericBounds(bounds))
671+
if f.alternate() {
672+
write!(f, "impl {:#}", GenericBounds(bounds))
673+
} else {
674+
write!(f, "impl {}", GenericBounds(bounds))
675+
}
669676
}
670677
clean::QPath { ref name, ref self_type, ref trait_ } => {
671678
let should_show_cast = match *trait_ {
@@ -834,7 +841,7 @@ impl fmt::Display for clean::FnDecl {
834841

835842
impl<'a> fmt::Display for Function<'a> {
836843
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
837-
let &Function { decl, name_len, indent, asyncness } = self;
844+
let &Function { decl, header_len, indent, asyncness } = self;
838845
let amp = if f.alternate() { "&" } else { "&amp;" };
839846
let mut args = String::new();
840847
let mut args_plain = String::new();
@@ -889,6 +896,8 @@ impl<'a> fmt::Display for Function<'a> {
889896
}
890897
}
891898

899+
let mut args_plain = format!("({})", args_plain);
900+
892901
if decl.variadic {
893902
args.push_str(",<br> ...");
894903
args_plain.push_str(", ...");
@@ -907,13 +916,8 @@ impl<'a> fmt::Display for Function<'a> {
907916
output.to_string()
908917
};
909918

910-
let pad = " ".repeat(name_len);
911-
let plain = format!("{pad}({args}){arrow}",
912-
pad = pad,
913-
args = args_plain,
914-
arrow = arrow_plain);
915-
916-
let output = if plain.len() > 80 {
919+
let declaration_len = header_len + args_plain.len() + arrow_plain.len();
920+
let output = if declaration_len > 80 {
917921
let full_pad = format!("<br>{}", "&nbsp;".repeat(indent + 4));
918922
let close_pad = format!("<br>{}", "&nbsp;".repeat(indent));
919923
format!("({args}{close}){arrow}",

src/librustdoc/html/render.rs

+23-19
Original file line numberDiff line numberDiff line change
@@ -2962,14 +2962,16 @@ fn item_static(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
29622962

29632963
fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
29642964
f: &clean::Function) -> fmt::Result {
2965-
let name_len = format!("{}{}{}{}{:#}fn {}{:#}",
2966-
VisSpace(&it.visibility),
2967-
ConstnessSpace(f.header.constness),
2968-
UnsafetySpace(f.header.unsafety),
2969-
AsyncSpace(f.header.asyncness),
2970-
AbiSpace(f.header.abi),
2971-
it.name.as_ref().unwrap(),
2972-
f.generics).len();
2965+
let header_len = format!(
2966+
"{}{}{}{}{:#}fn {}{:#}",
2967+
VisSpace(&it.visibility),
2968+
ConstnessSpace(f.header.constness),
2969+
UnsafetySpace(f.header.unsafety),
2970+
AsyncSpace(f.header.asyncness),
2971+
AbiSpace(f.header.abi),
2972+
it.name.as_ref().unwrap(),
2973+
f.generics
2974+
).len();
29732975
write!(w, "{}<pre class='rust fn'>", render_spotlight_traits(it)?)?;
29742976
render_attributes(w, it)?;
29752977
write!(w,
@@ -2985,7 +2987,7 @@ fn item_function(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
29852987
where_clause = WhereClause { gens: &f.generics, indent: 0, end_newline: true },
29862988
decl = Function {
29872989
decl: &f.decl,
2988-
name_len,
2990+
header_len,
29892991
indent: 0,
29902992
asyncness: f.header.asyncness,
29912993
})?;
@@ -3400,16 +3402,18 @@ fn render_assoc_item(w: &mut fmt::Formatter,
34003402
href(did).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor)
34013403
}
34023404
};
3403-
let mut head_len = format!("{}{}{}{}{:#}fn {}{:#}",
3404-
VisSpace(&meth.visibility),
3405-
ConstnessSpace(header.constness),
3406-
UnsafetySpace(header.unsafety),
3407-
AsyncSpace(header.asyncness),
3408-
AbiSpace(header.abi),
3409-
name,
3410-
*g).len();
3405+
let mut header_len = format!(
3406+
"{}{}{}{}{:#}fn {}{:#}",
3407+
VisSpace(&meth.visibility),
3408+
ConstnessSpace(header.constness),
3409+
UnsafetySpace(header.unsafety),
3410+
AsyncSpace(header.asyncness),
3411+
AbiSpace(header.abi),
3412+
name,
3413+
*g
3414+
).len();
34113415
let (indent, end_newline) = if parent == ItemType::Trait {
3412-
head_len += 4;
3416+
header_len += 4;
34133417
(4, false)
34143418
} else {
34153419
(0, true)
@@ -3427,7 +3431,7 @@ fn render_assoc_item(w: &mut fmt::Formatter,
34273431
generics = *g,
34283432
decl = Function {
34293433
decl: d,
3430-
name_len: head_len,
3434+
header_len,
34313435
indent,
34323436
asyncness: header.asyncness,
34333437
},

src/test/rustdoc/wrapping.rs

+5
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)