Skip to content

Commit bc25dc2

Browse files
authored
Rollup merge of rust-lang#140248 - GuillaumeGomez:fix-impl-block-items-indent, r=notriddle
Fix impl block items indent Fixes rust-lang#139771. Now, all impl block "before impl block items" indent are the same (ie, item info and documentation). With this fix, it looks like this: ![Screenshot From 2025-04-24 12-32-40](https://github.com/user-attachments/assets/907803a2-2949-4d01-afa5-fc3132d10ff2) You can test it [here](https://rustdoc.crud.net/imperio/fix-impl-block-items-indent/foo/struct.Context.html). r? `@notriddle`
2 parents c43022f + a29072a commit bc25dc2

File tree

6 files changed

+63
-14
lines changed

6 files changed

+63
-14
lines changed

src/librustdoc/html/render/mod.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,7 @@ fn render_impl(
20862086
.split_summary_and_content()
20872087
})
20882088
.unwrap_or((None, None));
2089+
20892090
write!(
20902091
w,
20912092
"{}",
@@ -2097,24 +2098,19 @@ fn render_impl(
20972098
use_absolute,
20982099
aliases,
20992100
before_dox.as_deref(),
2101+
trait_.is_none() && impl_.items.is_empty(),
21002102
)
21012103
)?;
21022104
if toggled {
21032105
w.write_str("</summary>")?;
21042106
}
21052107

2106-
if before_dox.is_some() {
2107-
if trait_.is_none() && impl_.items.is_empty() {
2108-
w.write_str(
2109-
"<div class=\"item-info\">\
2110-
<div class=\"stab empty-impl\">This impl block contains no items.</div>\
2111-
</div>",
2112-
)?;
2113-
}
2114-
if let Some(after_dox) = after_dox {
2115-
write!(w, "<div class=\"docblock\">{after_dox}</div>")?;
2116-
}
2108+
if before_dox.is_some()
2109+
&& let Some(after_dox) = after_dox
2110+
{
2111+
write!(w, "<div class=\"docblock\">{after_dox}</div>")?;
21172112
}
2113+
21182114
if !default_impl_items.is_empty() || !impl_items.is_empty() {
21192115
w.write_str("<div class=\"impl-items\">")?;
21202116
close_tags.push("</div>");
@@ -2182,6 +2178,7 @@ fn render_impl_summary(
21822178
// in documentation pages for trait with automatic implementations like "Send" and "Sync".
21832179
aliases: &[String],
21842180
doc: Option<&str>,
2181+
impl_is_empty: bool,
21852182
) -> impl fmt::Display {
21862183
fmt::from_fn(move |w| {
21872184
let inner_impl = i.inner_impl();
@@ -2237,6 +2234,13 @@ fn render_impl_summary(
22372234
}
22382235

22392236
if let Some(doc) = doc {
2237+
if impl_is_empty {
2238+
w.write_str(
2239+
"<div class=\"item-info\">\
2240+
<div class=\"stab empty-impl\">This impl block contains no items.</div>\
2241+
</div>",
2242+
)?;
2243+
}
22402244
write!(w, "<div class=\"docblock\">{doc}</div>")?;
22412245
}
22422246

src/librustdoc/html/static/css/rustdoc.css

+4-1
Original file line numberDiff line numberDiff line change
@@ -2319,7 +2319,10 @@ details.toggle > summary:not(.hideme)::before {
23192319
doc block while aligning it with the impl block items. */
23202320
.implementors-toggle > .docblock,
23212321
/* We indent trait items as well. */
2322-
#main-content > .methods > :not(.item-info) {
2322+
#main-content > .methods > :not(.item-info),
2323+
.impl > .item-info,
2324+
.impl > .docblock,
2325+
.impl + .docblock {
23232326
margin-left: var(--impl-items-indent);
23242327
}
23252328

tests/rustdoc-gui/docblock-table-overflow.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ assert-property: (".top-doc .docblock table", {"scrollWidth": "1572"})
1111
// Checking it works on other doc blocks as well...
1212

1313
// Logically, the ".docblock" and the "<p>" should have the same scroll width (if we exclude the margin).
14-
assert-property: ("#implementations-list > details .docblock", {"scrollWidth": 816})
14+
assert-property: ("#implementations-list > details .docblock", {"scrollWidth": 835})
1515
assert-property: ("#implementations-list > details .docblock > p", {"scrollWidth": 835})
1616
// However, since there is overflow in the <table>, its scroll width is bigger.
1717
assert-property: ("#implementations-list > details .docblock table", {"scrollWidth": "1572"})
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Checks the impl block docs have the correct indent.
2+
go-to: "file://" + |DOC_PATH| + "/test_docs/impls_indent/struct.Context.html"
3+
4+
// First we ensure that the impl items are indent (more on the right of the screen) than the
5+
// impl itself.
6+
store-position: ("#impl-Context", {"x": impl_x})
7+
store-position: ("#impl-Context > .item-info", {"x": impl_item_x})
8+
assert: |impl_x| < |impl_item_x|
9+
10+
// And we ensure that all impl items have the same indent.
11+
assert-position: ("#impl-Context > .docblock", {"x": |impl_item_x|})
12+
assert-position: ("#impl-Context + .docblock", {"x": |impl_item_x|})
13+
14+
// Same with the collapsible impl block.
15+
assert-position: ("#impl-Context-1 > .docblock", {"x": |impl_item_x|})
16+
assert-position: (".implementors-toggle > summary + .docblock", {"x": |impl_item_x|})

tests/rustdoc-gui/item-info-overflow.goml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ compare-elements-property: (
2121
)
2222
assert-property: (
2323
"#impl-SimpleTrait-for-LongItemInfo2 .item-info",
24-
{"scrollWidth": "916"},
24+
{"scrollWidth": "935"},
2525
)
2626
// Just to be sure we're comparing the correct "item-info":
2727
assert-text: (

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

+26
Original file line numberDiff line numberDiff line change
@@ -740,3 +740,29 @@ pub mod SidebarSort {
740740
impl Sort for Cell<u8> {}
741741
impl<'a> Sort for &'a str {}
742742
}
743+
744+
pub mod impls_indent {
745+
pub struct Context;
746+
747+
/// Working with objects.
748+
///
749+
/// # Safety
750+
///
751+
/// Functions that take indices of locals do not check bounds on these indices;
752+
/// the caller must ensure that the indices are less than the number of locals
753+
/// in the current stack frame.
754+
impl Context {
755+
}
756+
757+
/// Working with objects.
758+
///
759+
/// # Safety
760+
///
761+
/// Functions that take indices of locals do not check bounds on these indices;
762+
/// the caller must ensure that the indices are less than the number of locals
763+
/// in the current stack frame.
764+
impl Context {
765+
/// bla
766+
pub fn bar() {}
767+
}
768+
}

0 commit comments

Comments
 (0)