Skip to content

Commit 8d5a10f

Browse files
authored
Rollup merge of rust-lang#128465 - GrigorenkoPV:128200, r=estebank
Some `const { }` asserts for rust-lang#128200 The correctness of code in rust-lang#128200 relies on an array being sorted (so that it can be used in binary search later), which is currently enforced with `// tidy-alphabetical` (and characters being written in `\u{XXXX}` form), as well as lack of duplicate entries with conflicting keys, which is not currently enforced. This PR changes it to using a `const{ }` assertion (and also checks for duplicate entries). Sadly, we cannot use the recently-stabilized `is_sorted_by_key` here, because it is not const (but it would not allow us to check for uniqueness anyways). Instead, let's write a manual loop. Alternative approach (perfect hash function): rust-lang#128463 r? `@ghost`
2 parents 9337f7a + 15982b2 commit 8d5a10f

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

compiler/rustc_errors/src/emitter.rs

+15-7
Original file line numberDiff line numberDiff line change
@@ -2595,9 +2595,7 @@ fn num_decimal_digits(num: usize) -> usize {
25952595

25962596
// We replace some characters so the CLI output is always consistent and underlines aligned.
25972597
// Keep the following list in sync with `rustc_span::char_width`.
2598-
// ATTENTION: keep lexicografically sorted so that the binary search will work
25992598
const OUTPUT_REPLACEMENTS: &[(char, &str)] = &[
2600-
// tidy-alphabetical-start
26012599
// In terminals without Unicode support the following will be garbled, but in *all* terminals
26022600
// the underlying codepoint will be as well. We could gate this replacement behind a "unicode
26032601
// support" gate.
@@ -2610,7 +2608,7 @@ const OUTPUT_REPLACEMENTS: &[(char, &str)] = &[
26102608
('\u{0006}', "␆"),
26112609
('\u{0007}', "␇"),
26122610
('\u{0008}', "␈"),
2613-
('\u{0009}', " "), // We do our own tab replacement
2611+
('\t', " "), // We do our own tab replacement
26142612
('\u{000b}', "␋"),
26152613
('\u{000c}', "␌"),
26162614
('\u{000d}', "␍"),
@@ -2643,13 +2641,23 @@ const OUTPUT_REPLACEMENTS: &[(char, &str)] = &[
26432641
('\u{2067}', "�"),
26442642
('\u{2068}', "�"),
26452643
('\u{2069}', "�"),
2646-
// tidy-alphabetical-end
26472644
];
26482645

26492646
fn normalize_whitespace(s: &str) -> String {
2650-
// Scan the input string for a character in the ordered table above. If it's present, replace
2651-
// it with it's alternative string (it can be more than 1 char!). Otherwise, retain the input
2652-
// char. At the end, allocate all chars into a string in one operation.
2647+
const {
2648+
let mut i = 1;
2649+
while i < OUTPUT_REPLACEMENTS.len() {
2650+
assert!(
2651+
OUTPUT_REPLACEMENTS[i - 1].0 < OUTPUT_REPLACEMENTS[i].0,
2652+
"The OUTPUT_REPLACEMENTS array must be sorted (for binary search to work) \
2653+
and must contain no duplicate entries"
2654+
);
2655+
i += 1;
2656+
}
2657+
}
2658+
// Scan the input string for a character in the ordered table above.
2659+
// If it's present, replace it with its alternative string (it can be more than 1 char!).
2660+
// Otherwise, retain the input char.
26532661
s.chars().fold(String::with_capacity(s.len()), |mut s, c| {
26542662
match OUTPUT_REPLACEMENTS.binary_search_by_key(&c, |(k, _)| *k) {
26552663
Ok(i) => s.push_str(OUTPUT_REPLACEMENTS[i].1),

0 commit comments

Comments
 (0)