Skip to content

Commit 9c0141a

Browse files
committed
Bless rustdoc test
1 parent 5406cbf commit 9c0141a

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
@@ -143,11 +143,14 @@ impl Buffer {
143143
}
144144
}
145145

146-
fn comma_sep<T: fmt::Display>(items: impl Iterator<Item = T>) -> impl fmt::Display {
146+
fn comma_sep<T: fmt::Display>(
147+
items: impl Iterator<Item = T>,
148+
space_after_comma: bool,
149+
) -> impl fmt::Display {
147150
display_fn(move |f| {
148151
for (i, item) in items.enumerate() {
149152
if i != 0 {
150-
write!(f, ", ")?;
153+
write!(f, ",{}", if space_after_comma { " " } else { "" })?;
151154
}
152155
fmt::Display::fmt(&item, f)?;
153156
}
@@ -248,9 +251,9 @@ impl clean::Generics {
248251
}
249252

250253
if f.alternate() {
251-
write!(f, "<{:#}>", comma_sep(real_params.map(|g| g.print(cx))))
254+
write!(f, "<{:#}>", comma_sep(real_params.map(|g| g.print(cx)), true))
252255
} else {
253-
write!(f, "&lt;{}&gt;", comma_sep(real_params.map(|g| g.print(cx))))
256+
write!(f, "&lt;{}&gt;", comma_sep(real_params.map(|g| g.print(cx)), true))
254257
}
255258
})
256259
}
@@ -266,10 +269,80 @@ crate fn print_where_clause<'a, 'tcx: 'a>(
266269
end_newline: bool,
267270
) -> impl fmt::Display + 'a + Captures<'tcx> {
268271
display_fn(move |f| {
269-
if gens.where_predicates.is_empty() {
272+
let mut where_predicates = gens.where_predicates.iter().filter(|pred| {
273+
!matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty())
274+
}).map(|pred| {
275+
display_fn(move |f| {
276+
if f.alternate() {
277+
f.write_str(" ")?;
278+
} else {
279+
f.write_str("<br>")?;
280+
}
281+
282+
match pred {
283+
clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => {
284+
let bounds = bounds;
285+
let for_prefix = match bound_params.len() {
286+
0 => String::new(),
287+
_ if f.alternate() => {
288+
format!(
289+
"for&lt;{:#}&gt; ",
290+
comma_sep(bound_params.iter().map(|lt| lt.print()), true)
291+
)
292+
}
293+
_ => format!(
294+
"for&lt;{}&gt; ",
295+
comma_sep(bound_params.iter().map(|lt| lt.print()), true)
296+
),
297+
};
298+
299+
if f.alternate() {
300+
write!(
301+
f,
302+
"{}{:#}: {:#}",
303+
for_prefix,
304+
ty.print(cx),
305+
print_generic_bounds(bounds, cx)
306+
)
307+
} else {
308+
write!(
309+
f,
310+
"{}{}: {}",
311+
for_prefix,
312+
ty.print(cx),
313+
print_generic_bounds(bounds, cx)
314+
)
315+
}
316+
}
317+
clean::WherePredicate::RegionPredicate { lifetime, bounds } => {
318+
write!(
319+
f,
320+
"{}: {}",
321+
lifetime.print(),
322+
bounds
323+
.iter()
324+
.map(|b| b.print(cx).to_string())
325+
.collect::<Vec<_>>()
326+
.join(" + ")
327+
)
328+
}
329+
clean::WherePredicate::EqPredicate { lhs, rhs } => {
330+
if f.alternate() {
331+
write!(f, "{:#} == {:#}", lhs.print(cx), rhs.print(cx),)
332+
} else {
333+
write!(f, "{} == {}", lhs.print(cx), rhs.print(cx),)
334+
}
335+
}
336+
}
337+
})
338+
}).peekable();
339+
340+
if where_predicates.peek().is_none() {
270341
return Ok(());
271342
}
343+
272344
let mut clause = String::new();
345+
273346
if f.alternate() {
274347
clause.push_str(" where");
275348
} else {
@@ -280,82 +353,7 @@ crate fn print_where_clause<'a, 'tcx: 'a>(
280353
}
281354
}
282355

283-
#[derive(Clone, Copy)]
284-
enum Print<'a> {
285-
Predicate(&'a clean::WherePredicate),
286-
Comma,
287-
}
288-
289-
for pred in gens.where_predicates.iter().filter(|pred| {
290-
!matches!(pred, clean::WherePredicate::BoundPredicate { bounds, .. } if bounds.is_empty())
291-
}).map(Print::Predicate).intersperse(Print::Comma) {
292-
let pred = match pred {
293-
Print::Predicate(pred) => pred,
294-
Print::Comma => {
295-
clause.push(',');
296-
continue;
297-
}
298-
};
299-
300-
if f.alternate() {
301-
clause.push(' ');
302-
} else {
303-
clause.push_str("<br>");
304-
}
305-
306-
match pred {
307-
clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => {
308-
let bounds = bounds;
309-
let for_prefix = match bound_params.len() {
310-
0 => String::new(),
311-
_ if f.alternate() => {
312-
format!(
313-
"for&lt;{:#}&gt; ",
314-
comma_sep(bound_params.iter().map(|lt| lt.print()))
315-
)
316-
}
317-
_ => format!(
318-
"for&lt;{}&gt; ",
319-
comma_sep(bound_params.iter().map(|lt| lt.print()))
320-
),
321-
};
322-
323-
if f.alternate() {
324-
clause.push_str(&format!(
325-
"{}{:#}: {:#}",
326-
for_prefix,
327-
ty.print(cx),
328-
print_generic_bounds(bounds, cx)
329-
));
330-
} else {
331-
clause.push_str(&format!(
332-
"{}{}: {}",
333-
for_prefix,
334-
ty.print(cx),
335-
print_generic_bounds(bounds, cx)
336-
));
337-
}
338-
}
339-
clean::WherePredicate::RegionPredicate { lifetime, bounds } => {
340-
clause.push_str(&format!(
341-
"{}: {}",
342-
lifetime.print(),
343-
bounds
344-
.iter()
345-
.map(|b| b.print(cx).to_string())
346-
.collect::<Vec<_>>()
347-
.join(" + ")
348-
));
349-
}
350-
clean::WherePredicate::EqPredicate { lhs, rhs } => {
351-
if f.alternate() {
352-
clause.push_str(&format!("{:#} == {:#}", lhs.print(cx), rhs.print(cx),));
353-
} else {
354-
clause.push_str(&format!("{} == {}", lhs.print(cx), rhs.print(cx),));
355-
}
356-
}
357-
}
358-
}
356+
clause.push_str(&comma_sep(where_predicates, false).to_string());
359357

360358
if end_newline {
361359
clause.push(',');
@@ -408,13 +406,13 @@ impl clean::PolyTrait {
408406
write!(
409407
f,
410408
"for<{:#}> ",
411-
comma_sep(self.generic_params.iter().map(|g| g.print(cx)))
409+
comma_sep(self.generic_params.iter().map(|g| g.print(cx)), true)
412410
)?;
413411
} else {
414412
write!(
415413
f,
416414
"for&lt;{}&gt; ",
417-
comma_sep(self.generic_params.iter().map(|g| g.print(cx)))
415+
comma_sep(self.generic_params.iter().map(|g| g.print(cx)), true)
418416
)?;
419417
}
420418
}
@@ -1125,7 +1123,7 @@ impl clean::BareFunctionDecl {
11251123
write!(
11261124
f,
11271125
"for&lt;{}&gt; ",
1128-
comma_sep(self.generic_params.iter().map(|g| g.print(cx)))
1126+
comma_sep(self.generic_params.iter().map(|g| g.print(cx)), true)
11291127
)
11301128
} else {
11311129
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)