You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rollup merge of rust-lang#48432 - flip1995:lit_diag, r=oli-obk
Suggest type for overflowing bin/hex-literals
Fixesrust-lang#48073
For hexadecimal and binary literals, which overflow, it gives an additional note to the warning message, like in this [comment](rust-lang#48073 (comment)).
Additionally it will suggest a type (`X < Y`):
- `iX`: if literal fits in `uX` => `uX`, else => `iY`
- `-iX` => `iY`
- `uX` => `uY`
Exceptions: `isize`, `usize`. I don't think you can make a good suggestion here. The programmer has to figure it out on it's own in this case.
r? @oli-obk
LL | let error = 255i8; //~WARNING literal out of range for i8
5
+
| ^^^^^
6
+
|
7
+
= note: #[warn(overflowing_literals)] on by default
8
+
9
+
warning: literal out of range for i8
10
+
--> $DIR/type-overflow.rs:21:16
11
+
|
12
+
LL | let fail = 0b1000_0001i8; //~WARNING literal out of range for i8
13
+
| ^^^^^^^^^^^^^ help: consider using `u8` instead: `0b1000_0001u8`
14
+
|
15
+
= note: the literal `0b1000_0001i8` (decimal `129`) does not fit into an `i8` and will become `-127i8`
16
+
17
+
warning: literal out of range for i64
18
+
--> $DIR/type-overflow.rs:23:16
19
+
|
20
+
LL | let fail = 0x8000_0000_0000_0000i64; //~WARNING literal out of range for i64
21
+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `u64` instead: `0x8000_0000_0000_0000u64`
22
+
|
23
+
= note: the literal `0x8000_0000_0000_0000i64` (decimal `9223372036854775808`) does not fit into an `i64` and will become `-9223372036854775808i64`
24
+
25
+
warning: literal out of range for u32
26
+
--> $DIR/type-overflow.rs:25:16
27
+
|
28
+
LL | let fail = 0x1_FFFF_FFFFu32; //~WARNING literal out of range for u32
29
+
| ^^^^^^^^^^^^^^^^ help: consider using `u64` instead: `0x1_FFFF_FFFFu64`
30
+
|
31
+
= note: the literal `0x1_FFFF_FFFFu32` (decimal `8589934591`) does not fit into an `u32` and will become `4294967295u32`
32
+
33
+
warning: literal out of range for i128
34
+
--> $DIR/type-overflow.rs:27:22
35
+
|
36
+
LL | let fail: i128 = 0x8000_0000_0000_0000_0000_0000_0000_0000;
37
+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38
+
|
39
+
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0000` (decimal `170141183460469231731687303715884105728`) does not fit into an `i128` and will become `-170141183460469231731687303715884105728i128`
40
+
= help: consider using `u128` instead
41
+
42
+
warning: literal out of range for i32
43
+
--> $DIR/type-overflow.rs:30:16
44
+
|
45
+
LL | let fail = 0x8FFF_FFFF_FFFF_FFFE; //~WARNING literal out of range for i32
46
+
| ^^^^^^^^^^^^^^^^^^^^^
47
+
|
48
+
= note: the literal `0x8FFF_FFFF_FFFF_FFFE` (decimal `10376293541461622782`) does not fit into an `i32` and will become `-2i32`
49
+
= help: consider using `i128` instead
50
+
51
+
warning: literal out of range for i8
52
+
--> $DIR/type-overflow.rs:32:17
53
+
|
54
+
LL | let fail = -0b1111_1111i8; //~WARNING literal out of range for i8
55
+
| ^^^^^^^^^^^^^ help: consider using `i16` instead: `0b1111_1111i16`
56
+
|
57
+
= note: the literal `0b1111_1111i8` (decimal `255`) does not fit into an `i8` and will become `-1i8`
0 commit comments