Skip to content

Commit 8f9ccc5

Browse files
authored
Rollup merge of #135249 - s-cerevisiae:fix-overflowing-literals-help, r=chenyukang
Fix overflows in the implementation of `overflowing_literals` lint's help This PR fixes two overflow problems that cause the `overflowing_literals` lint to behave incorrectly in some edge cases. 1. When an integer literal is between `i128::MAX` and `u128::MAX`, an overflowing `as` cast can cause the suggested type to be overly small. It's fixed by using checked type conversion and returning `u128` when it's the only choice. (Fixes #135248) 2. When an integer literal is `i128::MIN` but is of a smaller type, an overflowing negation cause the compiler to panic in debug build. Fixed by checking the number size beforehand and `wrapping_neg`. (Fixes #131849) Edit: extracted the type conversion part into a standalone function to separate the concern of overflowing.
2 parents 87b3671 + 74e2e8b commit 8f9ccc5

File tree

3 files changed

+94
-16
lines changed

3 files changed

+94
-16
lines changed

compiler/rustc_lint/src/types/literal.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,35 @@ fn get_type_suggestion(t: Ty<'_>, val: u128, negative: bool) -> Option<&'static
204204
match t.kind() {
205205
ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize) => None,
206206
ty::Uint(_) => Some(Integer::fit_unsigned(val).uint_ty_str()),
207-
ty::Int(_) if negative => Some(Integer::fit_signed(-(val as i128)).int_ty_str()),
208-
ty::Int(int) => {
209-
let signed = Integer::fit_signed(val as i128);
210-
let unsigned = Integer::fit_unsigned(val);
211-
Some(if Some(unsigned.size().bits()) == int.bit_width() {
212-
unsigned.uint_ty_str()
207+
ty::Int(_) => {
208+
let signed = literal_to_i128(val, negative).map(Integer::fit_signed);
209+
if negative {
210+
signed.map(Integer::int_ty_str)
213211
} else {
214-
signed.int_ty_str()
215-
})
212+
let unsigned = Integer::fit_unsigned(val);
213+
Some(if let Some(signed) = signed {
214+
if unsigned.size() < signed.size() {
215+
unsigned.uint_ty_str()
216+
} else {
217+
signed.int_ty_str()
218+
}
219+
} else {
220+
unsigned.uint_ty_str()
221+
})
222+
}
216223
}
217224
_ => None,
218225
}
219226
}
220227

228+
fn literal_to_i128(val: u128, negative: bool) -> Option<i128> {
229+
if negative {
230+
(val <= i128::MAX as u128 + 1).then(|| val.wrapping_neg() as i128)
231+
} else {
232+
val.try_into().ok()
233+
}
234+
}
235+
221236
fn lint_int_literal<'tcx>(
222237
cx: &LateContext<'tcx>,
223238
type_limits: &TypeLimits,

tests/ui/lint/type-overflow.rs

+26
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,46 @@
33

44
fn main() {
55
let error = 255i8; //~WARNING literal out of range for `i8`
6+
//~^ HELP consider using the type `u8` instead
67

78
let ok = 0b1000_0001; // should be ok -> i32
89
let ok = 0b0111_1111i8; // should be ok -> 127i8
910

1011
let fail = 0b1000_0001i8; //~WARNING literal out of range for `i8`
12+
//~^ HELP consider using the type `u8` instead
13+
//~| HELP consider using the type `u8` for the literal and cast it to `i8`
1114

1215
let fail = 0x8000_0000_0000_0000i64; //~WARNING literal out of range for `i64`
16+
//~^ HELP consider using the type `u64` instead
17+
//~| HELP consider using the type `u64` for the literal and cast it to `i64`
1318

1419
let fail = 0x1_FFFF_FFFFu32; //~WARNING literal out of range for `u32`
20+
//~^ HELP consider using the type `u64` instead
1521

1622
let fail: i128 = 0x8000_0000_0000_0000_0000_0000_0000_0000;
1723
//~^ WARNING literal out of range for `i128`
24+
//~| HELP consider using the type `u128` instead
25+
//~| HELP consider using the type `u128` for the literal and cast it to `i128`
26+
27+
let fail = 0x8000_0000_0000_0000_0000_0000_0000_0000;
28+
//~^ WARNING literal out of range for `i32`
29+
//~| HELP consider using the type `u128` instead
30+
31+
let fail = -0x8000_0000_0000_0000_0000_0000_0000_0000; // issue #131849
32+
//~^ WARNING literal out of range for `i32`
33+
//~| HELP consider using the type `i128` instead
34+
35+
let fail = -0x8000_0000_0000_0000_0000_0000_0000_0001i128;
36+
//~^ WARNING literal out of range for `i128`
37+
38+
let fail = 340282366920938463463374607431768211455i8;
39+
//~^ WARNING literal out of range for `i8`
40+
//~| HELP consider using the type `u128` instead
1841

1942
let fail = 0x8FFF_FFFF_FFFF_FFFE; //~WARNING literal out of range for `i32`
43+
//~| HELP consider using the type `u64` instead
44+
//~| HELP
2045

2146
let fail = -0b1111_1111i8; //~WARNING literal out of range for `i8`
47+
//~| HELP consider using the type `i16` instead
2248
}

