rust-lang / rust-lang/rust

Sorting has drifted between rustfmt, rustdoc, and other places where it matters

Open
#151,934 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-libtest C-bug T-compiler T-rustdoc
Dominant language
Rust
Stars
119k
Forks
16.2k
PR merge metrics
PR metrics pending

Description

Right now, the best way to sort paths in Rust is defined by the style guide, which is followed by rustfmt. However, rustdoc does not fully honour this sorting method, only doing the numeric portion of the sorting, and the test harness uses lexicographic sorting.

(Also, since GitHub is garbage software, clicking on the links will not honour the line numbers in the web app. They do have attached line numbers in some cases.)

I don't know if there are other relevant places where this should be followed, but isn't.

Note that while the algorithm does account for general strings whereas only path-sorting is needed in most cases, this simple crate source shows the discrepancies:

pub struct X_Y;
pub struct XY;

mod test {
    pub use super::{X_Y, XY};
}

#[test]
fn test_X_Y() {}
#[test]
fn test_XY() {}
#[test]
fn test_u8() {}
#[test]
fn test_u16() {}

Note that rustdoc will show XY before X_Y, but rustfmt will always sort X_Y before XY when formatting the file. The numeric rule, which is followed by both, sorts u8 before u16, although this fails when you run the tests.

I was going to "solve" this issue by making the test harness sort things the same way as rustdoc… when I learned that both were actually not doing what rustfmt does. The function that rustdoc points to is from an old commit, and when you go to the latest one, you find out that the sorting algorithm has been more refined since then.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Rust style guide's sorting rules, then compare rustfmt's src/sort.rs with rustdoc's src/librustdoc/html/render/print_item.rs and the test harness at compiler/rustc_builtin_macros/src/test_harness.rs. Use the provided X_Y, XY, u8, and u16 example to reproduce the differences. Done means the relevant sorting behavior is consistent and covered by tests, including any additional locations found during the review.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, testing, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.