Skip to content

Commit d7c0b4f

Browse files
authored
Rollup merge of #93019 - 5225225:uppercase-suffix, r=wesleywiser
If an integer is entered with an upper-case base prefix (0Xbeef, 0O755, 0B1010), suggest to make it lowercase The current error for this case isn't really great, it just complains about the whole thing past the `0` being an invalid suffix.
2 parents 5159c01 + ec3b711 commit d7c0b4f

File tree

4 files changed

+277
-0
lines changed

4 files changed

+277
-0
lines changed

compiler/rustc_parse/src/parser/expr.rs

+25
Original file line numberDiff line numberDiff line change
@@ -1700,6 +1700,19 @@ impl<'a> Parser<'a> {
17001700
s.len() > 1 && s.starts_with(first_chars) && s[1..].chars().all(|c| c.is_ascii_digit())
17011701
}
17021702

1703+
// Try to lowercase the prefix if it's a valid base prefix.
1704+
fn fix_base_capitalisation(s: &str) -> Option<String> {
1705+
if let Some(stripped) = s.strip_prefix("B") {
1706+
Some(format!("0b{stripped}"))
1707+
} else if let Some(stripped) = s.strip_prefix("O") {
1708+
Some(format!("0o{stripped}"))
1709+
} else if let Some(stripped) = s.strip_prefix("X") {
1710+
Some(format!("0x{stripped}"))
1711+
} else {
1712+
None
1713+
}
1714+
}
1715+
17031716
let token::Lit { kind, suffix, .. } = lit;
17041717
match err {
17051718
// `NotLiteral` is not an error by itself, so we don't report
@@ -1724,6 +1737,18 @@ impl<'a> Parser<'a> {
17241737
self.struct_span_err(span, &msg)
17251738
.help("valid widths are 8, 16, 32, 64 and 128")
17261739
.emit();
1740+
} else if let Some(fixed) = fix_base_capitalisation(suf) {
1741+
let msg = "invalid base prefix for number literal";
1742+
1743+
self.struct_span_err(span, &msg)
1744+
.note("base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase")
1745+
.span_suggestion(
1746+
span,
1747+
"try making the prefix lowercase",
1748+
fixed,
1749+
Applicability::MaybeIncorrect,
1750+
)
1751+
.emit();
17271752
} else {
17281753
let msg = format!("invalid suffix `{}` for number literal", suf);
17291754
self.struct_span_err(span, &msg)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// run-rustfix
2+
// Checks that integers with an uppercase base prefix (0B, 0X, 0O) have a nice error
3+
#![allow(unused_variables)]
4+
5+
fn main() {
6+
let a = 0xABCDEF;
7+
//~^ ERROR invalid base prefix for number literal
8+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
9+
//~| HELP try making the prefix lowercase
10+
//~| SUGGESTION 0xABCDEF
11+
12+
let b = 0o755;
13+
//~^ ERROR invalid base prefix for number literal
14+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
15+
//~| HELP try making the prefix lowercase
16+
//~| SUGGESTION 0o755
17+
18+
let c = 0b10101010;
19+
//~^ ERROR invalid base prefix for number literal
20+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
21+
//~| HELP try making the prefix lowercase
22+
//~| SUGGESTION 0b10101010
23+
24+
let d = 0xABC_DEF;
25+
//~^ ERROR invalid base prefix for number literal
26+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
27+
//~| HELP try making the prefix lowercase
28+
//~| SUGGESTION 0xABC_DEF
29+
30+
let e = 0o7_55;
31+
//~^ ERROR invalid base prefix for number literal
32+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
33+
//~| HELP try making the prefix lowercase
34+
//~| SUGGESTION 0o7_55
35+
36+
let f = 0b1010_1010;
37+
//~^ ERROR invalid base prefix for number literal
38+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
39+
//~| HELP try making the prefix lowercase
40+
//~| SUGGESTION 0b1010_1010
41+
42+
let g = 0xABC_DEF_u64;
43+
//~^ ERROR invalid base prefix for number literal
44+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
45+
//~| HELP try making the prefix lowercase
46+
//~| SUGGESTION 0xABC_DEF_u64
47+
48+
let h = 0o7_55_u32;
49+
//~^ ERROR invalid base prefix for number literal
50+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
51+
//~| HELP try making the prefix lowercase
52+
//~| SUGGESTION 0o7_55_u32
53+
54+
let i = 0b1010_1010_u8;
55+
//~^ ERROR invalid base prefix for number literal
56+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
57+
//~| HELP try making the prefix lowercase
58+
//~| SUGGESTION 0b1010_1010_u8
59+
//
60+
let j = 0xABCDEFu64;
61+
//~^ ERROR invalid base prefix for number literal
62+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
63+
//~| HELP try making the prefix lowercase
64+
//~| SUGGESTION 0xABCDEFu64
65+
66+
let k = 0o755u32;
67+
//~^ ERROR invalid base prefix for number literal
68+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
69+
//~| HELP try making the prefix lowercase
70+
//~| SUGGESTION 0o755u32
71+
72+
let l = 0b10101010u8;
73+
//~^ ERROR invalid base prefix for number literal
74+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
75+
//~| HELP try making the prefix lowercase
76+
//~| SUGGESTION 0b10101010u8
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// run-rustfix
2+
// Checks that integers with an uppercase base prefix (0B, 0X, 0O) have a nice error
3+
#![allow(unused_variables)]
4+
5+
fn main() {
6+
let a = 0XABCDEF;
7+
//~^ ERROR invalid base prefix for number literal
8+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
9+
//~| HELP try making the prefix lowercase
10+
//~| SUGGESTION 0xABCDEF
11+
12+
let b = 0O755;
13+
//~^ ERROR invalid base prefix for number literal
14+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
15+
//~| HELP try making the prefix lowercase
16+
//~| SUGGESTION 0o755
17+
18+
let c = 0B10101010;
19+
//~^ ERROR invalid base prefix for number literal
20+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
21+
//~| HELP try making the prefix lowercase
22+
//~| SUGGESTION 0b10101010
23+
24+
let d = 0XABC_DEF;
25+
//~^ ERROR invalid base prefix for number literal
26+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
27+
//~| HELP try making the prefix lowercase
28+
//~| SUGGESTION 0xABC_DEF
29+
30+
let e = 0O7_55;
31+
//~^ ERROR invalid base prefix for number literal
32+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
33+
//~| HELP try making the prefix lowercase
34+
//~| SUGGESTION 0o7_55
35+
36+
let f = 0B1010_1010;
37+
//~^ ERROR invalid base prefix for number literal
38+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
39+
//~| HELP try making the prefix lowercase
40+
//~| SUGGESTION 0b1010_1010
41+
42+
let g = 0XABC_DEF_u64;
43+
//~^ ERROR invalid base prefix for number literal
44+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
45+
//~| HELP try making the prefix lowercase
46+
//~| SUGGESTION 0xABC_DEF_u64
47+
48+
let h = 0O7_55_u32;
49+
//~^ ERROR invalid base prefix for number literal
50+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
51+
//~| HELP try making the prefix lowercase
52+
//~| SUGGESTION 0o7_55_u32
53+
54+
let i = 0B1010_1010_u8;
55+
//~^ ERROR invalid base prefix for number literal
56+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
57+
//~| HELP try making the prefix lowercase
58+
//~| SUGGESTION 0b1010_1010_u8
59+
//
60+
let j = 0XABCDEFu64;
61+
//~^ ERROR invalid base prefix for number literal
62+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
63+
//~| HELP try making the prefix lowercase
64+
//~| SUGGESTION 0xABCDEFu64
65+
66+
let k = 0O755u32;
67+
//~^ ERROR invalid base prefix for number literal
68+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
69+
//~| HELP try making the prefix lowercase
70+
//~| SUGGESTION 0o755u32
71+
72+
let l = 0B10101010u8;
73+
//~^ ERROR invalid base prefix for number literal
74+
//~| NOTE base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
75+
//~| HELP try making the prefix lowercase
76+
//~| SUGGESTION 0b10101010u8
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
error: invalid base prefix for number literal
2+
--> $DIR/uppercase-base-prefix.rs:6:13
3+
|
4+
LL | let a = 0XABCDEF;
5+
| ^^^^^^^^ help: try making the prefix lowercase (notice the capitalization): `0xABCDEF`
6+
|
7+
= note: base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
8+
9+
error: invalid base prefix for number literal
10+
--> $DIR/uppercase-base-prefix.rs:12:13
11+
|
12+
LL | let b = 0O755;
13+
| ^^^^^ help: try making the prefix lowercase (notice the capitalization): `0o755`
14+
|
15+
= note: base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
16+
17+
error: invalid base prefix for number literal
18+
--> $DIR/uppercase-base-prefix.rs:18:13
19+
|
20+
LL | let c = 0B10101010;
21+
| ^^^^^^^^^^ help: try making the prefix lowercase: `0b10101010`
22+
|
23+
= note: base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
24+
25+
error: invalid base prefix for number literal
26+
--> $DIR/uppercase-base-prefix.rs:24:13
27+
|
28+
LL | let d = 0XABC_DEF;
29+
| ^^^^^^^^^ help: try making the prefix lowercase (notice the capitalization): `0xABC_DEF`
30+
|
31+
= note: base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
32+
33+
error: invalid base prefix for number literal
34+
--> $DIR/uppercase-base-prefix.rs:30:13
35+
|
36+
LL | let e = 0O7_55;
37+
| ^^^^^^ help: try making the prefix lowercase (notice the capitalization): `0o7_55`
38+
|
39+
= note: base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
40+
41+
error: invalid base prefix for number literal
42+
--> $DIR/uppercase-base-prefix.rs:36:13
43+
|
44+
LL | let f = 0B1010_1010;
45+
| ^^^^^^^^^^^ help: try making the prefix lowercase: `0b1010_1010`
46+
|
47+
= note: base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
48+
49+
error: invalid base prefix for number literal
50+
--> $DIR/uppercase-base-prefix.rs:42:13
51+
|
52+
LL | let g = 0XABC_DEF_u64;
53+
| ^^^^^^^^^^^^^ help: try making the prefix lowercase (notice the capitalization): `0xABC_DEF_u64`
54+
|
55+
= note: base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
56+
57+
error: invalid base prefix for number literal
58+
--> $DIR/uppercase-base-prefix.rs:48:13
59+
|
60+
LL | let h = 0O7_55_u32;
61+
| ^^^^^^^^^^ help: try making the prefix lowercase (notice the capitalization): `0o7_55_u32`
62+
|
63+
= note: base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
64+
65+
error: invalid base prefix for number literal
66+
--> $DIR/uppercase-base-prefix.rs:54:13
67+
|
68+
LL | let i = 0B1010_1010_u8;
69+
| ^^^^^^^^^^^^^^ help: try making the prefix lowercase: `0b1010_1010_u8`
70+
|
71+
= note: base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
72+
73+
error: invalid base prefix for number literal
74+
--> $DIR/uppercase-base-prefix.rs:60:13
75+
|
76+
LL | let j = 0XABCDEFu64;
77+
| ^^^^^^^^^^^ help: try making the prefix lowercase (notice the capitalization): `0xABCDEFu64`
78+
|
79+
= note: base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
80+
81+
error: invalid base prefix for number literal
82+
--> $DIR/uppercase-base-prefix.rs:66:13
83+
|
84+
LL | let k = 0O755u32;
85+
| ^^^^^^^^ help: try making the prefix lowercase (notice the capitalization): `0o755u32`
86+
|
87+
= note: base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
88+
89+
error: invalid base prefix for number literal
90+
--> $DIR/uppercase-base-prefix.rs:72:13
91+
|
92+
LL | let l = 0B10101010u8;
93+
| ^^^^^^^^^^^^ help: try making the prefix lowercase: `0b10101010u8`
94+
|
95+
= note: base prefixes (`0xff`, `0b1010`, `0o755`) are lowercase
96+
97+
error: aborting due to 12 previous errors
98+

0 commit comments

Comments
 (0)