about summary refs log tree commit diff
diff options
context:
space:
mode:
author许杰友 Jieyou Xu (Joe) <39484203+jieyouxu@users.noreply.github.com>2025-02-28 21:41:58 +0800
committerGitHub <noreply@github.com>2025-02-28 21:41:58 +0800
commit743f26de6468a971d22a704f52e7e57923f8680a (patch)
tree9613039bcce90a30a44d291542a00b6d6a8e3df0
parent2f581937e1c06adb4607df1b571c0bef6d98e6ec (diff)
parentdbf8fe068a30b9dc0186410e3e977cabe4e5aedb (diff)
downloadrust-743f26de6468a971d22a704f52e7e57923f8680a.tar.gz
rust-743f26de6468a971d22a704f52e7e57923f8680a.zip
Rollup merge of #136424 - 11happy:overflow.hex.fix, r=fmease
fix: overflowing bin hex

**Overview:**
- This PR fixes #135404.

**Testing**
- Tested the updated functionality.
- previously emitted diagnostics:
```bash
error: literal out of range for `i32`
 --> src/main.rs:2:9
  |
2 |     _ = 0x8FFF_FFFF_FFFF_FFFE;
  |         ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: the literal `0x8FFF_FFFF_FFFF_FFFE` (decimal `10376293541461622782`) does not fit into the type `i32` and will become `-2i32`
  = help: consider using the type `i128` instead
  = note: `#[deny(overflowing_literals)]` on by default
help: to use as a negative number (decimal `-2`), consider using the type `u32` for the literal and cast it to `i32`
  |
2 |     _ = 0x8FFF_FFFF_FFFF_FFFEu32 as i32;
  |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ```
- current diagnostics:
```bash
error: literal out of range for `i32`
 --> ../temp.rs:2:13
  |
2 |     let x = 0x8FFF_FFFF_FFFF_FFFE;
  |             ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: the literal `0x8FFF_FFFF_FFFF_FFFE` (decimal `10376293541461622782`) does not fit into the type `i32` and will become `-2i32`
  = help: consider using the type `u64` instead
  = note: `#[deny(overflowing_literals)]` on by default
help: to use as a negative number (decimal `-2`), consider using the type `u64` for the literal and cast it to `i32`
  |
2 |     let x = 0x8FFF_FFFF_FFFF_FFFEu64 as i32;
  |             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
-rw-r--r--compiler/rustc_lint/src/types/literal.rs2
-rw-r--r--tests/ui/lint/type-overflow.rs6
-rw-r--r--tests/ui/lint/type-overflow.stderr19
3 files changed, 22 insertions, 5 deletions
diff --git a/compiler/rustc_lint/src/types/literal.rs b/compiler/rustc_lint/src/types/literal.rs
index 3e4532a6dbe..7cb00262b6f 100644
--- a/compiler/rustc_lint/src/types/literal.rs
+++ b/compiler/rustc_lint/src/types/literal.rs
@@ -186,7 +186,7 @@ fn report_bin_hex_error(
                 lit_no_suffix,
                 negative_val: actually.clone(),
                 int_ty: int_ty.name_str(),
-                uint_ty: int_ty.to_unsigned().name_str(),
+                uint_ty: Integer::fit_unsigned(val).uint_ty_str(),
             })
         })
         .flatten();
diff --git a/tests/ui/lint/type-overflow.rs b/tests/ui/lint/type-overflow.rs
index 1e74a8925f6..16a021da065 100644
--- a/tests/ui/lint/type-overflow.rs
+++ b/tests/ui/lint/type-overflow.rs
@@ -41,8 +41,12 @@ fn main() {
 
     let fail = 0x8FFF_FFFF_FFFF_FFFE; //~WARNING literal out of range for `i32`
     //~| HELP consider using the type `u64` instead
-    //~| HELP
+    //~| HELP consider using the type `u64` for the literal and cast it to `i32`
 
     let fail = -0b1111_1111i8; //~WARNING literal out of range for `i8`
     //~| HELP consider using the type `i16` instead
+
+    let fail = 0x8000_0000_0000_0000_0000_0000_FFFF_FFFE; //~WARNING literal out of range for `i32`
+    //~| HELP consider using the type `u128` instead
+    //~| HELP consider using the type `u128` for the literal and cast it to `i32`
 }
diff --git a/tests/ui/lint/type-overflow.stderr b/tests/ui/lint/type-overflow.stderr
index 4d6403b1e7d..065c530adcf 100644
--- a/tests/ui/lint/type-overflow.stderr
+++ b/tests/ui/lint/type-overflow.stderr
@@ -112,9 +112,9 @@ LL |     let fail = 0x8FFF_FFFF_FFFF_FFFE;
    |
    = note: the literal `0x8FFF_FFFF_FFFF_FFFE` (decimal `10376293541461622782`) does not fit into the type `i32` and will become `-2i32`
    = help: consider using the type `u64` instead
-help: to use as a negative number (decimal `-2`), consider using the type `u32` for the literal and cast it to `i32`
+help: to use as a negative number (decimal `-2`), consider using the type `u64` for the literal and cast it to `i32`
    |
-LL |     let fail = 0x8FFF_FFFF_FFFF_FFFEu32 as i32;
+LL |     let fail = 0x8FFF_FFFF_FFFF_FFFEu64 as i32;
    |                                     ++++++++++
 
 warning: literal out of range for `i8`
@@ -126,5 +126,18 @@ LL |     let fail = -0b1111_1111i8;
    = note: the literal `0b1111_1111i8` (decimal `255`) does not fit into the type `i8`
    = note: and the value `-0b1111_1111i8` will become `1i8`
 
-warning: 11 warnings emitted
+warning: literal out of range for `i32`
+  --> $DIR/type-overflow.rs:49:16
+   |
+LL |     let fail = 0x8000_0000_0000_0000_0000_0000_FFFF_FFFE;
+   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+   |
+   = note: the literal `0x8000_0000_0000_0000_0000_0000_FFFF_FFFE` (decimal `170141183460469231731687303720179073022`) does not fit into the type `i32` and will become `-2i32`
+   = help: consider using the type `u128` instead
+help: to use as a negative number (decimal `-2`), consider using the type `u128` for the literal and cast it to `i32`
+   |
+LL |     let fail = 0x8000_0000_0000_0000_0000_0000_FFFF_FFFEu128 as i32;
+   |                                                         +++++++++++
+
+warning: 12 warnings emitted