Skip to content

rustdoc: Fix trait impl ordering#157233

Merged
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
nnethercote:rustdoc-impl-order
Jun 2, 2026
Merged

rustdoc: Fix trait impl ordering#157233
rust-bors[bot] merged 3 commits into
rust-lang:mainfrom
nnethercote:rustdoc-impl-order

Conversation

@nnethercote
Copy link
Copy Markdown
Contributor

For types, rustdoc produces different impl orderings in the sidebar vs. the main section.

E.g. for std::cell::UnsafeCell the "Trait Implementations" sidebar order is this:

!Freeze
!RefUnwindSafe
!Sync
CoerceUnsized<UnsafeCell<U>>
Debug
Default
DispatchFromDyn<UnsafeCell<U>>
From<T>

The primary sort is on polarity, and the secondary sort is alphabetical. Makes sense.

The main section order is this:

impl<T> Debug for UnsafeCell<T>
impl<T> Default for UnsafeCell<T>
impl<T> From<T> for UnsafeCell<T>
impl<T, U> CoerceUnsized<UnsafeCell<U>> for UnsafeCell<T>
impl<T, U> DispatchFromDyn<UnsafeCell<U>> for UnsafeCell<T>
impl<T> !Freeze for UnsafeCell<T>
impl<T> !RefUnwindSafe for UnsafeCell<T>
impl<T> !Sync for UnsafeCell<T>

This strange ordering occurs because the entries are sorted on the generated HTML. Impls with methods (the first three) come first because they start with a <details> tag while the others start with a <section> tag. After that, the order depends on a section id of the form impl-{trait}-for-{type} that doesn't include the polarity, which is why the secondary ordering is alphabetical but ignores the !.

The sidebar ordering is the better of the two. This commit changes the main section to use the same ordering as the sidebar. This involves creating a new function impl_trait_key shared between the two parts.

r? @GuillaumeGomez

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Jun 1, 2026
@nnethercote
Copy link
Copy Markdown
Contributor Author

It's not ideal that this change didn't require any test changes. I know very little about rustdoc testing so I'm not sure where/how to add tests for this.

@GuillaumeGomez
Copy link
Copy Markdown
Member

The tool we use for XPaths in tests/rustdoc-html doesn't allow to specify which nth item we want, so we'll be forced to add a test in tests/rustdoc-gui. The selector for the trait implementations in the sidebar would be .sidebar .trait-implementation li:nth-child(3) a (where you change the integer of nth-child) to check its text with assert-text.

For the main section, the selector is #trait-implementations-list details:nth-child(3) .code-header and same as described previously. Don't hesitate if you need help.

@nnethercote nnethercote force-pushed the rustdoc-impl-order branch from 2665e2a to a43bb36 Compare June 2, 2026 01:44
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 2, 2026

Some changes occurred in GUI tests.

cc @GuillaumeGomez

@nnethercote
Copy link
Copy Markdown
Contributor Author

I have added a new test. Thanks for the rustdoc-gui tips, they were very helpful.

@rust-log-analyzer

This comment has been minimized.

@nnethercote nnethercote force-pushed the rustdoc-impl-order branch from a43bb36 to 7a6efc5 Compare June 2, 2026 03:04
This shows how the ordering in the sidebar and the main section differ.
The next commit will fix this inconsistency.
For types, rustdoc produces different impl orderings in the sidebar vs.
the main section.

E.g. for `std::cell::UnsafeCell` the "Trait Implementations" sidebar
order is this:
```
!Freeze
!RefUnwindSafe
!Sync
CoerceUnsized<UnsafeCell<U>>
Debug
Default
DispatchFromDyn<UnsafeCell<U>>
From<T>
```
The primary sort is on polarity, and the secondary sort is alphabetical.
Makes sense.

The main section order is this:
```
impl<T> Debug for UnsafeCell<T>
impl<T> Default for UnsafeCell<T>
impl<T> From<T> for UnsafeCell<T>
impl<T, U> CoerceUnsized<UnsafeCell<U>> for UnsafeCell<T>
impl<T, U> DispatchFromDyn<UnsafeCell<U>> for UnsafeCell<T>
impl<T> !Freeze for UnsafeCell<T>
impl<T> !RefUnwindSafe for UnsafeCell<T>
impl<T> !Sync for UnsafeCell<T>
```
This strange ordering occurs because the entries are sorted on the
generated HTML. Impls with methods (the first three) come first because
they start with a `<details>` tag while the others start with a
`<section>` tag. After that, the order depends on a section id of the
form `impl-{trait}-for-{type}` that doesn't include the polarity, which
is why the secondary ordering is alphabetical but ignores the `!`.

The sidebar ordering is the better of the two. This commit changes the
main section to use the same ordering as the sidebar. This involves
creating a new function `impl_trait_key` shared between the two parts.
@nnethercote nnethercote force-pushed the rustdoc-impl-order branch from 7a6efc5 to eef3ae7 Compare June 2, 2026 03:04
@nnethercote nnethercote changed the title Fix impl ordering rustdoc: Fix trait impl ordering Jun 2, 2026
@GuillaumeGomez
Copy link
Copy Markdown
Member

