Skip to content

Commit 17aaba7

Browse files
authored
Rollup merge of #127975 - GuillaumeGomez:fix-trait-bounds-display, r=notriddle
Fix trait bounds display Fixes #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`
2 parents ae28d5c + eec3c3d commit 17aaba7

File tree

4 files changed

+68
-16
lines changed

4 files changed

+68
-16
lines changed

src/librustdoc/html/render/print_item.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -2062,16 +2062,23 @@ pub(super) fn item_path(ty: ItemType, name: &str) -> String {
20622062

20632063
fn bounds(t_bounds: &[clean::GenericBound], trait_alias: bool, cx: &Context<'_>) -> String {
20642064
let mut bounds = String::new();
2065-
if !t_bounds.is_empty() {
2066-
if !trait_alias {
2065+
if t_bounds.is_empty() {
2066+
return bounds;
2067+
}
2068+
let has_lots_of_bounds = t_bounds.len() > 2;
2069+
let inter_str = if has_lots_of_bounds { "\n + " } else { " + " };
2070+
if !trait_alias {
2071+
if has_lots_of_bounds {
2072+
bounds.push_str(":\n ");
2073+
} else {
20672074
bounds.push_str(": ");
20682075
}
2069-
for (i, p) in t_bounds.iter().enumerate() {
2070-
if i > 0 {
2071-
bounds.push_str(" + ");
2072-
}
2073-
bounds.push_str(&p.print(cx).to_string());
2076+
}
2077+
for (i, p) in t_bounds.iter().enumerate() {
2078+
if i > 0 {
2079+
bounds.push_str(inter_str);
20742080
}
2081+
bounds.push_str(&p.print(cx).to_string());
20752082
}
20762083
bounds
20772084
}

tests/rustdoc-gui/src/test_docs/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -614,3 +614,9 @@ pub mod private {
614614
B,
615615
}
616616
}
617+
618+
pub mod trait_bounds {
619+
pub trait OneBound: Sized {}
620+
pub trait TwoBounds: Sized + Copy {}
621+
pub trait ThreeBounds: Sized + Copy + Eq {}
622+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Check that if a trait has more than 2 bounds, they are displayed on different lines.
2+
3+
// It tries to load a JS for each trait but there are none since they're not implemented.
4+
fail-on-request-error: false
5+
go-to: "file://" + |DOC_PATH| + "/test_docs/trait_bounds/trait.OneBound.html"
6+
// They should have the same Y position.
7+
compare-elements-position: (
8+
".item-decl code",
9+
".item-decl a.trait[title='trait core::marker::Sized']",
10+
["y"],
11+
)
12+
go-to: "file://" + |DOC_PATH| + "/test_docs/trait_bounds/trait.TwoBounds.html"
13+
// They should have the same Y position.
14+
compare-elements-position: (
15+
".item-decl code",
16+
".item-decl a.trait[title='trait core::marker::Copy']",
17+
["y"],
18+
)
19+
go-to: "file://" + |DOC_PATH| + "/test_docs/trait_bounds/trait.ThreeBounds.html"
20+
// All on their own line.
21+
compare-elements-position-false: (
22+
".item-decl code",
23+
".item-decl a.trait[title='trait core::marker::Sized']",
24+
["y"],
25+
)
26+
compare-elements-position-false: (
27+
".item-decl a.trait[title='trait core::marker::Sized']",
28+
".item-decl a.trait[title='trait core::marker::Copy']",
29+
["y"],
30+
)
31+
compare-elements-position-false: (
32+
".item-decl a.trait[title='trait core::marker::Copy']",
33+
".item-decl a.trait[title='trait core::cmp::Eq']",
34+
["y"],
35+
)

tests/rustdoc-gui/type-declation-overflow.goml

+13-9
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,38 @@ fail-on-request-error: false
88

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

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

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

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

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

0 commit comments

Comments
 (0)