Rollup merge of #127975 - GuillaumeGomez:fix-trait-bounds-display, r=notriddle
Fix trait bounds display Fixes https://github.com/rust-lang/rust/issues/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`
This commit is contained in:
commit
17aaba70d9
4 changed files with 68 additions and 16 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
tests/rustdoc-gui/trait-with-bounds.goml
Normal file
35
tests/rustdoc-gui/trait-with-bounds.goml
Normal file
|
@ -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"],
|
||||
)
|
|
@ -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"})
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue