Skip to content

Commit ec00784

Browse files
Rollup merge of #79981 - camelid:overflowing_literals-inference-error, r=lcnr
Add 'consider using' message to overflowing_literals Fixes #79744. Ironically, the `overflowing_literals` handler for binary or hex already had this message! You would think it would be the other way around :) cc ```@scottmcm```
2 parents ee88f46 + a9b16c6 commit ec00784

10 files changed

+87
-33
lines changed

compiler/rustc_lint/src/types.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ fn report_bin_hex_error(
225225
(t.name_str(), actually.to_string())
226226
}
227227
};
228-
let mut err = lint.build(&format!("literal out of range for {}", t));
228+
let mut err = lint.build(&format!("literal out of range for `{}`", t));
229229
err.note(&format!(
230230
"the literal `{}` (decimal `{}`) does not fit into \
231231
the type `{}` and will become `{}{}`",
@@ -238,12 +238,12 @@ fn report_bin_hex_error(
238238
let (sans_suffix, _) = repr_str.split_at(pos);
239239
err.span_suggestion(
240240
expr.span,
241-
&format!("consider using `{}` instead", sugg_ty),
241+
&format!("consider using the type `{}` instead", sugg_ty),
242242
format!("{}{}", sans_suffix, sugg_ty),
243243
Applicability::MachineApplicable,
244244
);
245245
} else {
246-
err.help(&format!("consider using `{}` instead", sugg_ty));
246+
err.help(&format!("consider using the type `{}` instead", sugg_ty));
247247
}
248248
}
249249
err.emit();
@@ -338,18 +338,23 @@ fn lint_int_literal<'tcx>(
338338
}
339339

340340
cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
341-
lint.build(&format!("literal out of range for `{}`", t.name_str()))
342-
.note(&format!(
343-
"the literal `{}` does not fit into the type `{}` whose range is `{}..={}`",
344-
cx.sess()
345-
.source_map()
346-
.span_to_snippet(lit.span)
347-
.expect("must get snippet from literal"),
348-
t.name_str(),
349-
min,
350-
max,
351-
))
352-
.emit();
341+
let mut err = lint.build(&format!("literal out of range for `{}`", t.name_str()));
342+
err.note(&format!(
343+
"the literal `{}` does not fit into the type `{}` whose range is `{}..={}`",
344+
cx.sess()
345+
.source_map()
346+
.span_to_snippet(lit.span)
347+
.expect("must get snippet from literal"),
348+
t.name_str(),
349+
min,
350+
max,
351+
));
352+
if let Some(sugg_ty) =
353+
get_type_suggestion(&cx.typeck_results().node_type(e.hir_id), v, negative)
354+
{
355+
err.help(&format!("consider using the type `{}` instead", sugg_ty));
356+
}
357+
err.emit();
353358
});
354359
}
355360
}

src/test/ui/enum/enum-discrim-too-small2.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ note: the lint level is defined here
1010
LL | #![deny(overflowing_literals)]
1111
| ^^^^^^^^^^^^^^^^^^^^
1212
= note: the literal `223` does not fit into the type `i8` whose range is `-128..=127`
13+
= help: consider using the type `u8` instead
1314

1415
error: literal out of range for `i16`
1516
--> $DIR/enum-discrim-too-small2.rs:15:12
@@ -18,6 +19,7 @@ LL | Ci16 = 55555,
1819
| ^^^^^
1920
|
2021
= note: the literal `55555` does not fit into the type `i16` whose range is `-32768..=32767`
22+
= help: consider using the type `u16` instead
2123

2224
error: literal out of range for `i32`
2325
--> $DIR/enum-discrim-too-small2.rs:22:12
@@ -26,6 +28,7 @@ LL | Ci32 = 3_000_000_000,
2628
| ^^^^^^^^^^^^^
2729
|
2830
= note: the literal `3_000_000_000` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
31+
= help: consider using the type `u32` instead
2932

3033
error: literal out of range for `i64`
3134
--> $DIR/enum-discrim-too-small2.rs:29:12
@@ -34,6 +37,7 @@ LL | Ci64 = 9223372036854775809,
3437
| ^^^^^^^^^^^^^^^^^^^
3538
|
3639
= note: the literal `9223372036854775809` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
40+
= help: consider using the type `u64` instead
3741

3842
error: aborting due to 4 previous errors
3943

src/test/ui/issues/issue-79744.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
let elem = 6i8;
3+
let e2 = 230;
4+
//~^ ERROR literal out of range for `i8`
5+
//~| HELP consider using the type `u8` instead
6+
7+
let mut vec = Vec::new();
8+
9+
vec.push(e2);
10+
vec.push(elem);
11+
12+
println!("{:?}", vec);
13+
}

src/test/ui/issues/issue-79744.stderr

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: literal out of range for `i8`
2+
--> $DIR/issue-79744.rs:3:14
3+
|
4+
LL | let e2 = 230;
5+
| ^^^
6+
|
7+
= note: `#[deny(overflowing_literals)]` on by default
8+
= note: the literal `230` does not fit into the type `i8` whose range is `-128..=127`
9+
= help: consider using the type `u8` instead
10+
11+
error: aborting due to previous error
12+

src/test/ui/lint/lint-type-limits2.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ note: the lint level is defined here
1818
LL | #![warn(overflowing_literals)]
1919
| ^^^^^^^^^^^^^^^^^^^^
2020
= note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
21+
= help: consider using the type `u8` instead
2122

2223
error: aborting due to previous error; 1 warning emitted
2324

src/test/ui/lint/lint-type-limits3.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ note: the lint level is defined here
1818
LL | #![warn(overflowing_literals)]
1919
| ^^^^^^^^^^^^^^^^^^^^
2020
= note: the literal `200` does not fit into the type `i8` whose range is `-128..=127`
21+
= help: consider using the type `u8` instead
2122

2223
error: aborting due to previous error; 1 warning emitted
2324

src/test/ui/lint/lint-type-overflow.stderr

+16
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ LL | let x1: i8 = 128;
2626
| ^^^
2727
|
2828
= note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
29+
= help: consider using the type `u8` instead
2930

3031
error: literal out of range for `i8`
3132
--> $DIR/lint-type-overflow.rs:18:19
@@ -34,6 +35,7 @@ LL | let x3: i8 = -129;
3435
| ^^^
3536
|
3637
= note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
38+
= help: consider using the type `i16` instead
3739

3840
error: literal out of range for `i8`
3941
--> $DIR/lint-type-overflow.rs:19:19
@@ -42,6 +44,7 @@ LL | let x3: i8 = -(129);
4244
| ^^^^^
4345
|
4446
= note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
47+
= help: consider using the type `i16` instead
4548

4649
error: literal out of range for `i8`
4750
--> $DIR/lint-type-overflow.rs:20:20
@@ -50,6 +53,7 @@ LL | let x3: i8 = -{129};
5053
| ^^^
5154
|
5255
= note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
56+
= help: consider using the type `u8` instead
5357

5458
error: literal out of range for `i8`
5559
--> $DIR/lint-type-overflow.rs:22:10
@@ -58,6 +62,7 @@ LL | test(1000);
5862
| ^^^^
5963
|
6064
= note: the literal `1000` does not fit into the type `i8` whose range is `-128..=127`
65+
= help: consider using the type `i16` instead
6166

6267
error: literal out of range for `i8`
6368
--> $DIR/lint-type-overflow.rs:24:13
@@ -66,6 +71,7 @@ LL | let x = 128_i8;
6671
| ^^^^^^
6772
|
6873
= note: the literal `128_i8` does not fit into the type `i8` whose range is `-128..=127`
74+
= help: consider using the type `u8` instead
6975

7076
error: literal out of range for `i8`
7177
--> $DIR/lint-type-overflow.rs:28:14
@@ -74,6 +80,7 @@ LL | let x = -129_i8;
7480
| ^^^^^^
7581
|
7682
= note: the literal `129_i8` does not fit into the type `i8` whose range is `-128..=127`
83+
= help: consider using the type `i16` instead
7784

7885
error: literal out of range for `i32`
7986
--> $DIR/lint-type-overflow.rs:32:18
@@ -82,6 +89,7 @@ LL | let x: i32 = 2147483648;
8289
| ^^^^^^^^^^
8390
|
8491
= note: the literal `2147483648` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
92+
= help: consider using the type `u32` instead
8593

