Skip to content

Commit c748551

Browse files
Fix invalid add of whitespace when there is where clause
1 parent 53792b9 commit c748551

File tree

2 files changed

+45
-14
lines changed

2 files changed

+45
-14
lines changed

src/librustdoc/html/format.rs

+4
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ impl Buffer {
146146
pub(crate) fn reserve(&mut self, additional: usize) {
147147
self.buffer.reserve(additional)
148148
}
149+
150+
pub(crate) fn len(&self) -> usize {
151+
self.buffer.len()
152+
}
149153
}
150154

151155
fn comma_sep<T: fmt::Display>(

src/librustdoc/html/render/print_item.rs

+41-14
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ struct ItemVars<'a> {
6262
src_href: Option<&'a str>,
6363
}
6464

65+
/// Calls `print_where_clause` and returns `true` if a `where` clause was generated.
66+
fn print_where_clause_and_check<'a, 'tcx: 'a>(
67+
buffer: &mut Buffer,
68+
gens: &'a clean::Generics,
69+
cx: &'a Context<'tcx>,
70+
) -> bool {
71+
let len_before = buffer.len();
72+
write!(buffer, "{}", print_where_clause(gens, cx, 0, true));
73+
len_before != buffer.len()
74+
}
75+
6576
pub(super) fn print_item(
6677
cx: &mut Context<'_>,
6778
item: &clean::Item,
@@ -1152,17 +1163,21 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
11521163
render_attributes_in_pre(w, it, "");
11531164
write!(
11541165
w,
1155-
"{}enum {}{}{}",
1166+
"{}enum {}{}",
11561167
it.visibility.print_with_space(it.item_id, cx),
11571168
it.name.unwrap(),
11581169
e.generics.print(cx),
1159-
print_where_clause(&e.generics, cx, 0, true),
11601170
);
1171+
if !print_where_clause_and_check(w, &e.generics, cx) {
1172+
// If there wasn't a `where` clause, we add a whitespace.
1173+
w.write_str(" ");
1174+
}
1175+
11611176
let variants_stripped = e.has_stripped_entries();
11621177
if count_variants == 0 && !variants_stripped {
1163-
w.write_str(" {}");
1178+
w.write_str("{}");
11641179
} else {
1165-
w.write_str(" {\n");
1180+
w.write_str("{\n");
11661181
let toggle = should_hide_fields(count_variants);
11671182
if toggle {
11681183
toggle_open(w, format_args!("{} variants", count_variants));
@@ -1643,13 +1658,21 @@ fn render_union(
16431658
tab: &str,
16441659
cx: &Context<'_>,
16451660
) {
1646-
write!(w, "{}union {}", it.visibility.print_with_space(it.item_id, cx), it.name.unwrap());
1647-
if let Some(g) = g {
1648-
write!(w, "{}", g.print(cx));
1649-
write!(w, "{}", print_where_clause(g, cx, 0, true));
1661+
write!(w, "{}union {}", it.visibility.print_with_space(it.item_id, cx), it.name.unwrap(),);
1662+
1663+
let where_displayed = g
1664+
.map(|g| {
1665+
write!(w, "{}", g.print(cx));
1666+
print_where_clause_and_check(w, g, cx)
1667+
})
1668+
.unwrap_or(false);
1669+
1670+
// If there wasn't a `where` clause, we add a whitespace.
1671+
if !where_displayed {
1672+
w.write_str(" ");
16501673
}
16511674

1652-
write!(w, " {{\n{}", tab);
1675+
write!(w, "{{\n{}", tab);
16531676
let count_fields =
16541677
fields.iter().filter(|f| matches!(*f.kind, clean::StructFieldItem(..))).count();
16551678
let toggle = should_hide_fields(count_fields);
@@ -1701,10 +1724,14 @@ fn render_struct(
17011724
}
17021725
match ty {
17031726
CtorKind::Fictive => {
1704-
if let Some(g) = g {
1705-
write!(w, "{}", print_where_clause(g, cx, 0, true),)
1727+
let where_diplayed = g.map(|g| print_where_clause_and_check(w, g, cx)).unwrap_or(false);
1728+
1729+
// If there wasn't a `where` clause, we add a whitespace.
1730+
if !where_diplayed {
1731+
w.write_str(" {");
1732+
} else {
1733+
w.write_str("{");
17061734
}
1707-
w.write_str(" {");
17081735
let count_fields =
17091736
fields.iter().filter(|f| matches!(*f.kind, clean::StructFieldItem(..))).count();
17101737
let has_visible_fields = count_fields > 0;
@@ -1759,7 +1786,7 @@ fn render_struct(
17591786
}
17601787
w.write_str(")");
17611788
if let Some(g) = g {
1762-
write!(w, "{}", print_where_clause(g, cx, 0, false),)
1789+
write!(w, "{}", print_where_clause(g, cx, 0, false));
17631790
}
17641791
// We only want a ";" when we are displaying a tuple struct, not a variant tuple struct.
17651792
if structhead {
@@ -1769,7 +1796,7 @@ fn render_struct(
17691796
CtorKind::Const => {
17701797
// Needed for PhantomData.
17711798
if let Some(g) = g {
1772-
write!(w, "{}", print_where_clause(g, cx, 0, false),)
1799+
write!(w, "{}", print_where_clause(g, cx, 0, false));
17731800
}
17741801
w.write_str(";");
17751802
}

0 commit comments

Comments
 (0)