about summary refs log tree commit diff
diff options
context:
space:
mode:
authorYI <uuuuuu@protonmail.com>2020-03-14 11:57:04 +0800
committerYI <uuuuuu@protonmail.com>2020-03-15 11:37:43 +0800
commita4ffbaadc81f9768a262059af77cf35fb88784bf (patch)
tree3ac524bc0c8fc6ab8f7860e09a60462aa48ba38d
parente5e8ba4edc435c9f87314b23a6c5d9c175bdf19c (diff)
downloadrust-a4ffbaadc81f9768a262059af77cf35fb88784bf.tar.gz
rust-a4ffbaadc81f9768a262059af77cf35fb88784bf.zip
Add more context to the literal overflow message
-rw-r--r--src/librustc_lint/types.rs44
-rw-r--r--src/test/ui/enum/enum-discrim-too-small2.stderr7
-rw-r--r--src/test/ui/issues/issue-63364.stderr1
-rw-r--r--src/test/ui/lint/deny-overflowing-literals.stderr1
-rw-r--r--src/test/ui/lint/lint-range-endpoint-overflow.stderr6
-rw-r--r--src/test/ui/lint/lint-type-limits2.stderr1
-rw-r--r--src/test/ui/lint/lint-type-limits3.stderr1
-rw-r--r--src/test/ui/lint/lint-type-overflow.stderr35
-rw-r--r--src/test/ui/lint/lint-type-overflow2.stderr9
-rw-r--r--src/test/ui/lint/type-overflow.stderr13
10 files changed, 107 insertions, 11 deletions
diff --git a/src/librustc_lint/types.rs b/src/librustc_lint/types.rs
index 86d93612e99..b5c0296e297 100644
--- a/src/librustc_lint/types.rs
+++ b/src/librustc_lint/types.rs
@@ -165,7 +165,7 @@ fn report_bin_hex_error(
         let mut err = lint.build(&format!("literal out of range for {}", t));
         err.note(&format!(
             "the literal `{}` (decimal `{}`) does not fit into \
-                    an `{}` and will become `{}{}`",
+             the type `{}` and will become `{}{}`",
             repr_str, val, t, actually, t
         ));
         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>(
     v: u128,
 ) {
     let int_type = t.normalize(cx.sess().target.ptr_width);
-    let (_, max) = int_ty_range(int_type);
+    let (min, max) = int_ty_range(int_type);
     let max = max as u128;
     let negative = type_limits.negated_expr_id == e.hir_id;
 
@@ -267,7 +267,19 @@ fn lint_int_literal<'a, 'tcx>(
         }
 
         cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
-            lint.build(&format!("literal out of range for `{}`", t.name_str())).emit()
+            lint.build(&format!("literal out of range for `{}`", t.name_str()))
+                .note(&format!(
+                    "the literal `{}` does not fit into the type `{}` whose range is `{}..={}`",
+                    cx.sess()
+                        .source_map()
+                        .span_to_snippet(lit.span)
+                        .ok()
+                        .expect("must get snippet from literal"),
+                    t.name_str(),
+                    min,
+                    max,
+                ))
+                .emit();
         });
     }
 }
@@ -320,7 +332,19 @@ fn lint_uint_literal<'a, 'tcx>(
             return;
         }
         cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
-            lint.build(&format!("literal out of range for `{}`", t.name_str())).emit()
+            lint.build(&format!("literal out of range for `{}`", t.name_str()))
+                .note(&format!(
+                    "the literal `{}` does not fit into the type `{}` whose range is `{}..={}`",
+                    cx.sess()
+                        .source_map()
+                        .span_to_snippet(lit.span)
+                        .ok()
+                        .expect("must get snippet from literal"),
+                    t.name_str(),
+                    min,
+                    max,
+                ))
+                .emit()
         });
     }
 }