8694
error: literal out of range for `i32`
8795
--> $DIR/lint-type-overflow.rs:33:13
@@ -90,6 +98,7 @@ LL | let x = 2147483648_i32;
9098
| ^^^^^^^^^^^^^^
9199
|
92100
= note: the literal `2147483648_i32` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
101+
= help: consider using the type `u32` instead
93102

94103
error: literal out of range for `i32`
95104
--> $DIR/lint-type-overflow.rs:36:19
@@ -98,6 +107,7 @@ LL | let x: i32 = -2147483649;
98107
| ^^^^^^^^^^
99108
|
100109
= note: the literal `2147483649` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
110+
= help: consider using the type `i64` instead
101111

102112
error: literal out of range for `i32`
103113
--> $DIR/lint-type-overflow.rs:37:14
@@ -106,6 +116,7 @@ LL | let x = -2147483649_i32;
106116
| ^^^^^^^^^^^^^^
107117
|
108118
= note: the literal `2147483649_i32` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
119+
= help: consider using the type `i64` instead
109120

110121
error: literal out of range for `i32`
111122
--> $DIR/lint-type-overflow.rs:38:13
@@ -114,6 +125,7 @@ LL | let x = 2147483648;
114125
| ^^^^^^^^^^
115126
|
116127
= note: the literal `2147483648` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
128+
= help: consider using the type `u32` instead
117129

118130
error: literal out of range for `i64`
119131
--> $DIR/lint-type-overflow.rs:40:13
@@ -122,6 +134,7 @@ LL | let x = 9223372036854775808_i64;
122134
| ^^^^^^^^^^^^^^^^^^^^^^^
123135
|
124136
= note: the literal `9223372036854775808_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
137+
= help: consider using the type `u64` instead
125138

126139
error: literal out of range for `i64`
127140
--> $DIR/lint-type-overflow.rs:42:13
@@ -130,6 +143,7 @@ LL | let x = 18446744073709551615_i64;
130143
| ^^^^^^^^^^^^^^^^^^^^^^^^
131144
|
132145
= note: the literal `18446744073709551615_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
146+
= help: consider using the type `u64` instead
133147

134148
error: literal out of range for `i64`
135149
--> $DIR/lint-type-overflow.rs:43:19
@@ -138,6 +152,7 @@ LL | let x: i64 = -9223372036854775809;
138152
| ^^^^^^^^^^^^^^^^^^^
139153
|
140154
= note: the literal `9223372036854775809` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
155+
= help: consider using the type `i128` instead
141156

