Skip to content

Commit 8bd2182

Browse files
committed
Bless rustdoc test
1 parent 2114ec8 commit 8bd2182

File tree

2 files changed

+83
-85
lines changed

2 files changed

+83
-85
lines changed

src/librustdoc/html/format.rs

+82-84
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,14 @@ impl Buffer {
126126
}
127127
}
128128

129-
fn comma_sep<T: fmt::Display>(items: impl Iterator<Item = T>) -> impl fmt::Display {
129+
fn comma_sep<T: fmt::Display>(
130+
items: impl Iterator<Item = T>,
131+
space_after_comma: bool,
132+
) -> impl fmt::Display {
130133
display_fn(move |f| {
131134
for (i, item) in items.enumerate() {
132135
if i != 0 {
133-
write!(f, ", ")?;
136+
write!(f, ",{}", if space_after_comma { " " } else { "" })?;
134137
}
135138
fmt::Display::fmt(&item, f)?;
136139
}
@@ -231,9 +234,9 @@ impl clean::Generics {
231234
}
232235

233236
if f.alternate() {
234-
write!(f, "<{:#}>", comma_sep(real_params.map(|g| g.print(cx))))
237+
write!(f, "<{:#}>", comma_sep(real_params.map(|g| g.print(cx)), true))
235238
} else {
236-
write!(f, "&lt;{}&gt;", comma_sep(real_params.map(|g| g.print(cx))))
239+
write!(f, "&lt;{}&gt;", comma_sep(real_params.map(|g| g.print(cx)), true))
237240
}
238241
})
239242
}
@@ -249,10 +252,80 @@ crate fn print_where_clause<'a, 'tcx: 'a>(
249252
end_newline: bool,
250253
) -> impl fmt::Display + 'a + Captures<'tcx> {
251254
display_fn(move |f| {
252-
if gens.where_predicates.is_empty() {
255+
let mut where_predicates = gens.where_predicates.iter().filter(|pred| {
256+
!matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty())
257+
}).map(|pred| {
258+
display_fn(move |f| {
259+
if f.alternate() {
260+
f.write_str(" ")?;
261+
} else {
262+
f.write_str("<br>")?;
263+
}
264+
265+
match pred {
266+
clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => {
267+
let bounds = bounds;
268+
let for_prefix = match bound_params.len() {
269+
0 => String::new(),
270+
_ if f.alternate() => {
271+
format!(
272+
"for&lt;{:#}&gt; ",
273+
comma_sep(bound_params.iter().map(|lt| lt.print()), true)
274+
)
275+
}
276+
_ => format!(
277+
"for&lt;{}&gt; ",
278+
comma_sep(bound_params.iter().map(|lt| lt.print()), true)
279+
),
280+
};
281+
282+
if f.alternate() {
283+
write!(
284+
f,
285+
"{}{:#}: {:#}",
286+
for_prefix,
287+
ty.print(cx),
288+
print_generic_bounds(bounds, cx)
289+
)
290+
} else {
291+
write!(
292+
f,
293+
"{}{}: {}",
294+
for_prefix,
295+
ty.print(cx),
296+
print_generic_bounds(bounds, cx)
297+
)
298+
}
299+
}
300+
clean::WherePredicate::RegionPredicate { lifetime, bounds } => {
301+
write!(
302+
f,
303+
"{}: {}",
304+
lifetime.print(),
305+
bounds
306+
.iter()
307+
.map(|b| b.print(cx).to_string())
308+
.collect::<Vec<_>>()
309+
.join(" + ")
310+
)
311+
}
312+
clean::WherePredicate::EqPredicate { lhs, rhs } => {
313+
if f.alternate() {
314+
write!(f, "{:#} == {:#}", lhs.print(cx), rhs.print(cx),)
315+
} else {
316+
write!(f, "{} == {}", lhs.print(cx), rhs.print(cx),)
317+
}
318+
}
319+
}
320+
})
321+
}).peekable();
322+
323+
if where_predicates.peek().is_none() {
253324
return Ok(());
254325
}
326+
255327
let mut clause = String::new();
328+
256329
if f.alternate() {
257330
clause.push_str(" where");
258331
} else {
@@ -263,82 +336,7 @@ crate fn print_where_clause<'a, 'tcx: 'a>(
263336
}
264337
}
265338

266-
#[derive(Clone, Copy)]
267-
enum Print<'a> {
268-
Predicate(&'a clean::WherePredicate),
269-
Comma,
270-
}
271-
272-
for pred in gens.where_predicates.iter().filter(|pred| {
273-
!matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty())
274-
}).map(Print::Predicate).intersperse(Print::Comma) {
275-
let pred = match pred {
276-
Print::Predicate(pred) => pred,
277-
Print::Comma => {
278-
clause.push(',');
279-
continue;
280-
}
281-
};
282-
283-
if f.alternate() {
284-
clause.push(' ');
285-
} else {
286-
clause.push_str("<br>");
287-
}
288-
289-
match pred {
290-
clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => {
291-
let bounds = bounds;
292-
let for_prefix = match bound_params.len() {
293-
0 => String::new(),
294-
_ if f.alternate() => {
295-
format!(
296-
"for&lt;{:#}&gt; ",
297-
comma_sep(bound_params.iter().map(|lt| lt.print()))
298-
)
299-
}
300-
_ => format!(
301-
"for&lt;{}&gt; ",
302-
comma_sep(bound_params.iter().map(|lt| lt.print()))
303-
),
304-
};
305-
306-
if f.alternate() {
307-
clause.push_str(&format!(
308-
"{}{:#}: {:#}",
309-
for_prefix,
310-
ty.print(cx),
311-
print_generic_bounds(bounds, cx)
312-
));
313-
} else {
314-
clause.push_str(&format!(
315-
"{}{}: {}",
316-
for_prefix,
317-
ty.print(cx),
318-
print_generic_bounds(bounds, cx)
319-
));
320-
}
321-
}
322-
clean::WherePredicate::RegionPredicate { lifetime, bounds } => {
323-
clause.push_str(&format!(
324-
"{}: {}",
325-
lifetime.print(),
326-
bounds
327-
.iter()
328-
.map(|b| b.print(cx).to_string())
329-
.collect::<Vec<_>>()
330-
.join(" + ")
331-
));
332-
}
333-
clean::WherePredicate::EqPredicate { lhs, rhs } => {
334-
if f.alternate() {
335-
clause.push_str(&format!("{:#} == {:#}", lhs.print(cx), rhs.print(cx),));
336-
} else {
337-
clause.push_str(&format!("{} == {}", lhs.print(cx), rhs.print(cx),));
338-
}
339-
}
340-
}
341-
}
339+
clause.push_str(&comma_sep(where_predicates, false).to_string());
342340

343341
if end_newline {
344342
clause.push(',');
@@ -391,13 +389,13 @@ impl clean::PolyTrait {
391389
write!(
392390
f,
393391
"for<{:#}> ",
394-
comma_sep(self.generic_params.iter().map(|g| g.print(cx)))
392+
comma_sep(self.generic_params.iter().map(|g| g.print(cx)), true)
395393
)?;
396394
} else {
397395
write!(
398396
f,
399397
"for&lt;{}&gt; ",
400-
comma_sep(self.generic_params.iter().map(|g| g.print(cx)))
398+
comma_sep(self.generic_params.iter().map(|g| g.print(cx)), true)
401399
)?;
402400
}
403401
}
@@ -1108,7 +1106,7 @@ impl clean::BareFunctionDecl {
11081106
write!(
11091107
f,
11101108
"for&lt;{}&gt; ",
1111-
comma_sep(self.generic_params.iter().map(|g| g.print(cx)))
1109+
comma_sep(self.generic_params.iter().map(|g| g.print(cx)), true)
11121110
)
11131111
} else {
11141112
Ok(())

src/test/rustdoc/const-generics/generic_const_exprs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
#![allow(incomplete_features)]
44
// make sure that `ConstEvaluatable` predicates dont cause rustdoc to ICE #77647
55
// @has foo/struct.Ice.html '//pre[@class="rust struct"]' \
6-
// 'pub struct Ice<const N: usize> where [(); N + 1]: ;'
6+
// 'pub struct Ice<const N: usize>;'
77
pub struct Ice<const N: usize> where [(); N + 1]:;

0 commit comments

Comments
 (0)