Skip to content

Commit 43a0686

Browse files
committed
Auto merge of rust-lang#115253 - donno2048:patch-1, r=albertlarsan68
Implement `PROBLEMATIC_CONSTS` generalization You forgot that `A≈4`, `B≈8`, and `E≈3` and some more constants. The new `PROBLEMATIC_CONSTS` was generated using this code: ```py from functools import reduce def generate_problems(consts: list, letter_digit: dict): for const in consts: problem = reduce(lambda string, rep: string.replace(*reversed(rep)), ['%X' % const, *letter_digit.items()]) indexes = [index for index, c in enumerate(problem) if c in letter_digit.keys()] for i in range(1 << len(indexes)): yield int(''.join(letter_digit[c] if index in indexes and (i >> indexes.index(index)) & 1 else c for index, c in enumerate(problem)), 0x10) problems = generate_problems( [ # Old PROBLEMATIC_CONSTS: 184594741, 2880289470, 2881141438, 2965027518, 2976579765, 3203381950, 3405691582, 3405697037, 3735927486, 3735932941, 4027431614, 4276992702, # More of my own: 195934910, 252707358, 762133, 179681982, 173390526 ], { 'A': '4', 'B': '8', 'E': '3', } ) # print(list(problems)) # won't use that to print formatted from itertools import islice while len(cur_problems := list(islice(problems, 8))): print(' ', end='') print(*cur_problems, sep=', ', end='') print(',') ```
2 parents c4f112a + e2ab540 commit 43a0686

File tree

4 files changed

+74
-7
lines changed

4 files changed

+74
-7
lines changed

Diff for: Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -5598,6 +5598,7 @@ dependencies = [
55985598
"lazy_static",
55995599
"miropt-test-tools",
56005600
"regex",
5601+
"rustc-hash",
56015602
"semver",
56025603
"termcolor",
56035604
"walkdir",

Diff for: src/tools/tidy/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ walkdir = "2"
1313
ignore = "0.4.18"
1414
semver = "1.0"
1515
termcolor = "1.1.3"
16+
rustc-hash = "1.1.0"
1617

1718
[[bin]]
1819
name = "rust-tidy"
1920
path = "src/main.rs"
21+
test = false

Diff for: src/tools/tidy/src/style.rs

+54-7
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919

2020
use crate::walk::{filter_dirs, walk};
2121
use regex::{Regex, RegexSet};
22+
use rustc_hash::FxHashMap;
2223
use std::{ffi::OsStr, path::Path};
2324

25+
#[cfg(test)]
26+
mod tests;
27+
2428
/// Error code markdown is restricted to 80 columns because they can be
2529
/// displayed on the console with --example.
2630
const ERROR_CODE_COLS: usize = 80;
@@ -65,12 +69,56 @@ const ANNOTATIONS_TO_IGNORE: &[&str] = &[
6569
"//@ normalize-stderr-test",
6670
];
6771

72+
fn generate_problems<'a>(
73+
consts: &'a [u32],
74+
letter_digit: &'a FxHashMap<char, char>,
75+
) -> impl Iterator<Item = u32> + 'a {
76+
consts.iter().flat_map(move |const_value| {
77+
let problem =
78+
letter_digit.iter().fold(format!("{:X}", const_value), |acc, (key, value)| {
79+
acc.replace(&value.to_string(), &key.to_string())
80+
});
81+
let indexes: Vec<usize> = problem
82+
.chars()
83+
.enumerate()
84+
.filter_map(|(index, c)| if letter_digit.contains_key(&c) { Some(index) } else { None })
85+
.collect();
86+
(0..1 << indexes.len()).map(move |i| {
87+
u32::from_str_radix(
88+
&problem
89+
.chars()
90+
.enumerate()
91+
.map(|(index, c)| {
92+
if let Some(pos) = indexes.iter().position(|&x| x == index) {
93+
if (i >> pos) & 1 == 1 { letter_digit[&c] } else { c }
94+
} else {
95+
c
96+
}
97+
})
98+
.collect::<String>(),
99+
0x10,
100+
)
101+
.unwrap()
102+
})
103+
})
104+
}
105+
68106
// Intentionally written in decimal rather than hex
69-
const PROBLEMATIC_CONSTS: &[u32] = &[
107+
const ROOT_PROBLEMATIC_CONSTS: &[u32] = &[
70108
184594741, 2880289470, 2881141438, 2965027518, 2976579765, 3203381950, 3405691582, 3405697037,
71-
3735927486, 3735932941, 4027431614, 4276992702,
109+
3735927486, 3735932941, 4027431614, 4276992702, 195934910, 252707358, 762133, 179681982,
110+
173390526, 721077,
72111
];
73112

113+
fn generate_problematic_strings(
114+
consts: &[u32],
115+
letter_digit: &FxHashMap<char, char>,
116+
) -> Vec<String> {
117+
generate_problems(consts, letter_digit)
118+
.flat_map(|v| vec![v.to_string(), format!("{:x}", v), format!("{:X}", v)])
119+
.collect()
120+
}
121+
74122
const INTERNAL_COMPILER_DOCS_LINE: &str = "#### This error code is internal to the compiler and will not be emitted with normal Rust code.";
75123

76124
/// Parser states for `line_is_url`.
@@ -267,11 +315,10 @@ pub fn check(path: &Path, bad: &mut bool) {
267315
// We only check CSS files in rustdoc.
268316
path.extension().map_or(false, |e| e == "css") && !is_in(path, "src", "librustdoc")
269317
}
270-
271-
let problematic_consts_strings: Vec<String> = (PROBLEMATIC_CONSTS.iter().map(u32::to_string))
272-
.chain(PROBLEMATIC_CONSTS.iter().map(|v| format!("{:x}", v)))
273-
.chain(PROBLEMATIC_CONSTS.iter().map(|v| format!("{:X}", v)))
274-
.collect();
318+
let problematic_consts_strings = generate_problematic_strings(
319+
ROOT_PROBLEMATIC_CONSTS,
320+
&[('A', '4'), ('B', '8'), ('E', '3')].iter().cloned().collect(),
321+
);
275322
let problematic_regex = RegexSet::new(problematic_consts_strings.as_slice()).unwrap();
276323

277324
walk(path, skip, &mut |entry, contents| {

Diff for: src/tools/tidy/src/style/tests.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use super::*;
2+
3+
#[test]
4+
fn test_generate_problematic_strings() {
5+
let problematic_regex = RegexSet::new(
6+
generate_problematic_strings(
7+
ROOT_PROBLEMATIC_CONSTS,
8+
&[('A', '4'), ('B', '8'), ('E', '3'), ('0', 'F')].iter().cloned().collect(), // use "futile" F intentionally
9+
)
10+
.as_slice(),
11+
)
12+
.unwrap();
13+
assert!(problematic_regex.is_match("786357")); // check with no "decimal" hex digits - converted to integer
14+
assert!(problematic_regex.is_match("589701")); // check with "decimal" replacements - converted to integer
15+
assert!(problematic_regex.is_match("8FF85")); // check for hex display
16+
assert!(!problematic_regex.is_match("1193046")); // check for non-matching value
17+
}

0 commit comments

Comments
 (0)