142157
error: literal out of range for `i64`
143158
--> $DIR/lint-type-overflow.rs:44:14
@@ -146,6 +161,7 @@ LL | let x = -9223372036854775809_i64;
146161
| ^^^^^^^^^^^^^^^^^^^^^^^
147162
|
148163
= note: the literal `9223372036854775809_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
164+
= help: consider using the type `i128` instead
149165

150166
error: aborting due to 18 previous errors
151167

src/test/ui/lint/lint-type-overflow2.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ note: the lint level is defined here
1010
LL | #![deny(overflowing_literals)]
1111
| ^^^^^^^^^^^^^^^^^^^^
1212
= note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
13+
= help: consider using the type `u8` instead
1314

1415
error: literal out of range for `f32`
1516
--> $DIR/lint-type-overflow2.rs:9:14

src/test/ui/lint/type-overflow.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ fn main() {
77
let ok = 0b1000_0001; // should be ok -> i32
88
let ok = 0b0111_1111i8; // should be ok -> 127i8
99

10-
let fail = 0b1000_0001i8; //~WARNING literal out of range for i8
10+
let fail = 0b1000_0001i8; //~WARNING literal out of range for `i8`
1111

12-
let fail = 0x8000_0000_0000_0000i64; //~WARNING literal out of range for i64
12+
let fail = 0x8000_0000_0000_0000i64; //~WARNING literal out of range for `i64`
1313

14-
let fail = 0x1_FFFF_FFFFu32; //~WARNING literal out of range for u32
14+
let fail = 0x1_FFFF_FFFFu32; //~WARNING literal out of range for `u32`
1515

1616
let fail: i128 = 0x8000_0000_0000_0000_0000_0000_0000_0000;
17-
//~^ WARNING literal out of range for i128
17+
//~^ WARNING literal out of range for `i128`
1818

19-
let fail = 0x8FFF_FFFF_FFFF_FFFE; //~WARNING literal out of range for i32
19+
let fail = 0x8FFF_FFFF_FFFF_FFFE; //~WARNING literal out of range for `i32`
2020

21-
let fail = -0b1111_1111i8; //~WARNING literal out of range for i8
21+
let fail = -0b1111_1111i8; //~WARNING literal out of range for `i8`
2222
}

src/test/ui/lint/type-overflow.stderr

+13-12
Original file line numberDiff line numberDiff line change
@@ -10,54 +10,55 @@ note: the lint level is defined here
1010
LL | #![warn(overflowing_literals)]
1111
| ^^^^^^^^^^^^^^^^^^^^
1212
= note: the literal `255i8` does not fit into the type `i8` whose range is `-128..=127`
13+
= help: consider using the type `u8` instead
1314

14-
warning: literal out of range for i8
15+
warning: literal out of range for `i8`
1516
--> $DIR/type-overflow.rs:10:16
1617
|
1718
LL | let fail = 0b1000_0001i8;
18-
| ^^^^^^^^^^^^^ help: consider using `u8` instead: `0b1000_0001u8`
19+
| ^^^^^^^^^^^^^ help: consider using the type `u8` instead: `0b1000_0001u8`
1920
|
2021
= note: the literal `0b1000_0001i8` (decimal `129`) does not fit into the type `i8` and will become `-127i8`
2122

22-
warning: literal out of range for i64
23+
warning: literal out of range for `i64`
2324
--> $DIR/type-overflow.rs:12:16
2425
|
2526
LL | let fail = 0x8000_0000_0000_0000i64;
26-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `u64` instead: `0x8000_0000_0000_0000u64`
27+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using the type `u64` instead: `0x8000_0000_0000_0000u64`
2728
|
2829
= note: the literal `0x8000_0000_0000_0000i64` (decimal `9223372036854775808`) does not fit into the type `i64` and will become `-9223372036854775808i64`
2930

30-
warning: literal out of range for u32
31+
warning: literal out of range for `u32`
3132
--> $DIR/type-overflow.rs:14:16
3233
|
3334
LL | let fail = 0x1_FFFF_FFFFu32;
34-
| ^^^^^^^^^^^^^^^^ help: consider using `u64` instead: `0x1_FFFF_FFFFu64`
35+
| ^^^^^^^^^^^^^^^^ help: consider using the type `u64` instead: `0x1_FFFF_FFFFu64`
3536
|
3637
= note: the literal `0x1_FFFF_FFFFu32` (decimal `8589934591`) does not fit into the type `u32` and will become `4294967295u32`
3738

38-
warning: literal out of range for i128
39+
warning: literal out of range for `i128`
3940
--> $DIR/type-overflow.rs:16:22
4041
|
4142
LL | let fail: i128 = 0x8000_0000_0000_0000_0000_0000_0000_0000;
4243
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4344
|
4445
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0000` (decimal `170141183460469231731687303715884105728`) does not fit into the type `i128` and will become `-170141183460469231731687303715884105728i128`
45-
= help: consider using `u128` instead
46+
= help: consider using the type `u128` instead
4647

47-
warning: literal out of range for i32
48+
warning: literal out of range for `i32`
4849
--> $DIR/type-overflow.rs:19:16
4950
|
5051
LL | let fail = 0x8FFF_FFFF_FFFF_FFFE;
5152
| ^^^^^^^^^^^^^^^^^^^^^
5253
|
5354
= note: the literal `0x8FFF_FFFF_FFFF_FFFE` (decimal `10376293541461622782`) does not fit into the type `i32` and will become `-2i32`
54-
= help: consider using `i128` instead
55+
= help: consider using the type `i128` instead
5556

56-
warning: literal out of range for i8
57+
warning: literal out of range for `i8`
5758
--> $DIR/type-overflow.rs:21:17
5859
|
5960
LL | let fail = -0b1111_1111i8;
60-
| ^^^^^^^^^^^^^ help: consider using `i16` instead: `0b1111_1111i16`
61+
| ^^^^^^^^^^^^^ help: consider using the type `i16` instead: `0b1111_1111i16`
6162
|
6263
= note: the literal `0b1111_1111i8` (decimal `255`) does not fit into the type `i8` and will become `-1i8`
6364

0 commit comments

Comments
 (0)