Skip to content

Commit

Permalink
Detect overflow when the literal is larger than i128::MAX
Browse files Browse the repository at this point in the history
  • Loading branch information
s-cerevisiae committed Jan 8, 2025
1 parent 6f2ca60 commit be23697
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions compiler/rustc_lint/src/types/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,15 @@ fn get_type_suggestion(t: Ty<'_>, val: u128, negative: bool) -> Option<&'static
ty::Uint(_) => Some(Integer::fit_unsigned(val).uint_ty_str()),
ty::Int(_) if negative => Some(Integer::fit_signed(-(val as i128)).int_ty_str()),
ty::Int(int) => {
let signed = Integer::fit_signed(val as i128);
let unsigned = Integer::fit_unsigned(val);
Some(if Some(unsigned.size().bits()) == int.bit_width() {
unsigned.uint_ty_str()
Some(if let Ok(signed) = i128::try_from(val).map(Integer::fit_signed) {
if Some(unsigned.size().bits()) == int.bit_width() {
unsigned.uint_ty_str()
} else {
signed.int_ty_str()
}
} else {
signed.int_ty_str()
unsigned.uint_ty_str()
})
}
_ => None,
Expand Down

0 comments on commit be23697

Please sign in to comment.