@@ -352,7 +376,17 @@ fn lint_literal<'a, 'tcx>(
             };
             if is_infinite == Ok(true) {
                 cx.struct_span_lint(OVERFLOWING_LITERALS, e.span, |lint| {
-                    lint.build(&format!("literal out of range for `{}`", t.name_str())).emit()
+                    lint.build(&format!("literal out of range for `{}`", t.name_str()))
+                        .note(&format!(
+                            "the literal `{}` does not fit into the type `{}` and will be converted to `std::{}::INFINITY`",
+                            cx.sess()
+                                .source_map()
+                                .span_to_snippet(lit.span)
+                                .expect("must get snippet from literal"),
+                            t.name_str(),
+                            t.name_str(),
+                        ))
+                        .emit();
                 });
             }
         }
diff --git a/src/test/ui/enum/enum-discrim-too-small2.stderr b/src/test/ui/enum/enum-discrim-too-small2.stderr
index 3aa88df29f1..fadf6ab86b4 100644
--- a/src/test/ui/enum/enum-discrim-too-small2.stderr
+++ b/src/test/ui/enum/enum-discrim-too-small2.stderr
@@ -9,24 +9,31 @@ note: the lint level is defined here
    |
 LL | #![deny(overflowing_literals)]
    |         ^^^^^^^^^^^^^^^^^^^^
+   = note: the literal `223` does not fit into the type `i8` whose range is `-128..=127`
 
 error: literal out of range for `i16`
   --> $DIR/enum-discrim-too-small2.rs:15:12
    |
 LL |     Ci16 = 55555,
    |            ^^^^^
+   |
+   = note: the literal `55555` does not fit into the type `i16` whose range is `-32768..=32767`
 
 error: literal out of range for `i32`
   --> $DIR/enum-discrim-too-small2.rs:22:12
    |
 LL |     Ci32 = 3_000_000_000,
    |            ^^^^^^^^^^^^^
+   |
+   = note: the literal `3_000_000_000` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
 
 error: literal out of range for `i64`
   --> $DIR/enum-discrim-too-small2.rs:29:12
    |
 LL |     Ci64 = 9223372036854775809,
    |            ^^^^^^^^^^^^^^^^^^^
+   |
+   = note: the literal `9223372036854775809` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
 
 error: aborting due to 4 previous errors
 
