Skip to content

rustdoc: Break bounds if they are too long #107298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 41 additions & 15 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,14 @@ pub(crate) fn comma_sep<T: fmt::Display>(
pub(crate) fn print_generic_bounds<'a, 'tcx: 'a>(
bounds: &'a [clean::GenericBound],
cx: &'a Context<'tcx>,
separator: &'static str,
) -> impl fmt::Display + 'a + Captures<'tcx> {
display_fn(move |f| {
let mut bounds_dup = FxHashSet::default();

for (i, bound) in bounds.iter().filter(|b| bounds_dup.insert(b.clone())).enumerate() {
if i > 0 {
f.write_str(" + ")?;
f.write_str(separator)?;
}
fmt::Display::fmt(&bound.print(cx), f)?;
}
Expand Down Expand Up @@ -206,9 +207,9 @@ impl clean::GenericParamDef {

if !bounds.is_empty() {
if f.alternate() {
write!(f, ": {:#}", print_generic_bounds(bounds, cx))?;
write!(f, ": {:#}", print_generic_bounds(bounds, cx, " + "))?;
} else {
write!(f, ":&nbsp;{}", print_generic_bounds(bounds, cx))?;
write!(f, ":&nbsp;{}", print_generic_bounds(bounds, cx, " + "))?;
}
}

Expand Down Expand Up @@ -295,7 +296,7 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
match pred {
clean::WherePredicate::BoundPredicate { ty, bounds, bound_params } => {
let ty_cx = ty.print(cx);
let generic_bounds = print_generic_bounds(bounds, cx);
let generic_bounds = print_generic_bounds(bounds, cx, " ++ ");

if bound_params.is_empty() {
if f.alternate() {
Expand All @@ -320,12 +321,8 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
}
}
clean::WherePredicate::RegionPredicate { lifetime, bounds } => {
let mut bounds_display = String::new();
for bound in bounds.iter().map(|b| b.print(cx)) {
write!(bounds_display, "{bound} + ")?;
}
bounds_display.truncate(bounds_display.len() - " + ".len());
write!(f, "{}: {bounds_display}", lifetime.print())
let generic_bounds = print_generic_bounds(bounds, cx, " ++ ");
write!(f, "{}: {generic_bounds}", lifetime.print())
}
// FIXME(fmease): Render bound params.
clean::WherePredicate::EqPredicate { lhs, rhs, bound_params: _ } => {
Expand Down Expand Up @@ -356,7 +353,36 @@ pub(crate) fn print_where_clause<'a, 'tcx: 'a>(
for _ in 0..indent + 4 {
br_with_padding.push_str("&nbsp;");
}
let where_preds = where_preds.to_string().replace("<br>", &br_with_padding);
let where_preds = where_preds.to_string().split("<br>").into_iter().enumerate().fold(
String::new(),
|mut acc, (pos, pred)| {
if pos > 0 {
acc.push_str(&br_with_padding);
}
if pred.split(" ++ ").count() > 4 {
// If we have more than 4 bounds, we put one by line instead.
let mut len = 0;
for (pos, part) in pred.split(" ++ ").enumerate() {
len += part.len();
if len > 80 {
acc.push_str(&br_with_padding);
len = part.len();
}
if pos > 0 {
acc.push_str(" + ");
}
acc.push_str(&part);
}
} else {
if pred.contains(" ++ ") {
acc.push_str(&pred.replace(" ++ ", " + "));
} else {
acc.push_str(&pred);
}
}
acc
},
);

if ending == Ending::Newline {
let mut clause = "&nbsp;".repeat(indent.saturating_sub(1));
Expand Down Expand Up @@ -1081,9 +1107,9 @@ fn fmt_type<'cx>(
}
clean::ImplTrait(ref bounds) => {
if f.alternate() {
write!(f, "impl {:#}", print_generic_bounds(bounds, cx))
write!(f, "impl {:#}", print_generic_bounds(bounds, cx, " + "))
} else {
write!(f, "impl {}", print_generic_bounds(bounds, cx))
write!(f, "impl {}", print_generic_bounds(bounds, cx, " + "))
}
}
clean::QPath(box clean::QPathData {
Expand Down Expand Up @@ -1615,9 +1641,9 @@ impl clean::TypeBinding {
clean::TypeBindingKind::Constraint { ref bounds } => {
if !bounds.is_empty() {
if f.alternate() {
write!(f, ": {:#}", print_generic_bounds(bounds, cx))?;
write!(f, ": {:#}", print_generic_bounds(bounds, cx, " + "))?;
} else {
write!(f, ":&nbsp;{}", print_generic_bounds(bounds, cx))?;
write!(f, ":&nbsp;{}", print_generic_bounds(bounds, cx, " + "))?;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ fn assoc_type(
generics = generics.print(cx),
);
if !bounds.is_empty() {
write!(w, ": {}", print_generic_bounds(bounds, cx))
write!(w, ": {}", print_generic_bounds(bounds, cx, " + "))
}
write!(w, "{}", print_where_clause(generics, cx, indent, Ending::NoNewline));
if let Some(default) = default {
Expand Down
4 changes: 4 additions & 0 deletions tests/rustdoc/lot-of-bounds.bounds.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<code>pub trait Fixed<span class="where fmt-newline">where<br />&#160;&#160;&#160;&#160;<br />&#160;&#160;&#160;&#160;Self: <a class="trait" href="{{channel}}/core/default/trait.Default.html" title="trait core::default::Default">Default</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/hash/trait.Hash.html" title="trait core::hash::Hash">Hash</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.Ord.html" title="trait core::cmp::Ord">Ord</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/fmt/trait.Debug.html" title="trait core::fmt::Debug">Debug</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/fmt/trait.Display.html" title="trait core::fmt::Display">Display</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/fmt/trait.LowerExp.html" title="trait core::fmt::LowerExp">LowerExp</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/fmt/trait.UpperExp.html" title="trait core::fmt::UpperExp">UpperExp</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/fmt/trait.Binary.html" title="trait core::fmt::Binary">Binary</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/fmt/trait.Octal.html" title="trait core::fmt::Octal">Octal</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/fmt/trait.LowerHex.html" title="trait core::fmt::LowerHex">LowerHex</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/fmt/trait.UpperHex.html" title="trait core::fmt::UpperHex">UpperHex</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.Add.html" title="trait core::ops::arith::Add">Add</a>&lt;Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.AddAssign.html" title="trait core::ops::arith::AddAssign">AddAssign</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.Sub.html" title="trait core::ops::arith::Sub">Sub</a>&lt;Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.SubAssign.html" title="trait core::ops::arith::SubAssign">SubAssign</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.Mul.html" title="trait core::ops::arith::Mul">Mul</a>&lt;Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.MulAssign.html" title="trait core::ops::arith::MulAssign">MulAssign</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.Div.html" title="trait core::ops::arith::Div">Div</a>&lt;Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.DivAssign.html" title="trait core::ops::arith::DivAssign">DivAssign</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.Rem.html" title="trait core::ops::arith::Rem">Rem</a>&lt;Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.RemAssign.html" title="trait core::ops::arith::RemAssign">RemAssign</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.Mul.html" title="trait core::ops::arith::Mul">Mul</a>&lt;Self::<a class="associatedtype" href="trait.Fixed.html#associatedtype.Bits" title="type foo::Fixed::Bits">Bits</a>, Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.MulAssign.html" title="trait core::ops::arith::MulAssign">MulAssign</a>&lt;Self::<a class="associatedtype" href="trait.Fixed.html#associatedtype.Bits" title="type foo::Fixed::Bits">Bits</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.Div.html" title="trait core::ops::arith::Div">Div</a>&lt;Self::<a class="associatedtype" href="trait.Fixed.html#associatedtype.Bits" title="type foo::Fixed::Bits">Bits</a>, Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.DivAssign.html" title="trait core::ops::arith::DivAssign">DivAssign</a>&lt;Self::<a class="associatedtype" href="trait.Fixed.html#associatedtype.Bits" title="type foo::Fixed::Bits">Bits</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.Rem.html" title="trait core::ops::arith::Rem">Rem</a>&lt;Self::<a class="associatedtype" href="trait.Fixed.html#associatedtype.Bits" title="type foo::Fixed::Bits">Bits</a>, Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.RemAssign.html" title="trait core::ops::arith::RemAssign">RemAssign</a>&lt;Self::<a class="associatedtype" href="trait.Fixed.html#associatedtype.Bits" title="type foo::Fixed::Bits">Bits</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.Rem.html" title="trait core::ops::arith::Rem">Rem</a>&lt;Self::<a class="associatedtype" href="trait.Fixed.html#associatedtype.NonZeroBits" title="type foo::Fixed::NonZeroBits">NonZeroBits</a>, Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/arith/trait.RemAssign.html" title="trait core::ops::arith::RemAssign">RemAssign</a>&lt;Self::<a class="associatedtype" href="trait.Fixed.html#associatedtype.NonZeroBits" title="type foo::Fixed::NonZeroBits">NonZeroBits</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/bit/trait.Not.html" title="trait core::ops::bit::Not">Not</a>&lt;Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/bit/trait.BitAnd.html" title="trait core::ops::bit::BitAnd">BitAnd</a>&lt;Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/bit/trait.BitAndAssign.html" title="trait core::ops::bit::BitAndAssign">BitAndAssign</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/bit/trait.BitOr.html" title="trait core::ops::bit::BitOr">BitOr</a>&lt;Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/bit/trait.BitOrAssign.html" title="trait core::ops::bit::BitOrAssign">BitOrAssign</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/bit/trait.BitXor.html" title="trait core::ops::bit::BitXor">BitXor</a>&lt;Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/bit/trait.BitXorAssign.html" title="trait core::ops::bit::BitXorAssign">BitXorAssign</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/bit/trait.Shl.html" title="trait core::ops::bit::Shl">Shl</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>, Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/bit/trait.ShlAssign.html" title="trait core::ops::bit::ShlAssign">ShlAssign</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/bit/trait.Shr.html" title="trait core::ops::bit::Shr">Shr</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>, Output = Self&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/ops/bit/trait.ShrAssign.html" title="trait core::ops::bit::ShrAssign">ShrAssign</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/iter/traits/accum/trait.Sum.html" title="trait core::iter::traits::accum::Sum">Sum</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/iter/traits/accum/trait.Product.html" title="trait core::iter::traits::accum::Product">Product</a><br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.i8.html">i8</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.i16.html">i16</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.i64.html">i64</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.i128.html">i128</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.isize.html">isize</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.u8.html">u8</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.u16.html">u16</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.u64.html">u64</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.u128.html">u128</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.usize.html">usize</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.f32.html">f32</a>&gt;<br />&#160;&#160;&#160;&#160; + <a class="trait" href="{{channel}}/core/cmp/trait.PartialOrd.html" title="trait core::cmp::PartialOrd">PartialOrd</a>&lt;<a class="primitive" href="{{channel}}/std/primitive.f64.html">f64</a>&gt;,</span>{
type <a href="#associatedtype.Bits" class="associatedtype">Bits</a>: <a class="trait" href="{{channel}}/core/convert/trait.From.html" title="trait core::convert::From">From</a>&lt;Self::<a class="associatedtype" href="trait.Fixed.html#associatedtype.NonZeroBits" title="type foo::Fixed::NonZeroBits">NonZeroBits</a>&gt;;
type <a href="#associatedtype.NonZeroBits" class="associatedtype">NonZeroBits</a>: <a class="trait" href="{{channel}}/core/convert/trait.TryFrom.html" title="trait core::convert::TryFrom">TryFrom</a>&lt;Self::<a class="associatedtype" href="trait.Fixed.html#associatedtype.Bits" title="type foo::Fixed::Bits">Bits</a>, Error = <a class="struct" href="{{channel}}/core/num/error/struct.TryFromIntError.html" title="struct core::num::error::TryFromIntError">TryFromIntError</a>&gt;;
}</code>
44 changes: 44 additions & 0 deletions tests/rustdoc/lot-of-bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// This is a regression test for <https://github.com/rust-lang/rust/issues/107283>.

#![crate_name = "foo"]

use std::convert::TryFrom;
use std::iter::{Product, Sum};
use std::num::TryFromIntError;
use std::ops::{Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div, DivAssign, Mul, MulAssign, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub, SubAssign};
use std::fmt::{Binary, Debug, Display, LowerExp, LowerHex, Octal, UpperExp, UpperHex};
use std::hash::Hash;

// @has 'foo/trait.Fixed.html'
// @snapshot bounds - '//*[@class="item-decl"]//code'
pub trait Fixed
where
Self: Default + Hash + Ord,
Self: Debug + Display + LowerExp + UpperExp,
Self: Binary + Octal + LowerHex + UpperHex,
Self: Add<Output = Self> + AddAssign,
Self: Sub<Output = Self> + SubAssign,
Self: Mul<Output = Self> + MulAssign,
Self: Div<Output = Self> + DivAssign,
Self: Rem<Output = Self> + RemAssign,
Self: Mul<<Self as Fixed>::Bits, Output = Self> + MulAssign<<Self as Fixed>::Bits>,
Self: Div<<Self as Fixed>::Bits, Output = Self> + DivAssign<<Self as Fixed>::Bits>,
Self: Rem<<Self as Fixed>::Bits, Output = Self> + RemAssign<<Self as Fixed>::Bits>,
Self: Rem<<Self as Fixed>::NonZeroBits, Output = Self>,
Self: RemAssign<<Self as Fixed>::NonZeroBits>,
Self: Not<Output = Self>,
Self: BitAnd<Output = Self> + BitAndAssign,
Self: BitOr<Output = Self> + BitOrAssign,
Self: BitXor<Output = Self> + BitXorAssign,
Self: Shl<u32, Output = Self> + ShlAssign<u32>,
Self: Shr<u32, Output = Self> + ShrAssign<u32>,
Self: Sum + Product,
Self: PartialOrd<i8> + PartialOrd<i16> + PartialOrd<i32>,
Self: PartialOrd<i64> + PartialOrd<i128> + PartialOrd<isize>,
Self: PartialOrd<u8> + PartialOrd<u16> + PartialOrd<u32>,
Self: PartialOrd<u64> + PartialOrd<u128> + PartialOrd<usize>,
Self: PartialOrd<f32> + PartialOrd<f64>,
{
type Bits: From<Self::NonZeroBits>;
type NonZeroBits: TryFrom<Self::Bits, Error = TryFromIntError>;
}