compiler: reuse {un,}signed_fit in get_type_suggestion (nfc)
no need for a weird macro when a self-explanatory `match` will do.
This commit is contained in:
parent
2db62e6893
commit
844edfe449
@ -833,6 +833,28 @@ pub enum Integer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Integer {
|
impl Integer {
|
||||||
|
pub fn int_ty_str(self) -> &'static str {
|
||||||
|
use Integer::*;
|
||||||
|
match self {
|
||||||
|
I8 => "i8",
|
||||||
|
I16 => "i16",
|
||||||
|
I32 => "i32",
|
||||||
|
I64 => "i64",
|
||||||
|
I128 => "i128",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn uint_ty_str(self) -> &'static str {
|
||||||
|
use Integer::*;
|
||||||
|
match self {
|
||||||
|
I8 => "u8",
|
||||||
|
I16 => "u16",
|
||||||
|
I32 => "u32",
|
||||||
|
I64 => "u64",
|
||||||
|
I128 => "u128",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn size(self) -> Size {
|
pub fn size(self) -> Size {
|
||||||
use Integer::*;
|
use Integer::*;
|
||||||
|
@ -202,49 +202,23 @@ fn report_bin_hex_error(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function finds the next fitting type and generates a suggestion string.
|
// Find the "next" fitting integer and return a suggestion string
|
||||||
// It searches for fitting types in the following way (`X < Y`):
|
|
||||||
// - `iX`: if literal fits in `uX` => `uX`, else => `iY`
|
|
||||||
// - `-iX` => `iY`
|
|
||||||
// - `uX` => `uY`
|
|
||||||
//
|
//
|
||||||
// No suggestion for: `isize`, `usize`.
|
// No suggestion is offered for `{i,u}size`. Otherwise, we try to suggest an equal-sized type.
|
||||||
fn get_type_suggestion(t: Ty<'_>, val: u128, negative: bool) -> Option<&'static str> {
|
fn get_type_suggestion(t: Ty<'_>, val: u128, negative: bool) -> Option<&'static str> {
|
||||||
use ty::IntTy::*;
|
|
||||||
use ty::UintTy::*;
|
|
||||||
macro_rules! find_fit {
|
|
||||||
($ty:expr, $val:expr, $negative:expr,
|
|
||||||
$($type:ident => [$($utypes:expr),*] => [$($itypes:expr),*]),+) => {
|
|
||||||
{
|
|
||||||
let _neg = if negative { 1 } else { 0 };
|
|
||||||
match $ty {
|
|
||||||
$($type => {
|
|
||||||
$(if !negative && val <= uint_ty_range($utypes).1 {
|
|
||||||
return Some($utypes.name_str())
|
|
||||||
})*
|
|
||||||
$(if val <= int_ty_range($itypes).1 as u128 + _neg {
|
|
||||||
return Some($itypes.name_str())
|
|
||||||
})*
|
|
||||||
None
|
|
||||||
},)+
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
match t.kind() {
|
match t.kind() {
|
||||||
ty::Int(i) => find_fit!(i, val, negative,
|
ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize) => None,
|
||||||
I8 => [U8] => [I16, I32, I64, I128],
|
ty::Uint(_) => Some(Integer::fit_unsigned(val).uint_ty_str()),
|
||||||
I16 => [U16] => [I32, I64, I128],
|
ty::Int(_) if negative => Some(Integer::fit_signed(-(val as i128)).int_ty_str()),
|
||||||
I32 => [U32] => [I64, I128],
|
ty::Int(int) => {
|
||||||
I64 => [U64] => [I128],
|
let signed = Integer::fit_signed(val as i128);
|
||||||
I128 => [U128] => []),
|
let unsigned = Integer::fit_unsigned(val);
|
||||||
ty::Uint(u) => find_fit!(u, val, negative,
|
Some(if Some(unsigned.size().bits()) == int.bit_width() {
|
||||||
U8 => [U8, U16, U32, U64, U128] => [],
|
unsigned.uint_ty_str()
|
||||||
U16 => [U16, U32, U64, U128] => [],
|
} else {
|
||||||
U32 => [U32, U64, U128] => [],
|
signed.int_ty_str()
|
||||||
U64 => [U64, U128] => [],
|
})
|
||||||
U128 => [U128] => []),
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user