diff --git a/src/test/ui/issues/issue-63364.stderr b/src/test/ui/issues/issue-63364.stderr
index 60ff318f35a..0375359aeab 100644
--- a/src/test/ui/issues/issue-63364.stderr
+++ b/src/test/ui/issues/issue-63364.stderr
@@ -5,6 +5,7 @@ LL |     for n in 100_000.. {
    |              ^^^^^^^
    |
    = note: `#[deny(overflowing_literals)]` on by default
+   = note: the literal `100_000` does not fit into the type `u16` whose range is `0..=65535`
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/lint/deny-overflowing-literals.stderr b/src/test/ui/lint/deny-overflowing-literals.stderr
index 7f59495023e..127dd4127c2 100644
--- a/src/test/ui/lint/deny-overflowing-literals.stderr
+++ b/src/test/ui/lint/deny-overflowing-literals.stderr
@@ -5,6 +5,7 @@ LL |     let x: u8 = 256;
    |                 ^^^
    |
    = note: `#[deny(overflowing_literals)]` on by default
+   = note: the literal `256` does not fit into the type `u8` whose range is `0..=255`
 
 error: range endpoint is out of range for `u8`
   --> $DIR/deny-overflowing-literals.rs:5:14
diff --git a/src/test/ui/lint/lint-range-endpoint-overflow.stderr b/src/test/ui/lint/lint-range-endpoint-overflow.stderr
index dff61e022eb..d2df7372741 100644
--- a/src/test/ui/lint/lint-range-endpoint-overflow.stderr
+++ b/src/test/ui/lint/lint-range-endpoint-overflow.stderr
@@ -15,18 +15,24 @@ error: literal out of range for `u8`
    |
 LL |     let range_c = 0..=256;
    |                       ^^^
+   |
+   = note: the literal `256` does not fit into the type `u8` whose range is `0..=255`
 
 error: literal out of range for `u8`
   --> $DIR/lint-range-endpoint-overflow.rs:7:19
    |
 LL |     let range_d = 256..5;
    |                   ^^^
+   |
+   = note: the literal `256` does not fit into the type `u8` whose range is `0..=255`
 
 error: literal out of range for `u8`
   --> $DIR/lint-range-endpoint-overflow.rs:8:22
    |
 LL |     let range_e = 0..257;
    |                      ^^^
+   |
+   = note: the literal `257` does not fit into the type `u8` whose range is `0..=255`
 
 error: range endpoint is out of range for `u8`
   --> $DIR/lint-range-endpoint-overflow.rs:9:20
diff --git a/src/test/ui/lint/lint-type-limits2.stderr b/src/test/ui/lint/lint-type-limits2.stderr
index bf510823b56..1e3c88dfc46 100644
--- a/src/test/ui/lint/lint-type-limits2.stderr
+++ b/src/test/ui/lint/lint-type-limits2.stderr
@@ -17,6 +17,7 @@ note: the lint level is defined here
    |
 LL | #![warn(overflowing_literals)]
    |         ^^^^^^^^^^^^^^^^^^^^
+   = note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/lint/lint-type-limits3.stderr b/src/test/ui/lint/lint-type-limits3.stderr
index 00441f99e60..150e9a2aa47 100644
--- a/src/test/ui/lint/lint-type-limits3.stderr
+++ b/src/test/ui/lint/lint-type-limits3.stderr
@@ -17,6 +17,7 @@ note: the lint level is defined here
    |
 LL | #![warn(overflowing_literals)]
    |         ^^^^^^^^^^^^^^^^^^^^
+   = note: the literal `200` does not fit into the type `i8` whose range is `-128..=127`
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/lint/lint-type-overflow.stderr b/src/test/ui/lint/lint-type-overflow.stderr
index ec15313158d..7715c0d3a4d 100644
--- a/src/test/ui/lint/lint-type-overflow.stderr
+++ b/src/test/ui/lint/lint-type-overflow.stderr
@@ -9,108 +9,143 @@ note: the lint level is defined here
    |
 LL | #![deny(overflowing_literals)]
    |         ^^^^^^^^^^^^^^^^^^^^
+   = note: the literal `256` does not fit into the type `u8` whose range is `0..=255`
 
 error: literal out of range for `u8`
   --> $DIR/lint-type-overflow.rs:13:14
    |
 LL |     let x1 = 256_u8;
    |              ^^^^^^
+   |
+   = note: the literal `256_u8` does not fit into the type `u8` whose range is `0..=255`
 
 error: literal out of range for `i8`
   --> $DIR/lint-type-overflow.rs:16:18
    |
 LL |     let x1: i8 = 128;
    |                  ^^^
+   |
+   = note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
 
 error: literal out of range for `i8`
   --> $DIR/lint-type-overflow.rs:18:19
    |
 LL |     let x3: i8 = -129;
    |                   ^^^
+   |
+   = note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
 
 error: literal out of range for `i8`
   --> $DIR/lint-type-overflow.rs:19:19
    |
 LL |     let x3: i8 = -(129);
    |                   ^^^^^
+   |
+   = note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
 
 error: literal out of range for `i8`
   --> $DIR/lint-type-overflow.rs:20:20
    |
 LL |     let x3: i8 = -{129};
    |                    ^^^
+   |
+   = note: the literal `129` does not fit into the type `i8` whose range is `-128..=127`
 
 error: literal out of range for `i8`
   --> $DIR/lint-type-overflow.rs:22:10
    |
 LL |     test(1000);
    |          ^^^^
+   |
+   = note: the literal `1000` does not fit into the type `i8` whose range is `-128..=127`
 
 error: literal out of range for `i8`
   --> $DIR/lint-type-overflow.rs:24:13
    |
 LL |     let x = 128_i8;
    |             ^^^^^^
+   |
+   = note: the literal `128_i8` does not fit into the type `i8` whose range is `-128..=127`
 
 error: literal out of range for `i8`
   --> $DIR/lint-type-overflow.rs:28:14
    |
 LL |     let x = -129_i8;
    |              ^^^^^^
+   |
+   = note: the literal `129_i8` does not fit into the type `i8` whose range is `-128..=127`
 
 error: literal out of range for `i32`
   --> $DIR/lint-type-overflow.rs:32:18
    |
 LL |     let x: i32 = 2147483648;
    |                  ^^^^^^^^^^
+   |
+   = note: the literal `2147483648` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
 
 error: literal out of range for `i32`
   --> $DIR/lint-type-overflow.rs:33:13
    |
 LL |     let x = 2147483648_i32;
    |             ^^^^^^^^^^^^^^
+   |
+   = note: the literal `2147483648_i32` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
 
 error: literal out of range for `i32`
   --> $DIR/lint-type-overflow.rs:36:19
    |
 LL |     let x: i32 = -2147483649;
    |                   ^^^^^^^^^^
+   |
+   = note: the literal `2147483649` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
 
 error: literal out of range for `i32`
   --> $DIR/lint-type-overflow.rs:37:14
    |
 LL |     let x = -2147483649_i32;
    |              ^^^^^^^^^^^^^^
+   |
+   = note: the literal `2147483649_i32` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
 
 error: literal out of range for `i32`
   --> $DIR/lint-type-overflow.rs:38:13
    |
 LL |     let x = 2147483648;
    |             ^^^^^^^^^^
+   |
+   = note: the literal `2147483648` does not fit into the type `i32` whose range is `-2147483648..=2147483647`
 
 error: literal out of range for `i64`
   --> $DIR/lint-type-overflow.rs:40:13
    |
 LL |     let x = 9223372036854775808_i64;
    |             ^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: the literal `9223372036854775808_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
 
 error: literal out of range for `i64`
   --> $DIR/lint-type-overflow.rs:42:13
    |
 LL |     let x = 18446744073709551615_i64;
    |             ^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: the literal `18446744073709551615_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
 
 error: literal out of range for `i64`
   --> $DIR/lint-type-overflow.rs:43:19
    |
 LL |     let x: i64 = -9223372036854775809;
    |                   ^^^^^^^^^^^^^^^^^^^
+   |
+   = note: the literal `9223372036854775809` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
 
 error: literal out of range for `i64`
   --> $DIR/lint-type-overflow.rs:44:14
    |
 LL |     let x = -9223372036854775809_i64;
    |              ^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: the literal `9223372036854775809_i64` does not fit into the type `i64` whose range is `-9223372036854775808..=9223372036854775807`
 
 error: aborting due to 18 previous errors
 
diff --git a/src/test/ui/lint/lint-type-overflow2.stderr b/src/test/ui/lint/lint-type-overflow2.stderr
index dfc691ab910..61e33b7a260 100644
--- a/src/test/ui/lint/lint-type-overflow2.stderr
+++ b/src/test/ui/lint/lint-type-overflow2.stderr
@@ -9,30 +9,39 @@ note: the lint level is defined here
    |
 LL | #![deny(overflowing_literals)]
    |         ^^^^^^^^^^^^^^^^^^^^
+   = note: the literal `128` does not fit into the type `i8` whose range is `-128..=127`
 
 error: literal out of range for `f32`
   --> $DIR/lint-type-overflow2.rs:9:14
    |
 LL |     let x = -3.40282357e+38_f32;
    |              ^^^^^^^^^^^^^^^^^^
+   |
+   = note: the literal `3.40282357e+38_f32` does not fit into the type `f32` and will be converted to `std::f32::INFINITY`
 
 error: literal out of range for `f32`
   --> $DIR/lint-type-overflow2.rs:10:14
    |
 LL |     let x =  3.40282357e+38_f32;
    |              ^^^^^^^^^^^^^^^^^^
+   |
+   = note: the literal `3.40282357e+38_f32` does not fit into the type `f32` and will be converted to `std::f32::INFINITY`
 
 error: literal out of range for `f64`
   --> $DIR/lint-type-overflow2.rs:11:14
    |
 LL |     let x = -1.7976931348623159e+308_f64;
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: the literal `1.7976931348623159e+308_f64` does not fit into the type `f64` and will be converted to `std::f64::INFINITY`
 
 error: literal out of range for `f64`
   --> $DIR/lint-type-overflow2.rs:12:14
    |
 LL |     let x =  1.7976931348623159e+308_f64;
    |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: the literal `1.7976931348623159e+308_f64` does not fit into the type `f64` and will be converted to `std::f64::INFINITY`
 
 error: aborting due to 5 previous errors
 
diff --git a/src/test/ui/lint/type-overflow.stderr b/src/test/ui/lint/type-overflow.stderr
index 2432eb78b87..a7a788b877a 100644
--- a/src/test/ui/lint/type-overflow.stderr
+++ b/src/test/ui/lint/type-overflow.stderr
@@ -9,6 +9,7 @@ note: the lint level is defined here
    |
 LL | #![warn(overflowing_literals)]
    |         ^^^^^^^^^^^^^^^^^^^^
+   = note: the literal `255i8` does not fit into the type `i8` whose range is `-128..=127`
 
 warning: literal out of range for i8
   --> $DIR/type-overflow.rs:10:16
@@ -16,7 +17,7 @@ warning: literal out of range for i8
 LL |     let fail = 0b1000_0001i8;
    |                ^^^^^^^^^^^^^ help: consider using `u8` instead: `0b1000_0001u8`
    |
-   = note: the literal `0b1000_0001i8` (decimal `129`) does not fit into an `i8` and will become `-127i8`
+   = note: the literal `0b1000_0001i8` (decimal `129`) does not fit into the type `i8` and will become `-127i8`
 
 warning: literal out of range for i64
   --> $DIR/type-overflow.rs:12:16
@@ -24,7 +25,7 @@ warning: literal out of range for i64
 LL |     let fail = 0x8000_0000_0000_0000i64;
    |                ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `u64` instead: `0x8000_0000_0000_0000u64`
    |
-   = note: the literal `0x8000_0000_0000_0000i64` (decimal `9223372036854775808`) does not fit into an `i64` and will become `-9223372036854775808i64`
+   = note: the literal `0x8000_0000_0000_0000i64` (decimal `9223372036854775808`) does not fit into the type `i64` and will become `-9223372036854775808i64`
 
 warning: literal out of range for u32
   --> $DIR/type-overflow.rs:14:16
@@ -32,7 +33,7 @@ warning: literal out of range for u32
 LL |     let fail = 0x1_FFFF_FFFFu32;
    |                ^^^^^^^^^^^^^^^^ help: consider using `u64` instead: `0x1_FFFF_FFFFu64`
    |
-   = note: the literal `0x1_FFFF_FFFFu32` (decimal `8589934591`) does not fit into an `u32` and will become `4294967295u32`
+   = note: the literal `0x1_FFFF_FFFFu32` (decimal `8589934591`) does not fit into the type `u32` and will become `4294967295u32`
 
 warning: literal out of range for i128
   --> $DIR/type-overflow.rs:16:22
@@ -40,7 +41,7 @@ warning: literal out of range for i128
 LL |     let fail: i128 = 0x8000_0000_0000_0000_0000_0000_0000_0000;
    |                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
-   = note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0000` (decimal `170141183460469231731687303715884105728`) does not fit into an `i128` and will become `-170141183460469231731687303715884105728i128`
+   = note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0000` (decimal `170141183460469231731687303715884105728`) does not fit into the type `i128` and will become `-170141183460469231731687303715884105728i128`
    = help: consider using `u128` instead
 
 warning: literal out of range for i32
@@ -49,7 +50,7 @@ warning: literal out of range for i32
 LL |     let fail = 0x8FFF_FFFF_FFFF_FFFE;
    |                ^^^^^^^^^^^^^^^^^^^^^
    |
-   = note: the literal `0x8FFF_FFFF_FFFF_FFFE` (decimal `10376293541461622782`) does not fit into an `i32` and will become `-2i32`
+   = note: the literal `0x8FFF_FFFF_FFFF_FFFE` (decimal `10376293541461622782`) does not fit into the type `i32` and will become `-2i32`
    = help: consider using `i128` instead
 
 warning: literal out of range for i8
@@ -58,5 +59,5 @@ warning: literal out of range for i8
 LL |     let fail = -0b1111_1111i8;
    |                 ^^^^^^^^^^^^^ help: consider using `i16` instead: `0b1111_1111i16`
    |
-   = note: the literal `0b1111_1111i8` (decimal `255`) does not fit into an `i8` and will become `-1i8`
+   = note: the literal `0b1111_1111i8` (decimal `255`) does not fit into the type `i8` and will become `-1i8`