Skip to content

Commit a4ffbaa

Browse files
committed
Add more context to the literal overflow message
1 parent e5e8ba4 commit a4ffbaa

10 files changed

+107
-11
lines changed

src/librustc_lint/types.rs

+39-5
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ fn report_bin_hex_error(
165165
let mut err = lint.build(&format!("literal out of range for {}", t));
166166
err.note(&format!(
167167
"the literal `{}` (decimal `{}`) does not fit into \
168-
an `{}` and will become `{}{}`",
168+
the type `{}` and will become `{}{}`",
169169
repr_str, val, t, actually, t
170170
));
171171
if let Some(sugg_ty) = get_type_suggestion(&cx.tables.node_type(expr.hir_id), val, negative)
@@ -242,7 +242,7 @@ fn lint_int_literal<'a, 'tcx>(
242242
v: u128,
243243
) {
244244
let int_type = t.normalize(cx.sess().target.ptr_width);
245-
let (_, max) = int_ty_range(int_type);
245+
let (min, max) = int_ty_range(int_type);
246246
let max = max as u128;
247247
let negative = type_limits.negated_expr_id == e.hir_id;
248248

@@ -267,7 +267,19 @@ fn lint_int_literal<'a, 'tcx>(
267267
}
268268

269269
cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
270-
lint.build(&format!("literal out of range for `{}`", t.name_str())).emit()
270+
lint.build(&format!("literal out of range for `{}`", t.name_str()))
271+
.note(&format!(
272+
"the literal `{}` does not fit into the type `{}` whose range is `{}..={}`",
273+
cx.sess()
274+
.source_map()
275+
.span_to_snippet(lit.span)
276+
.ok()
277+
.expect("must get snippet from literal"),
278+
t.name_str(),
279+
min,
280+
max,
281+
))
282+
.emit();
271283
});
272284
}
273285
}
@@ -320,7 +332,19 @@ fn lint_uint_literal<'a, 'tcx>(
320332
return;
321333
}
322334
cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
323-
lint.build(&format!("literal out of range for `{}`", t.name_str())).emit()
335+
lint.build(&format!("literal out of range for `{}`", t.name_str()))
336+
.note(&format!(
337+
"the literal `{}` does not fit into the type `{}` whose range is `{}..={}`",
338+
cx.sess()
339+
.source_map()
340+
.span_to_snippet(lit.span)
341+
.ok()
342+
.expect("must get snippet from literal"),
343+
t.name_str(),
344+
min,
345+
max,
346+
))
347+
.emit()
324348
});
325349
}
326350
}
@@ -352,7 +376,17 @@ fn lint_literal<'a, 'tcx>(
352376
};
353377
if is_infinite == Ok(true) {
354378
cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
355-
lint.build(&format!("literal out of range for `{}`", t.name_str())).emit()
379+
lint.build(&format!("literal out of range for `{}`", t.name_str()))
380+
.note(&format!(
381+
"the literal `{}` does not fit into the type `{}` and will be converted to `std::{}::INFINITY`",
382+
cx.sess()
383+
.source_map()
384+
.span_to_snippet(lit.span)
385+
.expect("must get snippet from literal"),
386+
t.name_str(),
387+
t.name_str(),
388+
))
389+
.emit();
356390
});
357391
}
358392
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,31 @@ note: the lint level is defined here
99
|
1010
LL | #![deny(overflowing_literals)]
1111
| ^^^^^^^^^^^^^^^^^^^^
12+
= note: the literal `223` does not fit into the type `i8` whose range is `-128..=127`
1213

1314
error: literal out of range for `i16`
1415
--> $DIR/enum-discrim-too-small2.rs:15:12
1516
|
1617
LL | Ci16 = 55555,
1718
| ^^^^^
19+
|
20+
= note: the literal `55555` does not fit into the type `i16` whose range is `-32768..=32767`
1821

1922
error: literal out of range for `i32`
2023
--> $DIR/enum-discrim-too-small2.rs:22:12
2124
|
2225
LL | Ci32 = 3_000_000_000,
2326
| ^^^^^^^^^^^^^
27+
|
28+
= note: the literal `3_000_000_000` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
2429

2530
error: literal out of range for `i64`
2631
--> $DIR/enum-discrim-too-small2.rs:29:12
2732
|
2833
LL | Ci64 = 9223372036854775809,
2934
| ^^^^^^^^^^^^^^^^^^^
35+
|
36+
= note: the literal `9223372036854775809` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
3037

