Skip to content

Commit a5f164f

Browse files
committed
Auto merge of rust-lang#84017 - Smittyvb:int-literal-underscores, r=jyn514
Valid underscores in hex/octal/binary literal docs Currently hex/octal/binary literals with computed values are displayed like `0_xff_fff_fffu32`, which is invalid since underscores can't be in the middle of integer prefixes. This properly formats prefixed integers. This causes [`std::u32::MAX`](https://doc.rust-lang.org/std/u32/constant.MAX.html) to be displayed as ```rust pub const MAX: u32 = u32::MAX; // 0_xff_fff_fffu32 ``` This PR changes it to be displayed as: ```rust pub const MAX: u32 = u32::MAX; // 0xffff_ffffu32 ```
2 parents 7a0f178 + f84b4c5 commit a5f164f

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/librustdoc/clean/utils.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ use rustc_middle::ty::{self, DefIdTree, TyCtxt};
1616
use rustc_span::symbol::{kw, sym, Symbol};
1717
use std::mem;
1818

19+
#[cfg(test)]
20+
mod tests;
21+
1922
crate fn krate(cx: &mut DocContext<'_>) -> Crate {
2023
use crate::visit_lib::LibEmbargoVisitor;
2124

@@ -335,11 +338,27 @@ crate fn print_evaluated_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<String>
335338

336339
fn format_integer_with_underscore_sep(num: &str) -> String {
337340
let num_chars: Vec<_> = num.chars().collect();
338-
let num_start_index = if num_chars.get(0) == Some(&'-') { 1 } else { 0 };
341+
let mut num_start_index = if num_chars.get(0) == Some(&'-') { 1 } else { 0 };
342+
let chunk_size = match num[num_start_index..].as_bytes() {
343+
[b'0', b'b' | b'x', ..] => {
344+
num_start_index += 2;
345+
4
346+
}
347+
[b'0', b'o', ..] => {
348+
num_start_index += 2;
349+
let remaining_chars = num_chars.len() - num_start_index;
350+
if remaining_chars <= 6 {
351+
// don't add underscores to Unix permissions like 0755 or 100755
352+
return num.to_string();
353+
}
354+
3
355+
}
356+
_ => 3,
357+
};
339358

340359
num_chars[..num_start_index]
341360
.iter()
342-
.chain(num_chars[num_start_index..].rchunks(3).rev().intersperse(&['_']).flatten())
361+
.chain(num_chars[num_start_index..].rchunks(chunk_size).rev().intersperse(&['_']).flatten())
343362
.collect()
344363
}
345364

src/librustdoc/clean/utils/tests.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use super::*;
2+
3+
#[test]
4+
fn int_format_decimal() {
5+
assert_eq!(format_integer_with_underscore_sep("12345678"), "12_345_678");
6+
assert_eq!(format_integer_with_underscore_sep("123"), "123");
7+
assert_eq!(format_integer_with_underscore_sep("123459"), "123_459");
8+
assert_eq!(format_integer_with_underscore_sep("-12345678"), "-12_345_678");
9+
assert_eq!(format_integer_with_underscore_sep("-123"), "-123");
10+
assert_eq!(format_integer_with_underscore_sep("-123459"), "-123_459");
11+
}
12+
13+
#[test]
14+
fn int_format_hex() {
15+
assert_eq!(format_integer_with_underscore_sep("0xab3"), "0xab3");
16+
assert_eq!(format_integer_with_underscore_sep("0xa2345b"), "0xa2_345b");
17+
assert_eq!(format_integer_with_underscore_sep("0xa2e6345b"), "0xa2e6_345b");
18+
assert_eq!(format_integer_with_underscore_sep("-0xab3"), "-0xab3");
19+
assert_eq!(format_integer_with_underscore_sep("-0xa2345b"), "-0xa2_345b");
20+
assert_eq!(format_integer_with_underscore_sep("-0xa2e6345b"), "-0xa2e6_345b");
21+
}
22+
23+
#[test]
24+
fn int_format_binary() {
25+
assert_eq!(format_integer_with_underscore_sep("0o12345671"), "0o12_345_671");
26+
assert_eq!(format_integer_with_underscore_sep("0o123"), "0o123");
27+
assert_eq!(format_integer_with_underscore_sep("0o123451"), "0o123451");
28+
assert_eq!(format_integer_with_underscore_sep("-0o12345671"), "-0o12_345_671");
29+
assert_eq!(format_integer_with_underscore_sep("-0o123"), "-0o123");
30+
assert_eq!(format_integer_with_underscore_sep("-0o123451"), "-0o123451");
31+
}
32+
33+
#[test]
34+
fn int_format_octal() {
35+
assert_eq!(format_integer_with_underscore_sep("0b101"), "0b101");
36+
assert_eq!(format_integer_with_underscore_sep("0b101101011"), "0b1_0110_1011");
37+
assert_eq!(format_integer_with_underscore_sep("0b01101011"), "0b0110_1011");
38+
assert_eq!(format_integer_with_underscore_sep("-0b101"), "-0b101");
39+
assert_eq!(format_integer_with_underscore_sep("-0b101101011"), "-0b1_0110_1011");
40+
assert_eq!(format_integer_with_underscore_sep("-0b01101011"), "-0b0110_1011");
41+
}

0 commit comments

Comments
 (0)