@@ -12,34 +12,6 @@ use rustc_span::{
12
12
} ;
13
13
use std:: borrow:: Cow ;
14
14
15
- fn int_ty_max ( int_ty : IntTy ) -> Option < u128 > {
16
- match int_ty {
17
- // isize is platform-dependent, so we should use
18
- // TyCtxt.data_layout.pointer_size
19
- // This is available, for example, from a LoweringContext
20
- IntTy :: Isize => None ,
21
- IntTy :: I8 => Some ( i8:: MAX as u128 ) ,
22
- IntTy :: I16 => Some ( i16:: MAX as u128 ) ,
23
- IntTy :: I32 => Some ( i32:: MAX as u128 ) ,
24
- IntTy :: I64 => Some ( i64:: MAX as u128 ) ,
25
- IntTy :: I128 => Some ( i128:: MAX as u128 ) ,
26
- }
27
- }
28
-
29
- fn uint_ty_max ( uint_ty : UintTy ) -> Option < u128 > {
30
- match uint_ty {
31
- // usize is platform-dependent, so we should use
32
- // TyCtxt.data_layout.pointer_size
33
- // This is available, for example, from a LoweringContext
34
- UintTy :: Usize => None ,
35
- UintTy :: U8 => Some ( u8:: MAX as u128 ) ,
36
- UintTy :: U16 => Some ( u16:: MAX as u128 ) ,
37
- UintTy :: U32 => Some ( u32:: MAX as u128 ) ,
38
- UintTy :: U64 => Some ( u64:: MAX as u128 ) ,
39
- UintTy :: U128 => Some ( u128:: MAX as u128 ) ,
40
- }
41
- }
42
-
43
15
impl < ' hir > LoweringContext < ' _ , ' hir > {
44
16
pub ( crate ) fn lower_format_args ( & mut self , sp : Span , fmt : & FormatArgs ) -> hir:: ExprKind < ' hir > {
45
17
// Never call the const constructor of `fmt::Arguments` if the
@@ -58,21 +30,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
58
30
match LitKind :: from_token_lit ( lit) {
59
31
Ok ( LitKind :: Str ( s, _) ) => Some ( s) ,
60
32
Ok ( LitKind :: Int ( n, ty) ) => {
61
- // platform-dependent usize and isize MAX
62
- let usize_bits = self . tcx . data_layout . pointer_size . bits ( ) ;
63
- let usize_max = if usize_bits >= 128 { u128:: MAX } else { 1u128 << usize_bits - 1 } ;
64
- let isize_max = usize_max >> 1 ;
65
33
match ty {
66
34
// unsuffixed integer literals are assumed to be i32's
67
35
LitIntType :: Unsuffixed => {
68
36
( n <= i32:: MAX as u128 ) . then_some ( Symbol :: intern ( & n. to_string ( ) ) )
69
37
}
70
38
LitIntType :: Signed ( int_ty) => {
71
- let max_literal = int_ty_max ( int_ty) . unwrap_or ( isize_max ) ;
39
+ let max_literal = self . int_ty_max ( int_ty) ;
72
40
( n <= max_literal) . then_some ( Symbol :: intern ( & n. to_string ( ) ) )
73
41
}
74
42
LitIntType :: Unsigned ( uint_ty) => {
75
- let max_literal = uint_ty_max ( uint_ty) . unwrap_or ( usize_max ) ;
43
+ let max_literal = self . uint_ty_max ( uint_ty) ;
76
44
( n <= max_literal) . then_some ( Symbol :: intern ( & n. to_string ( ) ) )
77
45
}
78
46
}
@@ -81,6 +49,30 @@ impl<'hir> LoweringContext<'_, 'hir> {
81
49
}
82
50
}
83
51
52
+ /// Get the maximum value of int_ty. It is platform-dependent due to the byte size of isize
53
+ fn int_ty_max ( & self , int_ty : IntTy ) -> u128 {
54
+ match int_ty {
55
+ IntTy :: Isize => self . tcx . data_layout . pointer_size . signed_int_max ( ) as u128 ,
56
+ IntTy :: I8 => i8:: MAX as u128 ,
57
+ IntTy :: I16 => i16:: MAX as u128 ,
58
+ IntTy :: I32 => i32:: MAX as u128 ,
59
+ IntTy :: I64 => i64:: MAX as u128 ,
60
+ IntTy :: I128 => i128:: MAX as u128 ,
61
+ }
62
+ }
63
+
64
+ /// Get the maximum value of uint_ty. It is platform-dependent due to the byte size of usize
65
+ fn uint_ty_max ( & self , uint_ty : UintTy ) -> u128 {
66
+ match uint_ty {
67
+ UintTy :: Usize => self . tcx . data_layout . pointer_size . unsigned_int_max ( ) ,
68
+ UintTy :: U8 => u8:: MAX as u128 ,
69
+ UintTy :: U16 => u16:: MAX as u128 ,
70
+ UintTy :: U32 => u32:: MAX as u128 ,
71
+ UintTy :: U64 => u64:: MAX as u128 ,
72
+ UintTy :: U128 => u128:: MAX as u128 ,
73
+ }
74
+ }
75
+
84
76
/// Inline literals into the format string.
85
77
///
86
78
/// Turns
0 commit comments