Skip to content

Commit a33adf5

Browse files
authored
Unrolled build for rust-lang#115046
Rollup merge of rust-lang#115046 - joshtriplett:master, r=compiler-errors Use version-sorting for all sorting Add a description of a version-sorting algorithm. (This algorithm does not precisely match `strverscmp`; it's intentionally simpler in its handling of leading zeroes, and produces a result easier for humans to easily understand and do by hand.) Change all references to sorting to use version-sorting. Change all references to "ASCIIbetically" to instead say "sort non-lowercase before lowercase".
2 parents 0a89233 + 2e931b5 commit a33adf5

File tree

4 files changed

+104
-11
lines changed

4 files changed

+104
-11
lines changed

src/doc/style-guide/src/README.md

+84
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,90 @@ fn bar() {}
112112
fn baz() {}
113113
```
114114

115+
### Sorting
116+
117+
In various cases, the default Rust style specifies to sort things. If not
118+
otherwise specified, such sorting should be "version sorting", which ensures
119+
that (for instance) `x8` comes before `x16` even though the character `1` comes
120+
before the character `8`. (If not otherwise specified, version-sorting is
121+
lexicographical.)
122+
123+
For the purposes of the Rust style, to compare two strings for version-sorting:
124+
125+
- Process both strings from beginning to end as two sequences of maximal-length
126+
chunks, where each chunk consists either of a sequence of characters other
127+
than ASCII digits, or a sequence of ASCII digits (a numeric chunk), and
128+
compare corresponding chunks from the strings.
129+
- To compare two numeric chunks, compare them by numeric value, ignoring
130+
leading zeroes. If the two chunks have equal numeric value, but different
131+
numbers of leading digits, and this is the first time this has happened for
132+
these strings, treat the chunks as equal (moving on to the next chunk) but
133+
remember which string had more leading zeroes.
134+
- To compare two chunks if both are not numeric, compare them by Unicode
135+
character lexicographically, except that `_` (underscore) sorts immediately
136+
after ` ` (space) but before any other character. (This treats underscore as
137+
a word separator, as commonly used in identifiers.)
138+
- If the use of version sorting specifies further modifiers, such as sorting
139+
non-lowercase before lowercase, apply those modifiers to the lexicographic
140+
sort in this step.
141+
- If the comparison reaches the end of the string and considers each pair of
142+
chunks equal:
143+
- If one of the numeric comparisons noted the earliest point at which one
144+
string had more leading zeroes than the other, sort the string with more
145+
leading zeroes first.
146+
- Otherwise, the strings are equal.
147+
148+
Note that there exist various algorithms called "version sorting", which
149+
generally try to solve the same problem, but which differ in various ways (such
150+
as in their handling of numbers with leading zeroes). This algorithm
151+
does not purport to precisely match the behavior of any particular other
152+
algorithm, only to produce a simple and satisfying result for Rust formatting.
153+
In particular, this algorithm aims to produce a satisfying result for a set of
154+
symbols that have the same number of leading zeroes, and an acceptable and
155+
easily understandable result for a set of symbols that has varying numbers of
156+
leading zeroes.
157+
158+
As an example, version-sorting will sort the following strings in the order
159+
given:
160+
- `_ZYWX`
161+
- `u_zzz`
162+
- `u8`
163+
- `u16`
164+
- `u32`
165+
- `u64`
166+
- `u128`
167+
- `u256`
168+
- `ua`
169+
- `usize`
170+
- `uz`
171+
- `v000`
172+
- `v00`
173+
- `v0`
174+
- `v0s`
175+
- `v00t`
176+
- `v0u`
177+
- `v001`
178+
- `v01`
179+
- `v1`
180+
- `v009`
181+
- `v09`
182+
- `v9`
183+
- `v010`
184+
- `v10`
185+
- `w005s09t`
186+
- `w5s009t`
187+
- `x64`
188+
- `x86`
189+
- `x86_32`
190+
- `x86_64`
191+
- `x86_128`
192+
- `x87`
193+
- `Z_YWX`
194+
- `ZY_WX`
195+
- `ZYW_X`
196+
- `ZYWX`
197+
- `ZYWX_`
198+
115199
### [Module-level items](items.md)
116200

117201
### [Statements](statements.md)

src/doc/style-guide/src/cargo.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ Put a blank line between the last key-value pair in a section and the header of
88
the next section. Do not place a blank line between section headers and the
99
key-value pairs in that section, or between key-value pairs in a section.
1010

11-
Sort key names alphabetically within each section, with the exception of the
11+
Version-sort key names within each section, with the exception of the
1212
`[package]` section. Put the `[package]` section at the top of the file; put
1313
the `name` and `version` keys in that order at the top of that section,
14-
followed by the remaining keys other than `description` in alphabetical order,
15-
followed by the `description` at the end of that section.
14+
followed by the remaining keys other than `description` in order, followed by
15+
the `description` at the end of that section.
1616

1717
Don't use quotes around any standard key names; use bare keys. Only use quoted
1818
keys for non-standard keys whose names require them, and avoid introducing such

src/doc/style-guide/src/editions.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ history of the style guide. Notable changes in the Rust 2024 style edition
3737
include:
3838

3939
- Miscellaneous `rustfmt` bugfixes.
40+
- Use version-sort (sort `x8`, `x16`, `x32`, `x64`, `x128` in that order).
41+
- Change "ASCIIbetical" sort to Unicode-aware "non-lowercase before lowercase".
4042

4143
## Rust 2015/2018/2021 style edition
4244

src/doc/style-guide/src/items.md

+15-8
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ an item appears at module level or within another item.
99
alphabetically.
1010

1111
`use` statements, and module *declarations* (`mod foo;`, not `mod { ... }`)
12-
must come before other items. Put imports before module declarations. Sort each
13-
alphabetically, except that `self` and `super` must come before any other
12+
must come before other items. Put imports before module declarations.
13+
Version-sort each, except that `self` and `super` must come before any other
1414
names.
1515

1616
Don't automatically move module declarations annotated with `#[macro_use]`,
@@ -467,8 +467,10 @@ foo::{
467467
A *group* of imports is a set of imports on the same or sequential lines. One or
468468
more blank lines or other items (e.g., a function) separate groups of imports.
469469

470-
Within a group of imports, imports must be sorted ASCIIbetically (uppercase
471-
before lowercase). Groups of imports must not be merged or re-ordered.
470+
Within a group of imports, imports must be version-sorted, except that
471+
non-lowercase characters (characters that can start an `UpperCamelCase`
472+
identifier) must be sorted before lowercase characters. Groups of imports must
473+
not be merged or re-ordered.
472474

473475
E.g., input:
474476

@@ -495,10 +497,15 @@ re-ordering.
495497

496498
### Ordering list import
497499

498-
Names in a list import must be sorted ASCIIbetically, but with `self` and
499-
`super` first, and groups and glob imports last. This applies recursively. For
500-
example, `a::*` comes before `b::a` but `a::b` comes before `a::*`. E.g.,
501-
`use foo::bar::{a, b::c, b::d, b::d::{x, y, z}, b::{self, r, s}};`.
500+
Names in a list import must be version-sorted, except that:
501+
- `self` and `super` always come first if present,
502+
- non-lowercase characters (characters that can start an `UpperCamelCase`
503+
identifier) must be sorted before lowercase characters, and
504+
- groups and glob imports always come last if present.
505+
506+
This applies recursively. For example, `a::*` comes before `b::a` but `a::b`
507+
comes before `a::*`. E.g., `use foo::bar::{a, b::c, b::d, b::d::{x, y, z},
508+
b::{self, r, s}};`.
502509

503510
### Normalisation
504511

0 commit comments

Comments
 (0)