Skip to content

Commit 1f057ed

Browse files
committed
make tidy-alphabetical use a natural sort
1 parent 4455c89 commit 1f057ed

File tree

7 files changed

+143
-31
lines changed

7 files changed

+143
-31
lines changed

compiler/rustc_target/src/target_features.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,6 @@ static AARCH64_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
217217
("flagm2", Unstable(sym::aarch64_unstable_target_feature), &[]),
218218
// We forbid directly toggling just `fp-armv8`; it must be toggled with `neon`.
219219
("fp-armv8", Stability::Forbidden { reason: "Rust ties `fp-armv8` to `neon`" }, &[]),
220-
// FEAT_FP16
221-
// Rust ties FP and Neon: https://github.com/rust-lang/rust/pull/91608
222-
("fp16", Stable, &["neon"]),
223220
// FEAT_FP8
224221
("fp8", Unstable(sym::aarch64_unstable_target_feature), &["faminmax", "lut", "bf16"]),
225222
// FEAT_FP8DOT2
@@ -228,6 +225,9 @@ static AARCH64_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
228225
("fp8dot4", Unstable(sym::aarch64_unstable_target_feature), &["fp8fma"]),
229226
// FEAT_FP8FMA
230227
("fp8fma", Unstable(sym::aarch64_unstable_target_feature), &["fp8"]),
228+
// FEAT_FP16
229+
// Rust ties FP and Neon: https://github.com/rust-lang/rust/pull/91608
230+
("fp16", Stable, &["neon"]),
231231
// FEAT_FRINTTS
232232
("frintts", Stable, &[]),
233233
// FEAT_HBC
@@ -241,10 +241,10 @@ static AARCH64_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
241241
("lor", Stable, &[]),
242242
// FEAT_LSE
243243
("lse", Stable, &[]),
244-
// FEAT_LSE128
245-
("lse128", Unstable(sym::aarch64_unstable_target_feature), &["lse"]),
246244
// FEAT_LSE2
247245
("lse2", Unstable(sym::aarch64_unstable_target_feature), &[]),
246+
// FEAT_LSE128
247+
("lse128", Unstable(sym::aarch64_unstable_target_feature), &["lse"]),
248248
// FEAT_LUT
249249
("lut", Unstable(sym::aarch64_unstable_target_feature), &[]),
250250
// FEAT_MOPS
@@ -293,14 +293,14 @@ static AARCH64_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
293293
("sme", Unstable(sym::aarch64_unstable_target_feature), &["bf16"]),
294294
// FEAT_SME_B16B16
295295
("sme-b16b16", Unstable(sym::aarch64_unstable_target_feature), &["bf16", "sme2", "sve-b16b16"]),
296-
// FEAT_SME_F16F16
297-
("sme-f16f16", Unstable(sym::aarch64_unstable_target_feature), &["sme2"]),
298-
// FEAT_SME_F64F64
299-
("sme-f64f64", Unstable(sym::aarch64_unstable_target_feature), &["sme"]),
300296
// FEAT_SME_F8F16
301297
("sme-f8f16", Unstable(sym::aarch64_unstable_target_feature), &["sme-f8f32"]),
302298
// FEAT_SME_F8F32
303299
("sme-f8f32", Unstable(sym::aarch64_unstable_target_feature), &["sme2", "fp8"]),
300+
// FEAT_SME_F16F16
301+
("sme-f16f16", Unstable(sym::aarch64_unstable_target_feature), &["sme2"]),
302+
// FEAT_SME_F64F64
303+
("sme-f64f64", Unstable(sym::aarch64_unstable_target_feature), &["sme"]),
304304
// FEAT_SME_FA64
305305
("sme-fa64", Unstable(sym::aarch64_unstable_target_feature), &["sme", "sve2"]),
306306
// FEAT_SME_I16I64
@@ -386,15 +386,16 @@ static X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
386386
("amx-avx512", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
387387
("amx-bf16", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
388388
("amx-complex", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
389-
("amx-fp16", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
390389
("amx-fp8", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
390+
("amx-fp16", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
391391
("amx-int8", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
392392
("amx-movrs", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
393393
("amx-tf32", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
394394
("amx-tile", Unstable(sym::x86_amx_intrinsics), &[]),
395395
("amx-transpose", Unstable(sym::x86_amx_intrinsics), &["amx-tile"]),
396396
("apxf", Unstable(sym::apx_target_feature), &[]),
397397
("avx", Stable, &["sse4.2"]),
398+
("avx2", Stable, &["avx"]),
398399
(
399400
"avx10.1",
400401
Unstable(sym::avx10_target_feature),
@@ -415,7 +416,6 @@ static X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
415416
],
416417
),
417418
("avx10.2", Unstable(sym::avx10_target_feature), &["avx10.1"]),
418-
("avx2", Stable, &["avx"]),
419419
("avx512bf16", Stable, &["avx512bw"]),
420420
("avx512bitalg", Stable, &["avx512bw"]),
421421
("avx512bw", Stable, &["avx512f"]),
@@ -433,8 +433,8 @@ static X86_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
433433
("avxifma", Stable, &["avx2"]),
434434
("avxneconvert", Stable, &["avx2"]),
435435
("avxvnni", Stable, &["avx2"]),
436-
("avxvnniint16", Stable, &["avx2"]),
437436
("avxvnniint8", Stable, &["avx2"]),
437+
("avxvnniint16", Stable, &["avx2"]),
438438
("bmi1", Stable, &[]),
439439
("bmi2", Stable, &[]),
440440
("cmpxchg16b", Stable, &[]),
@@ -493,12 +493,12 @@ static POWERPC_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
493493
("altivec", Unstable(sym::powerpc_target_feature), &[]),
494494
("msync", Unstable(sym::powerpc_target_feature), &[]),
495495
("partword-atomics", Unstable(sym::powerpc_target_feature), &[]),
496-
("power10-vector", Unstable(sym::powerpc_target_feature), &["power9-vector"]),
497496
("power8-altivec", Unstable(sym::powerpc_target_feature), &["altivec"]),
498497
("power8-crypto", Unstable(sym::powerpc_target_feature), &["power8-altivec"]),
499498
("power8-vector", Unstable(sym::powerpc_target_feature), &["vsx", "power8-altivec"]),
500499
("power9-altivec", Unstable(sym::powerpc_target_feature), &["power8-altivec"]),
501500
("power9-vector", Unstable(sym::powerpc_target_feature), &["power8-vector", "power9-altivec"]),
501+
("power10-vector", Unstable(sym::powerpc_target_feature), &["power9-vector"]),
502502
("quadword-atomics", Unstable(sym::powerpc_target_feature), &[]),
503503
("vsx", Unstable(sym::powerpc_target_feature), &["altivec"]),
504504
// tidy-alphabetical-end
@@ -530,8 +530,8 @@ static RISCV_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
530530
("unaligned-scalar-mem", Unstable(sym::riscv_target_feature), &[]),
531531
("unaligned-vector-mem", Unstable(sym::riscv_target_feature), &[]),
532532
("v", Unstable(sym::riscv_target_feature), &["zvl128b", "zve64d"]),
533-
("za128rs", Unstable(sym::riscv_target_feature), &[]),
534533
("za64rs", Unstable(sym::riscv_target_feature), &["za128rs"]), // Za64rs ⊃ Za128rs
534+
("za128rs", Unstable(sym::riscv_target_feature), &[]),
535535
("zaamo", Unstable(sym::riscv_target_feature), &[]),
536536
("zabha", Unstable(sym::riscv_target_feature), &["zaamo"]),
537537
("zacas", Unstable(sym::riscv_target_feature), &["zaamo"]),
@@ -608,18 +608,18 @@ static RISCV_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
608608
("zvksg", Unstable(sym::riscv_target_feature), &["zvks", "zvkg"]),
609609
("zvksh", Unstable(sym::riscv_target_feature), &["zve32x"]),
610610
("zvkt", Unstable(sym::riscv_target_feature), &[]),
611-
("zvl1024b", Unstable(sym::riscv_target_feature), &["zvl512b"]),
611+
("zvl32b", Unstable(sym::riscv_target_feature), &[]),
612+
("zvl64b", Unstable(sym::riscv_target_feature), &["zvl32b"]),
612613
("zvl128b", Unstable(sym::riscv_target_feature), &["zvl64b"]),
613-
("zvl16384b", Unstable(sym::riscv_target_feature), &["zvl8192b"]),
614-
("zvl2048b", Unstable(sym::riscv_target_feature), &["zvl1024b"]),
615614
("zvl256b", Unstable(sym::riscv_target_feature), &["zvl128b"]),
616-
("zvl32768b", Unstable(sym::riscv_target_feature), &["zvl16384b"]),
617-
("zvl32b", Unstable(sym::riscv_target_feature), &[]),
618-
("zvl4096b", Unstable(sym::riscv_target_feature), &["zvl2048b"]),
619615
("zvl512b", Unstable(sym::riscv_target_feature), &["zvl256b"]),
620-
("zvl64b", Unstable(sym::riscv_target_feature), &["zvl32b"]),
621-
("zvl65536b", Unstable(sym::riscv_target_feature), &["zvl32768b"]),
616+
("zvl1024b", Unstable(sym::riscv_target_feature), &["zvl512b"]),
617+
("zvl2048b", Unstable(sym::riscv_target_feature), &["zvl1024b"]),
618+
("zvl4096b", Unstable(sym::riscv_target_feature), &["zvl2048b"]),
622619
("zvl8192b", Unstable(sym::riscv_target_feature), &["zvl4096b"]),
620+
("zvl16384b", Unstable(sym::riscv_target_feature), &["zvl8192b"]),
621+
("zvl32768b", Unstable(sym::riscv_target_feature), &["zvl16384b"]),
622+
("zvl65536b", Unstable(sym::riscv_target_feature), &["zvl32768b"]),
623623
// tidy-alphabetical-end
624624
];
625625

@@ -646,13 +646,13 @@ const BPF_FEATURES: &[(&str, Stability, ImpliedFeatures)] =
646646

647647
static CSKY_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
648648
// tidy-alphabetical-start
649-
("10e60", Unstable(sym::csky_target_feature), &["7e10"]),
650649
("2e3", Unstable(sym::csky_target_feature), &["e2"]),
651650
("3e3r1", Unstable(sym::csky_target_feature), &[]),
652651
("3e3r2", Unstable(sym::csky_target_feature), &["3e3r1", "doloop"]),
653652
("3e3r3", Unstable(sym::csky_target_feature), &["doloop"]),
654653
("3e7", Unstable(sym::csky_target_feature), &["2e3"]),
655654
("7e10", Unstable(sym::csky_target_feature), &["3e7"]),
655+
("10e60", Unstable(sym::csky_target_feature), &["7e10"]),
656656
("cache", Unstable(sym::csky_target_feature), &[]),
657657
("doloop", Unstable(sym::csky_target_feature), &[]),
658658
("dsp1e2", Unstable(sym::csky_target_feature), &[]),

library/core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@
148148
#![feature(doc_cfg_hide)]
149149
#![feature(doc_notable_trait)]
150150
#![feature(extern_types)]
151-
#![feature(f128)]
152151
#![feature(f16)]
152+
#![feature(f128)]
153153
#![feature(freeze_impls)]
154154
#![feature(fundamental)]
155155
#![feature(generic_arg_infer)]

library/coretests/tests/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
#![feature(exact_size_is_empty)]
3232
#![feature(extend_one)]
3333
#![feature(extern_types)]
34-
#![feature(f128)]
3534
#![feature(f16)]
35+
#![feature(f128)]
3636
#![feature(float_algebraic)]
3737
#![feature(float_gamma)]
3838
#![feature(float_minimum_maximum)]

library/std/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@
296296
#![feature(doc_notable_trait)]
297297
#![feature(dropck_eyepatch)]
298298
#![feature(extended_varargs_abi_support)]
299-
#![feature(f128)]
300299
#![feature(f16)]
300+
#![feature(f128)]
301301
#![feature(ffi_const)]
302302
#![feature(formatting_options)]
303303
#![feature(if_let_guard)]

library/std/tests/run-time-detect.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ fn aarch64_linux() {
5757
println!("fhm: {}", is_aarch64_feature_detected!("fhm"));
5858
println!("flagm2: {}", is_aarch64_feature_detected!("flagm2"));
5959
println!("flagm: {}", is_aarch64_feature_detected!("flagm"));
60-
println!("fp16: {}", is_aarch64_feature_detected!("fp16"));
6160
println!("fp8: {}", is_aarch64_feature_detected!("fp8"));
6261
println!("fp8dot2: {}", is_aarch64_feature_detected!("fp8dot2"));
6362
println!("fp8dot4: {}", is_aarch64_feature_detected!("fp8dot4"));
6463
println!("fp8fma: {}", is_aarch64_feature_detected!("fp8fma"));
64+
println!("fp16: {}", is_aarch64_feature_detected!("fp16"));
6565
println!("fpmr: {}", is_aarch64_feature_detected!("fpmr"));
6666
println!("frintts: {}", is_aarch64_feature_detected!("frintts"));
6767
println!("hbc: {}", is_aarch64_feature_detected!("hbc"));
6868
println!("i8mm: {}", is_aarch64_feature_detected!("i8mm"));
6969
println!("jsconv: {}", is_aarch64_feature_detected!("jsconv"));
70-
println!("lse128: {}", is_aarch64_feature_detected!("lse128"));
7170
println!("lse2: {}", is_aarch64_feature_detected!("lse2"));
71+
println!("lse128: {}", is_aarch64_feature_detected!("lse128"));
7272
println!("lse: {}", is_aarch64_feature_detected!("lse"));
7373
println!("lut: {}", is_aarch64_feature_detected!("lut"));
7474
println!("mops: {}", is_aarch64_feature_detected!("mops"));
@@ -87,10 +87,10 @@ fn aarch64_linux() {
8787
println!("sha3: {}", is_aarch64_feature_detected!("sha3"));
8888
println!("sm4: {}", is_aarch64_feature_detected!("sm4"));
8989
println!("sme-b16b16: {}", is_aarch64_feature_detected!("sme-b16b16"));
90-
println!("sme-f16f16: {}", is_aarch64_feature_detected!("sme-f16f16"));
91-
println!("sme-f64f64: {}", is_aarch64_feature_detected!("sme-f64f64"));
9290
println!("sme-f8f16: {}", is_aarch64_feature_detected!("sme-f8f16"));
9391
println!("sme-f8f32: {}", is_aarch64_feature_detected!("sme-f8f32"));
92+
println!("sme-f16f16: {}", is_aarch64_feature_detected!("sme-f16f16"));
93+
println!("sme-f64f64: {}", is_aarch64_feature_detected!("sme-f64f64"));
9494
println!("sme-fa64: {}", is_aarch64_feature_detected!("sme-fa64"));
9595
println!("sme-i16i64: {}", is_aarch64_feature_detected!("sme-i16i64"));
9696
println!("sme-lutv2: {}", is_aarch64_feature_detected!("sme-lutv2"));

src/tools/tidy/src/alphabetical.rs

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//! If a line ends with an opening delimiter, we effectively join the following line to it before
2020
//! checking it. E.g. `foo(\nbar)` is treated like `foo(bar)`.
2121
22+
use std::cmp::Ordering;
2223
use std::fmt::Display;
2324
use std::path::Path;
2425

@@ -101,7 +102,7 @@ fn check_section<'a>(
101102

102103
let prev_line_trimmed_lowercase = prev_line.trim_start_matches(' ').to_lowercase();
103104

104-
if trimmed_line.to_lowercase() < prev_line_trimmed_lowercase {
105+
if cmp_natural(&trimmed_line.to_lowercase(), &prev_line_trimmed_lowercase).is_lt() {
105106
tidy_error_ext!(err, bad, "{file}:{}: line not in alphabetical order", idx + 1);
106107
}
107108

@@ -111,6 +112,71 @@ fn check_section<'a>(
111112
tidy_error_ext!(err, bad, "{file}: reached end of file expecting `{END_MARKER}`")
112113
}
113114

115+
fn consume_numeric_prefix<I: Iterator<Item = char>>(it: &mut std::iter::Peekable<I>) -> String {
116+
let mut result = String::new();
117+
118+
while let Some(&c) = it.peek() {
119+
if c.is_numeric() {
120+
result.push(c);
121+
it.next();
122+
continue;
123+
}
124+
125+
break;
126+
}
127+
128+
result
129+
}
130+
131+
/// Compare the lines using the natural sort order.
132+
///
133+
/// The natural sort order is equal to alphabetical order, except that single- and multi-digit
134+
/// numbers are treated atomically, i.e., as if they were a single character, and compared
135+
/// between themselves by their actual numerical values.
136+
///
137+
/// For example, according to this function, these lines are sorted correctly:
138+
///
139+
/// - foo2
140+
/// - foo10
141+
///
142+
/// See also https://en.wikipedia.org/wiki/Natural_sort_order.
143+
fn cmp_natural(a: &str, b: &str) -> Ordering {
144+
let mut it1 = a.chars().peekable();
145+
let mut it2 = b.chars().peekable();
146+
147+
while let (Some(x), Some(y)) = (it1.peek(), it2.peek()) {
148+
match (x.is_numeric(), y.is_numeric()) {
149+
(true, true) => {
150+
let num1: String = consume_numeric_prefix(it1.by_ref());
151+
let num2: String = consume_numeric_prefix(it2.by_ref());
152+
153+
// Assume a u64 has sufficient digits in practice.
154+
let int1: u64 = num1.parse().unwrap();
155+
let int2: u64 = num2.parse().unwrap();
156+
157+
// Compare the numeric value, so `2 < 10` even though `"2" > "10"`.
158+
match Ord::cmp(&int1, &int2) {
159+
Ordering::Equal => continue,
160+
different => return different,
161+
}
162+
}
163+
(true, false) | (false, true) => return x.cmp(y),
164+
(false, false) => match x.cmp(y) {
165+
Ordering::Equal => {
166+
it1.next();
167+
it2.next();
168+
continue;
169+
}
170+
different => return different,
171+
},
172+
}
173+
}
174+
175+
// At least one of the iterators is empty at this point, and an empty string is smaller
176+
// than a non-empty string, mapping nicely onto the `Ord` instance of `Option`.
177+
Ord::cmp(&it1.next(), &it2.next())
178+
}
179+
114180
fn check_lines<'a>(
115181
file: &impl Display,
116182
mut lines: impl Iterator<Item = (usize, &'a str)>,

src/tools/tidy/src/alphabetical/tests.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::str::from_utf8;
33

44
use super::*;
55

6+
#[track_caller]
67
fn test(lines: &str, name: &str, expected_msg: &str, expected_bad: bool) {
78
let mut actual_msg = Vec::new();
89
let mut actual_bad = false;
@@ -15,10 +16,12 @@ fn test(lines: &str, name: &str, expected_msg: &str, expected_bad: bool) {
1516
assert_eq!(expected_bad, actual_bad);
1617
}
1718

19+
#[track_caller]
1820
fn good(lines: &str) {
1921
test(lines, "good", "", false);
2022
}
2123

24+
#[track_caller]
2225
fn bad(lines: &str, expected_msg: &str) {
2326
test(lines, "bad", expected_msg, true);
2427
}
@@ -187,3 +190,46 @@ fn test_double_end() {
187190
";
188191
bad(lines, "bad:5 found `tidy-alphabetical-end` expecting `tidy-alphabetical-start`");
189192
}
193+
194+
#[test]
195+
fn test_numeric_good() {
196+
let lines = "\
197+
# tidy-alphabetical-start
198+
fp-armv8
199+
fp16
200+
201+
item1
202+
item2
203+
item10
204+
205+
v5te
206+
v6
207+
v6k
208+
v6t2
209+
210+
zve64d
211+
zve64f
212+
# tidy-alphabetical-end
213+
";
214+
good(lines);
215+
}
216+
217+
#[test]
218+
fn test_numeric_bad() {
219+
let lines = "\
220+
# tidy-alphabetical-start
221+
item1
222+
item10
223+
item2
224+
# tidy-alphabetical-end
225+
";
226+
bad(lines, "bad:4: line not in alphabetical order");
227+
228+
let lines = "\
229+
# tidy-alphabetical-start
230+
zve64f
231+
zve64d
232+
# tidy-alphabetical-end
233+
";
234+
bad(lines, "bad:3: line not in alphabetical order");
235+
}

0 commit comments

Comments
 (0)