Skip to content

rustdoc: deterministic sorting for doc_cfg badges#156401

Open
shivendra02467 wants to merge 1 commit into
rust-lang:mainfrom
shivendra02467:doc-cfg-sort-fix
Open

rustdoc: deterministic sorting for doc_cfg badges#156401
shivendra02467 wants to merge 1 commit into
rust-lang:mainfrom
shivendra02467:doc-cfg-sort-fix

Conversation

@shivendra02467
Copy link
Copy Markdown
Contributor

@shivendra02467 shivendra02467 commented May 10, 2026

Fixes #156391

Currently, target-exclusive doc_cfg badges (eg. "Available on...") reuse the order of predicates as they appear in the source code. This often buries popular targets behind niche ones and leads to inconsistent UI rendering.

This PR introduces a deterministic sorting mechanism to the Cfg AST prior to HTML/JSON rendering.

Note for Reviewers:
To provide the best UX, I implemented a lightweight tiering heuristic (prioritizing major platforms like Linux/Apple/Windows first, followed by mobile, then BSDs, and alphabetizing the rest). However, I am completely open to tweaking these priority groupings or falling back to a different sorting logic if the team prefers. Let me know what you think!

@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 May 10, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 10, 2026

r? @fmease

rustbot has assigned @fmease.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: rustdoc
  • rustdoc expanded to 9 candidates
  • Random selection from GuillaumeGomez, fmease, lolbinarycat, notriddle

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 10, 2026

Some changes occurred in GUI tests.

cc @GuillaumeGomez

@GuillaumeGomez
Copy link
Copy Markdown
Member

Sorting the doc_cfg badges sounds like a good idea, but I'm not sure sorting by "importance" is a good idea. I think we should sort by target then by feature and internally alphabetically.

As for not()/all()/any(), not too sure...

@GuillaumeGomez
Copy link
Copy Markdown
Member

Also, would be nice to have a unit test with different scenarios directly in cfg.rs.

@shivendra02467
Copy link
Copy Markdown
Contributor Author

Sorting the doc_cfg badges sounds like a good idea, but I'm not sure sorting by "importance" is a good idea. I think we should sort by target then by feature and internally alphabetically.

@GuillaumeGomez Thanks for the review! That makes complete sense.

I've just pushed a commit that drops the importance heuristic and replaces it with the logic you suggested.

Updates:

  1. Sorting Order: It now categorizes by Targets (0) -> Target Features (1) -> Crate Features (2). Within those categories, it falls back to case-insensitive alphabetical sorting.
  2. Unit Tests: Added test_sort_for_rendering in cfg/tests.rs to assert this exact categorization and alphabetical fallback, and moved the HTML regression test into the doc-cfg/ directory.

As for not()/all()/any(), not too sure...

I completely agree these are tricky! Here is how this commit handles them:

  • It recursively sorts their internal contents using the exact same target -> feature -> alphabetical rules.
  • It places the any/all/not blocks themselves into a final Category (3). This effectively pushes complex, nested groupings to the end of the badge list, keeping the simple, single-item targets and features grouped neatly at the front (e.g., it will render unix and (linux or windows) rather than (linux or windows) and unix).

Does pushing the complex nested blocks to the end seem like the best UX call to you, or would you prefer them sorted differently?

@GuillaumeGomez
Copy link
Copy Markdown
Member

Does pushing the complex nested blocks to the end seem like the best UX call to you, or would you prefer them sorted differently?

Sounds like the right way to handle them indeed. Good call!

@fmease fmease assigned GuillaumeGomez and unassigned fmease May 22, 2026
@fmease fmease added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 22, 2026
@shivendra02467
Copy link
Copy Markdown
Contributor Author

Sounds like the right way to handle them indeed. Good call!

@GuillaumeGomez Thanks! Is there any other change needed before we can merge this?

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 27, 2026
@GuillaumeGomez
Copy link
Copy Markdown
Member