3138
error: aborting due to 4 previous errors
3239

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

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | for n in 100_000.. {
55
| ^^^^^^^
66
|
77
= note: `#[deny(overflowing_literals)]` on by default
8+
= note: the literal `100_000` does not fit into the type `u16` whose range is `0..=65535`
89

910
error: aborting due to previous error
1011

src/test/ui/lint/deny-overflowing-literals.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | let x: u8 = 256;
55
| ^^^
66
|
77
= note: `#[deny(overflowing_literals)]` on by default
8+
= note: the literal `256` does not fit into the type `u8` whose range is `0..=255`
89

910
error: range endpoint is out of range for `u8`
1011
--> $DIR/deny-overflowing-literals.rs:5:14

src/test/ui/lint/lint-range-endpoint-overflow.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@ error: literal out of range for `u8`
1515
|
1616
LL | let range_c = 0..=256;
1717
| ^^^
18+
|
19+
= note: the literal `256` does not fit into the type `u8` whose range is `0..=255`
1820

1921
error: literal out of range for `u8`
2022
--> $DIR/lint-range-endpoint-overflow.rs:7:19
2123
|
2224
LL | let range_d = 256..5;
2325
| ^^^
26+
|
27+
= note: the literal `256` does not fit into the type `u8` whose range is `0..=255`
2428

2529
error: literal out of range for `u8`
2630
--> $DIR/lint-range-endpoint-overflow.rs:8:22
2731
|
2832
LL | let range_e = 0..257;
2933
| ^^^
34+
|
35+
= note: the literal `257` does not fit into the type `u8` whose range is `0..=255`
3036

3137
error: range endpoint is out of range for `u8`
3238
--> $DIR/lint-range-endpoint-overflow.rs:9:20

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

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ note: the lint level is defined here
1717
|
1818
LL | #![warn(overflowing_literals)]
1919
| ^^^^^^^^^^^^^^^^^^^^
20+
= note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
2021

2122
error: aborting due to previous error
2223

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

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ note: the lint level is defined here
1717
|
1818
LL | #![warn(overflowing_literals)]
1919
| ^^^^^^^^^^^^^^^^^^^^
20+
= note: the literal `200` does not fit into the type `i8` whose range is `-128..=127`
2021

2122
error: aborting due to previous error
2223

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

+35
Original file line numberDiff line numberDiff line change
@@ -9,108 +9,143 @@ note: the lint level is defined here
99
|
1010
LL | #![deny(overflowing_literals)]
1111
| ^^^^^^^^^^^^^^^^^^^^
12+
= note: the literal `256` does not fit into the type `u8` whose range is `0..=255`
1213

1314
error: literal out of range for `u8`
1415
--> $DIR/lint-type-overflow.rs:13:14
1516
|
1617
LL | let x1 = 256_u8;
1718
| ^^^^^^
19+
|
20+
= note: the literal `256_u8` does not fit into the type `u8` whose range is `0..=255`
1821

1922
error: literal out of range for `i8`
2023
--> $DIR/lint-type-overflow.rs:16:18
2124
|
2225
LL | let x1: i8 = 128;
2326
| ^^^
27+
|
28+
= note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
2429

2530
error: literal out of range for `i8`
2631
--> $DIR/lint-type-overflow.rs:18:19
2732
|
2833
LL | let x3: i8 = -129;
2934
| ^^^
35+
|
36+
= note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
3037

3138
error: literal out of range for `i8`
3239
--> $DIR/lint-type-overflow.rs:19:19
3340
|
3441
LL | let x3: i8 = -(129);
3542
| ^^^^^
43+
|
44+
= note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
3645

3746
error: literal out of range for `i8`
3847
--> $DIR/lint-type-overflow.rs:20:20
3948
|
4049
LL | let x3: i8 = -{129};
4150
| ^^^
51+
|
52+
= note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
4253

4354
error: literal out of range for `i8`
4455
--> $DIR/lint-type-overflow.rs:22:10
4556
|
4657
LL | test(1000);
4758
| ^^^^
59+
|
60+
= note: the literal `1000` does not fit into the type `i8` whose range is `-128..=127`
4861

4962
error: literal out of range for `i8`
5063
--> $DIR/lint-type-overflow.rs:24:13
5164
|
5265
LL | let x = 128_i8;
5366
| ^^^^^^
67+
|
68+
= note: the literal `128_i8` does not fit into the type `i8` whose range is `-128..=127`
5469

5570
error: literal out of range for `i8`
5671
--> $DIR/lint-type-overflow.rs:28:14
5772
|
5873
LL | let x = -129_i8;
5974
| ^^^^^^
75+
|
76+
= note: the literal `129_i8` does not fit into the type `i8` whose range is `-128..=127`
6077

6178
error: literal out of range for `i32`
6279
--> $DIR/lint-type-overflow.rs:32:18
6380
|
6481
LL | let x: i32 = 2147483648;
6582
| ^^^^^^^^^^
83+
|
84+
= note: the literal `2147483648` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
6685

6786
error: literal out of range for `i32`
6887
--> $DIR/lint-type-overflow.rs:33:13
6988
|
7089
LL | let x = 2147483648_i32;
7190
| ^^^^^^^^^^^^^^
91+
|
92+
= note: the literal `2147483648_i32` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
7293

7394
error: literal out of range for `i32`
7495
--> $DIR/lint-type-overflow.rs:36:19
7596
|
7697
LL | let x: i32 = -2147483649;
7798
| ^^^^^^^^^^
99+
|
100+
= note: the literal `2147483649` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
78101

79102
error: literal out of range for `i32`
80103
--> $DIR/lint-type-overflow.rs:37:14
81104
|
82105
LL | let x = -2147483649_i32;
83106
| ^^^^^^^^^^^^^^
107+
|
108+
= note: the literal `2147483649_i32` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
84109

85110
error: literal out of range for `i32`
86111
--> $DIR/lint-type-overflow.rs:38:13
87112
|
88113
LL | let x = 2147483648;
89114
| ^^^^^^^^^^
115+
|
116+
= note: the literal `2147483648` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
90117

91118
error: literal out of range for `i64`
92119
--> $DIR/lint-type-overflow.rs:40:13
93120
|
94121
LL | let x = 9223372036854775808_i64;
95122
| ^^^^^^^^^^^^^^^^^^^^^^^
123+
|
124+
= note: the literal `9223372036854775808_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
96125

97126
error: literal out of range for `i64`
98127
--> $DIR/lint-type-overflow.rs:42:13
99128
|
100129
LL | let x = 18446744073709551615_i64;
101130
| ^^^^^^^^^^^^^^^^^^^^^^^^
131+
|
132+
= note: the literal `18446744073709551615_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
102133

103134
error: literal out of range for `i64`
104135
--> $DIR/lint-type-overflow.rs:43:19
105136
|
106137
LL | let x: i64 = -9223372036854775809;
107138
| ^^^^^^^^^^^^^^^^^^^
139+
|
140+
= note: the literal `9223372036854775809` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
108141

109142
error: literal out of range for `i64`
110143
--> $DIR/lint-type-overflow.rs:44:14
111144
|
112145
LL | let x = -9223372036854775809_i64;
113146
| ^^^^^^^^^^^^^^^^^^^^^^^
147+
|
148+
= note: the literal `9223372036854775809_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
114149

115150
error: aborting due to 18 previous errors
116151

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

+9
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,39 @@ note: the lint level is defined here
99
|
1010
LL | #![deny(overflowing_literals)]
1111
| ^^^^^^^^^^^^^^^^^^^^
12+
= note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
1213

1314
error: literal out of range for `f32`
1415
--> $DIR/lint-type-overflow2.rs:9:14
1516
|
1617
LL | let x = -3.40282357e+38_f32;
1718
| ^^^^^^^^^^^^^^^^^^
19+
|
20+
= note: the literal `3.40282357e+38_f32` does not fit into the type `f32` and will be converted to `std::f32::INFINITY`
1821

1922
error: literal out of range for `f32`
2023
--> $DIR/lint-type-overflow2.rs:10:14
2124
|
2225
LL | let x = 3.40282357e+38_f32;
2326
| ^^^^^^^^^^^^^^^^^^
27+
|
28+
= note: the literal `3.40282357e+38_f32` does not fit into the type `f32` and will be converted to `std::f32::INFINITY`
2429

2530
error: literal out of range for `f64`
2631
--> $DIR/lint-type-overflow2.rs:11:14
2732
|
2833
LL | let x = -1.7976931348623159e+308_f64;
2934
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
|
36+
= note: the literal `1.7976931348623159e+308_f64` does not fit into the type `f64` and will be converted to `std::f64::INFINITY`
3037

3138
error: literal out of range for `f64`
3239
--> $DIR/lint-type-overflow2.rs:12:14
3340
|
3441
LL | let x = 1.7976931348623159e+308_f64;
3542
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
43+
|
44+
= note: the literal `1.7976931348623159e+308_f64` does not fit into the type `f64` and will be converted to `std::f64::INFINITY`
3645

3746
error: aborting due to 5 previous errors
3847

0 commit comments

Comments
 (0)