Skip to content

Commit 4c8d210

Browse files
committed
Improve semantics of int_ty_max functions
1 parent 38c4885 commit 4c8d210

File tree

1 file changed

+26
-34
lines changed

1 file changed

+26
-34
lines changed

compiler/rustc_ast_lowering/src/format.rs

+26-34
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,6 @@ use rustc_span::{
1212
};
1313
use std::borrow::Cow;
1414

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-
4315
impl<'hir> LoweringContext<'_, 'hir> {
4416
pub(crate) fn lower_format_args(&mut self, sp: Span, fmt: &FormatArgs) -> hir::ExprKind<'hir> {
4517
// Never call the const constructor of `fmt::Arguments` if the
@@ -58,21 +30,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
5830
match LitKind::from_token_lit(lit) {
5931
Ok(LitKind::Str(s, _)) => Some(s),
6032
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;
6533
match ty {
6634
// unsuffixed integer literals are assumed to be i32's
6735
LitIntType::Unsuffixed => {
6836
(n <= i32::MAX as u128).then_some(Symbol::intern(&n.to_string()))
6937
}
7038
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);
7240
(n <= max_literal).then_some(Symbol::intern(&n.to_string()))
7341
}
7442
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);
7644
(n <= max_literal).then_some(Symbol::intern(&n.to_string()))
7745
}
7846
}
@@ -81,6 +49,30 @@ impl<'hir> LoweringContext<'_, 'hir> {
8149
}
8250
}
8351

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+
8476
/// Inline literals into the format string.
8577
///
8678
/// Turns

0 commit comments

Comments
 (0)