tests/ui/lint/type-overflow.stderr

+45-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | #![warn(overflowing_literals)]
1313
| ^^^^^^^^^^^^^^^^^^^^
1414

1515
warning: literal out of range for `i8`
16-
--> $DIR/type-overflow.rs:10:16
16+
--> $DIR/type-overflow.rs:11:16
1717
|
1818
LL | let fail = 0b1000_0001i8;
1919
| ^^^^^^^^^^^^^
@@ -29,7 +29,7 @@ LL | let fail = 0b1000_0001u8 as i8;
2929
| ~~~~~~~~~~~~~~~~~~~
3030

3131
warning: literal out of range for `i64`
32-
--> $DIR/type-overflow.rs:12:16
32+
--> $DIR/type-overflow.rs:15:16
3333
|
3434
LL | let fail = 0x8000_0000_0000_0000i64;
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -45,15 +45,15 @@ LL | let fail = 0x8000_0000_0000_0000u64 as i64;
4545
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4646

4747
warning: literal out of range for `u32`
48-
--> $DIR/type-overflow.rs:14:16
48+
--> $DIR/type-overflow.rs:19:16
4949
|
5050
LL | let fail = 0x1_FFFF_FFFFu32;
5151
| ^^^^^^^^^^^^^^^^ help: consider using the type `u64` instead: `0x1_FFFF_FFFFu64`
5252
|
5353
= note: the literal `0x1_FFFF_FFFFu32` (decimal `8589934591`) does not fit into the type `u32` and will become `4294967295u32`
5454

5555
warning: literal out of range for `i128`
56-
--> $DIR/type-overflow.rs:16:22
56+
--> $DIR/type-overflow.rs:22:22
5757
|
5858
LL | let fail: i128 = 0x8000_0000_0000_0000_0000_0000_0000_0000;
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -66,26 +66,63 @@ LL | let fail: i128 = 0x8000_0000_0000_0000_0000_0000_0000_0000u128 as i128;
6666
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6767

6868
warning: literal out of range for `i32`
69-
--> $DIR/type-overflow.rs:19:16
69+
--> $DIR/type-overflow.rs:27:16
70+
|
71+
LL | let fail = 0x8000_0000_0000_0000_0000_0000_0000_0000;
72+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
73+
|
74+
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0000` (decimal `170141183460469231731687303715884105728`) does not fit into the type `i32` and will become `0i32`
75+
= help: consider using the type `u128` instead
76+
77+
warning: literal out of range for `i32`
78+
--> $DIR/type-overflow.rs:31:17
79+
|
80+
LL | let fail = -0x8000_0000_0000_0000_0000_0000_0000_0000; // issue #131849
81+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
82+
|
83+
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0000` (decimal `170141183460469231731687303715884105728`) does not fit into the type `i32`
84+
= note: and the value `-0x8000_0000_0000_0000_0000_0000_0000_0000` will become `0i32`
85+
= help: consider using the type `i128` instead
86+
87+
warning: literal out of range for `i128`
88+
--> $DIR/type-overflow.rs:35:17
89+
|
90+
LL | let fail = -0x8000_0000_0000_0000_0000_0000_0000_0001i128;
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92+
|
93+
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0001i128` (decimal `170141183460469231731687303715884105729`) does not fit into the type `i128`
94+
= note: and the value `-0x8000_0000_0000_0000_0000_0000_0000_0001i128` will become `170141183460469231731687303715884105727i128`
95+
96+
warning: literal out of range for `i8`
97+
--> $DIR/type-overflow.rs:38:16
98+
|
99+
LL | let fail = 340282366920938463463374607431768211455i8;
100+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
101+
|
102+
= note: the literal `340282366920938463463374607431768211455i8` does not fit into the type `i8` whose range is `-128..=127`
103+
= help: consider using the type `u128` instead
104+
105+
warning: literal out of range for `i32`
106+
--> $DIR/type-overflow.rs:42:16
70107
|
71108
LL | let fail = 0x8FFF_FFFF_FFFF_FFFE;
72109
| ^^^^^^^^^^^^^^^^^^^^^
73110
|
74111
= note: the literal `0x8FFF_FFFF_FFFF_FFFE` (decimal `10376293541461622782`) does not fit into the type `i32` and will become `-2i32`
75-
= help: consider using the type `i128` instead
112+
= help: consider using the type `u64` instead
76113
help: to use as a negative number (decimal `-2`), consider using the type `u32` for the literal and cast it to `i32`
77114
|
78115
LL | let fail = 0x8FFF_FFFF_FFFF_FFFEu32 as i32;
79116
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80117

81118
warning: literal out of range for `i8`
82-
--> $DIR/type-overflow.rs:21:17
119+
--> $DIR/type-overflow.rs:46:17
83120
|
84121
LL | let fail = -0b1111_1111i8;
85122
| ^^^^^^^^^^^^^ help: consider using the type `i16` instead: `0b1111_1111i16`
86123
|
87124
= note: the literal `0b1111_1111i8` (decimal `255`) does not fit into the type `i8`
88125
= note: and the value `-0b1111_1111i8` will become `1i8`
89126

90-
warning: 7 warnings emitted
127+
warning: 11 warnings emitted
91128

0 commit comments

Comments
 (0)