@@ -202,49 +202,23 @@ fn report_bin_hex_error(
202202 )
203203}
204204
205- // This function finds the next fitting type and generates a suggestion string.
206- // It searches for fitting types in the following way (`X < Y`):
207- // - `iX`: if literal fits in `uX` => `uX`, else => `iY`
208- // - `-iX` => `iY`
209- // - `uX` => `uY`
205+ // Find the "next" fitting integer and return a suggestion string
210206//
211- // No suggestion for: `isize`, `usize` .
207+ // No suggestion is offered for `{i,u}size`. Otherwise, we try to suggest an equal-sized type .
212208fn get_type_suggestion ( t : Ty < ' _ > , val : u128 , negative : bool ) -> Option < & ' static str > {
213- use ty:: IntTy :: * ;
214- use ty:: UintTy :: * ;
215- macro_rules! find_fit {
216- ( $ty: expr, $val: expr, $negative: expr,
217- $( $type: ident => [ $( $utypes: expr) ,* ] => [ $( $itypes: expr) ,* ] ) ,+) => {
218- {
219- let _neg = if negative { 1 } else { 0 } ;
220- match $ty {
221- $( $type => {
222- $( if !negative && val <= uint_ty_range( $utypes) . 1 {
223- return Some ( $utypes. name_str( ) )
224- } ) *
225- $( if val <= int_ty_range( $itypes) . 1 as u128 + _neg {
226- return Some ( $itypes. name_str( ) )
227- } ) *
228- None
229- } , ) +
230- _ => None
231- }
232- }
233- }
234- }
235209 match t. kind ( ) {
236- ty:: Int ( i ) => find_fit ! ( i , val , negative ,
237- I8 => [ U8 ] => [ I16 , I32 , I64 , I128 ] ,
238- I16 => [ U16 ] => [ I32 , I64 , I128 ] ,
239- I32 => [ U32 ] => [ I64 , I128 ] ,
240- I64 => [ U64 ] => [ I128 ] ,
241- I128 => [ U128 ] => [ ] ) ,
242- ty :: Uint ( u ) => find_fit ! ( u , val , negative ,
243- U8 => [ U8 , U16 , U32 , U64 , U128 ] => [ ] ,
244- U16 => [ U16 , U32 , U64 , U128 ] => [ ] ,
245- U32 => [ U32 , U64 , U128 ] => [ ] ,
246- U64 => [ U64 , U128 ] => [ ] ,
247- U128 => [ U128 ] => [ ] ) ,
210+ ty:: Uint ( ty :: UintTy :: Usize ) | ty :: Int ( ty :: IntTy :: Isize ) => None ,
211+ ty :: Uint ( _ ) => Some ( Integer :: fit_unsigned ( val ) . uint_ty_str ( ) ) ,
212+ ty :: Int ( _ ) if negative => Some ( Integer :: fit_signed ( - ( val as i128 ) ) . int_ty_str ( ) ) ,
213+ ty :: Int ( int ) => {
214+ let signed = Integer :: fit_signed ( val as i128 ) ;
215+ let unsigned = Integer :: fit_unsigned ( val ) ;
216+ Some ( if Some ( unsigned . size ( ) . bits ( ) ) == int . bit_width ( ) {
217+ unsigned . uint_ty_str ( )
218+ } else {
219+ signed . int_ty_str ( )
220+ } )
221+ }
248222 _ => None ,
249223 }
250224}
0 commit comments