Thanks!

@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 2, 2026

📌 Commit eef3ae7 has been approved by GuillaumeGomez

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 2, 2026
rust-bors Bot pushed a commit that referenced this pull request Jun 2, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #156467 (mark the 'import linkage' statics as unnamed_addr)
 - #156923 (Couple of diagnostics improvements for EII)
 - #157240 (Enable Enzyme for aarch64-apple-darwin)
 - #157244 (Privacy: tweak macros + more tests)
 - #157276 (miri subtree update)
 - #157130 (Use a `ArrayVec` in `CastTarget`)
 - #157131 (Add regression test for issue #144888)
 - #157195 (Move feature gating to the new attr parsing infrastructure)
 - #157233 (rustdoc: Fix trait impl ordering)
 - #157256 (tests: adapt for LLVM codegen change)
 - #157265 (Update books)
 - #157277 (triagebot.toml: add LawnGnome to libs reviewers)
 - #157291 (Clean up attribute target checking diagnostics)
 - #157301 (Remove unused import from a test)
rust-bors Bot pushed a commit that referenced this pull request Jun 2, 2026
…uwer

Rollup of 14 pull requests

Successful merges:

 - #156467 (mark the 'import linkage' statics as unnamed_addr)
 - #156923 (Couple of diagnostics improvements for EII)
 - #157240 (Enable Enzyme for aarch64-apple-darwin)
 - #157244 (Privacy: tweak macros + more tests)
 - #157276 (miri subtree update)
 - #157130 (Use a `ArrayVec` in `CastTarget`)
 - #157131 (Add regression test for issue #144888)
 - #157195 (Move feature gating to the new attr parsing infrastructure)
 - #157233 (rustdoc: Fix trait impl ordering)
 - #157256 (tests: adapt for LLVM codegen change)
 - #157265 (Update books)
 - #157277 (triagebot.toml: add LawnGnome to libs reviewers)
 - #157291 (Clean up attribute target checking diagnostics)
 - #157301 (Remove unused import from a test)
@rust-bors rust-bors Bot merged commit 181ccb1 into rust-lang:main Jun 2, 2026
12 checks passed
@rustbot rustbot added this to the 1.98.0 milestone Jun 2, 2026
rust-timer added a commit that referenced this pull request Jun 2, 2026
Rollup merge of #157233 - nnethercote:rustdoc-impl-order, r=GuillaumeGomez

rustdoc: Fix trait impl ordering

For types, rustdoc produces different impl orderings in the sidebar vs. the main section.

E.g. for `std::cell::UnsafeCell` the "Trait Implementations" sidebar order is this:
```
!Freeze
!RefUnwindSafe
!Sync
CoerceUnsized<UnsafeCell<U>>
Debug
Default
DispatchFromDyn<UnsafeCell<U>>
From<T>
```
The primary sort is on polarity, and the secondary sort is alphabetical. Makes sense.

The main section order is this:
```
impl<T> Debug for UnsafeCell<T>
impl<T> Default for UnsafeCell<T>
impl<T> From<T> for UnsafeCell<T>
impl<T, U> CoerceUnsized<UnsafeCell<U>> for UnsafeCell<T>
impl<T, U> DispatchFromDyn<UnsafeCell<U>> for UnsafeCell<T>
impl<T> !Freeze for UnsafeCell<T>
impl<T> !RefUnwindSafe for UnsafeCell<T>
impl<T> !Sync for UnsafeCell<T>
```
This strange ordering occurs because the entries are sorted on the generated HTML. Impls with methods (the first three) come first because they start with a `<details>` tag while the others start with a `<section>` tag. After that, the order depends on a section id of the form `impl-{trait}-for-{type}` that doesn't include the polarity, which is why the secondary ordering is alphabetical but ignores the `!`.

The sidebar ordering is the better of the two. This commit changes the main section to use the same ordering as the sidebar. This involves creating a new function `impl_trait_key` shared between the two parts.

r? @GuillaumeGomez
@nnethercote nnethercote deleted the rustdoc-impl-order branch June 3, 2026 00:25
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

@rust-timer build 0f41b4a

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (0f41b4a): comparison URL.

Overall result: ❌ regressions - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.2% [0.1%, 0.3%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [0.1%, 0.3%] 2

Max RSS (memory usage)

Results (secondary 5.5%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
5.5% [5.5%, 5.5%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results (primary 2.1%, secondary -1.6%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.1% [2.1%, 2.1%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-1.6% [-1.6%, -1.6%] 1
All ❌✅ (primary) 2.1% [2.1%, 2.1%] 1

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 510.625s -> 511.417s (0.16%)
Artifact size: 400.75 MiB -> 400.78 MiB (0.01%)

@rustbot rustbot added the perf-regression Performance regression. label Jun 3, 2026
@JonathanBrouwer
Copy link
Copy Markdown
Contributor

This PR caused the regression in #157303

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants