Skip to content

Commit

Permalink
Unrolled build for rust-lang#127975
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#127975 - GuillaumeGomez:fix-trait-bounds-display, r=notriddle

Fix trait bounds display

Fixes rust-lang#127398.

I took a simple rule: if there are more than two bounds, we display them like rustfmt.

Before this PR:

![Screenshot from 2024-07-19 17-38-59](https://github.com/user-attachments/assets/4162b57e-7ebb-48f9-a3a1-25e443c140cb)

After this PR:

![Screenshot from 2024-07-19 17-39-09](https://github.com/user-attachments/assets/a3ba22dd-5f34-45d0-ad9d-0cdf89dc509c)

r? `@notriddle`
  • Loading branch information
rust-timer authored Jul 21, 2024
2 parents c1a631d + eec3c3d commit 9b50e0f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
21 changes: 14 additions & 7 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2062,16 +2062,23 @@ pub(super) fn item_path(ty: ItemType, name: &str) -> String {

fn bounds(t_bounds: &[clean::GenericBound], trait_alias: bool, cx: &Context<'_>) -> String {
let mut bounds = String::new();
if !t_bounds.is_empty() {
if !trait_alias {
if t_bounds.is_empty() {
return bounds;
}
let has_lots_of_bounds = t_bounds.len() > 2;
let inter_str = if has_lots_of_bounds { "\n + " } else { " + " };
if !trait_alias {
if has_lots_of_bounds {
bounds.push_str(":\n ");
} else {
bounds.push_str(": ");
}
for (i, p) in t_bounds.iter().enumerate() {
if i > 0 {
bounds.push_str(" + ");
}
bounds.push_str(&p.print(cx).to_string());
}
for (i, p) in t_bounds.iter().enumerate() {
if i > 0 {
bounds.push_str(inter_str);
}
bounds.push_str(&p.print(cx).to_string());
}
bounds
}
Expand Down
6 changes: 6 additions & 0 deletions tests/rustdoc-gui/src/test_docs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,3 +614,9 @@ pub mod private {
B,
}
}

pub mod trait_bounds {
pub trait OneBound: Sized {}
pub trait TwoBounds: Sized + Copy {}
pub trait ThreeBounds: Sized + Copy + Eq {}
}
35 changes: 35 additions & 0 deletions tests/rustdoc-gui/trait-with-bounds.goml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Check that if a trait has more than 2 bounds, they are displayed on different lines.

// It tries to load a JS for each trait but there are none since they're not implemented.
fail-on-request-error: false
go-to: "file://" + |DOC_PATH| + "/test_docs/trait_bounds/trait.OneBound.html"
// They should have the same Y position.
compare-elements-position: (
".item-decl code",
".item-decl a.trait[title='trait core::marker::Sized']",
["y"],
)
go-to: "file://" + |DOC_PATH| + "/test_docs/trait_bounds/trait.TwoBounds.html"
// They should have the same Y position.
compare-elements-position: (
".item-decl code",
".item-decl a.trait[title='trait core::marker::Copy']",
["y"],
)
go-to: "file://" + |DOC_PATH| + "/test_docs/trait_bounds/trait.ThreeBounds.html"
// All on their own line.
compare-elements-position-false: (
".item-decl code",
".item-decl a.trait[title='trait core::marker::Sized']",
["y"],
)
compare-elements-position-false: (
".item-decl a.trait[title='trait core::marker::Sized']",
".item-decl a.trait[title='trait core::marker::Copy']",
["y"],
)
compare-elements-position-false: (
".item-decl a.trait[title='trait core::marker::Copy']",
".item-decl a.trait[title='trait core::cmp::Eq']",
["y"],
)
22 changes: 13 additions & 9 deletions tests/rustdoc-gui/type-declation-overflow.goml
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,38 @@ fail-on-request-error: false

go-to: "file://" + |DOC_PATH| + "/lib2/long_trait/trait.ALongNameBecauseItHelpsTestingTheCurrentProblem.html"
// We set a fixed size so there is no chance of "random" resize.
set-window-size: (1100, 800)
set-window-size: (710, 800)
// Logically, the <body> scroll width should be the width of the window.
assert-property: ("body", {"scrollWidth": "1100"})
// However, since there is overflow in the type declaration, its scroll width is bigger.
assert-property: ("pre.item-decl", {"scrollWidth": "1324"})
assert-property: ("body", {"scrollWidth": "710"})
// We now check that the section width hasn't grown because of it.
assert-property: ("#main-content", {"scrollWidth": "450"})
// However, since there is overflow in the type declaration, its scroll width is bigger that "#main-content".
assert-property: ("pre.item-decl", {"scrollWidth": "585"})

// In the table-ish view on the module index, the name should not be wrapped more than necessary.
go-to: "file://" + |DOC_PATH| + "/lib2/too_long/index.html"

// We'll ensure that items with short documentation have the same width.
store-property: ("//*[@class='item-table']//*[@class='struct']/..", {"offsetWidth": offset_width})
assert: |offset_width| == "277"
assert: |offset_width| == "149"
assert-property: ("//*[@class='item-table']//*[@class='constant']/..", {"offsetWidth": |offset_width|})

// We now make the same check on type declaration...
go-to: "file://" + |DOC_PATH| + "/lib2/too_long/type.ReallyLongTypeNameLongLongLong.html"
assert-property: ("body", {"scrollWidth": "1100"})
assert-property: ("body", {"scrollWidth": "710"})
// Getting the width of the "<main>" element.
assert-property: ("main", {"scrollWidth": "510"})
// We now check that the section width hasn't grown because of it.
assert-property: ("#main-content", {"scrollWidth": "840"})
assert-property: ("#main-content", {"scrollWidth": "450"})
// And now checking that it has scrollable content.
assert-property: ("pre.item-decl", {"scrollWidth": "1103"})

// ... and constant.
// On a sidenote, it also checks that the (very) long title isn't changing the docblock width.
go-to: "file://" + |DOC_PATH| + "/lib2/too_long/constant.ReallyLongTypeNameLongLongLongConstBecauseWhyNotAConstRightGigaGigaSupraLong.html"
assert-property: ("body", {"scrollWidth": "1100"})
assert-property: ("body", {"scrollWidth": "710"})
// We now check that the section width hasn't grown because of it.
assert-property: ("#main-content", {"scrollWidth": "840"})
assert-property: ("#main-content", {"scrollWidth": "450"})
// And now checking that it has scrollable content.
assert-property: ("pre.item-decl", {"scrollWidth": "950"})

Expand Down

0 comments on commit 9b50e0f

Please sign in to comment.