Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: overflowing bin hex #136424

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/types/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/lint/type-overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}
19 changes: 16 additions & 3 deletions tests/ui/lint/type-overflow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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

Loading