Sorry, I didn't realize that the changes were already pushed. Looks all good to me, thanks!

@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 27, 2026

📌 Commit 7cbc028 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 May 27, 2026
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request May 27, 2026
…=GuillaumeGomez

rustdoc: deterministic sorting for `doc_cfg` badges

Fixes rust-lang#156391

Currently, target-exclusive `doc_cfg` badges (eg. "Available on...") reuse the order of predicates as they appear in the source code. This often buries popular targets behind niche ones and leads to inconsistent UI rendering.

This PR introduces a deterministic sorting mechanism to the `Cfg` AST prior to HTML/JSON rendering.

**Note for Reviewers:**
To provide the best UX, I implemented a lightweight tiering heuristic (prioritizing major platforms like Linux/Apple/Windows first, followed by mobile, then BSDs, and alphabetizing the rest). However, I am completely open to tweaking these priority groupings or falling back to a different sorting logic if the team prefers. Let me know what you think!
rust-bors Bot pushed a commit that referenced this pull request May 27, 2026
Rollup of 8 pull requests

Successful merges:

 - #156970 (coverage: Use original HIR info for synthetic by-move coroutine bodies)
 - #156390 (Constify Iterator-related methods and functions)
 - #156401 (rustdoc: deterministic sorting for `doc_cfg` badges)
 - #156845 (Clarify "infinite size" in cyclic-type diagnostic refers to the type name)
 - #156973 (Add uwtable annotation to modules when required)
 - #156985 (Limit the additional DLL to Windows)
 - #156988 (interpret/validity: properly treat zero-variant enums so that we do not have to check layout.is_uninhabited)
 - #157002 (std: Fix thread::available_parallelism on Redox targets)
@GuillaumeGomez
Copy link
Copy Markdown
Member

Failed in #157020 (comment).

@bors r-

@rust-bors rust-bors Bot added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 27, 2026
@rust-bors rust-bors Bot removed the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label May 27, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 27, 2026

This pull request was unapproved.

This PR was contained in a rollup (#157020), which was unapproved.

View changes since this unapproval

@shivendra02467 shivendra02467 force-pushed the doc-cfg-sort-fix branch 2 times, most recently from 7cbc028 to a5e27d3 Compare May 27, 2026 19:19
@GuillaumeGomez
Copy link
Copy Markdown
Member

cfg() and doc(cfg()) should work the same, so not sure your last commit is fixing the CI issue. However: please add the same test for both kind of attributes so we're sure they're handled the same.

@shivendra02467
Copy link
Copy Markdown
Contributor Author

cfg() and doc(cfg()) should work the same, so not sure your last commit is fixing the CI issue. However: please add the same test for both kind of attributes so we're sure they're handled the same.

You are absolutely right that their badges should look and behave identically!
Just to clarify the CI failure: the Windows runner failed previously because #[cfg(target_os = "linux")] caused the Rust compiler to actively strip fn foo() out of the AST before rustdoc could even see it, resulting in a 'File does not exist' test error. Switching it to #[doc(cfg(...))] made the attribute inert for conditional compilation, allowing the file to generate and the badge sorting to be tested across all CI platforms.
However, I completely agree we need to guarantee the sorting AST hits both paths! I have just pushed an update to tests/rustdoc-html/doc-cfg/sort.rs. I included #![doc(auto_cfg)] (noting that doc_auto_cfg was recently merged into doc_cfg) and added a second test specifically for standard #[cfg(...)]. I used custom feature flags passed via compile-flags for that second test so it will successfully compile and test the badge sorting on all CI runners without being stripped.
Let me know if everything looks good!


#![crate_name = "foo"]
#![feature(doc_cfg)]
#![doc(auto_cfg)]
Copy link
Copy Markdown
Member

@GuillaumeGomez GuillaumeGomez May 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this line.

View changes since the review

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

Long lists of doc_cfg targets are not sorted in a useful way

5 participants