diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 0097386591529..9256330ac7c46 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -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 } diff --git a/tests/rustdoc-gui/src/test_docs/lib.rs b/tests/rustdoc-gui/src/test_docs/lib.rs index 7e34178e56f03..6c638dc3b9d87 100644 --- a/tests/rustdoc-gui/src/test_docs/lib.rs +++ b/tests/rustdoc-gui/src/test_docs/lib.rs @@ -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 {} +} diff --git a/tests/rustdoc-gui/trait-with-bounds.goml b/tests/rustdoc-gui/trait-with-bounds.goml new file mode 100644 index 0000000000000..f076bdd47070d --- /dev/null +++ b/tests/rustdoc-gui/trait-with-bounds.goml @@ -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"], +) diff --git a/tests/rustdoc-gui/type-declation-overflow.goml b/tests/rustdoc-gui/type-declation-overflow.goml index fdf84c3fd2950..8df946c6f39ed 100644 --- a/tests/rustdoc-gui/type-declation-overflow.goml +++ b/tests/rustdoc-gui/type-declation-overflow.goml @@ -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 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 "
" 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"})