Skip to content

Commit b0ae218

Browse files
committed
Fix #116631
1 parent 6d05c43 commit b0ae218

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

compiler/rustc_ast/src/ast.rs

+20
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,26 @@ pub enum LitIntType {
17951795
Unsuffixed,
17961796
}
17971797

1798+
impl LitIntType {
1799+
pub const fn max_literal_value(&self) -> u128 {
1800+
match self {
1801+
LitIntType::Signed(IntTy::I8) => i8::MAX as u128,
1802+
LitIntType::Signed(IntTy::I16) => i16::MAX as u128,
1803+
LitIntType::Signed(IntTy::I32) => i32::MAX as u128,
1804+
LitIntType::Signed(IntTy::I64) => i64::MAX as u128,
1805+
LitIntType::Signed(IntTy::I128) => i128::MAX as u128,
1806+
LitIntType::Signed(IntTy::Isize) => isize::MAX as u128,
1807+
LitIntType::Unsigned(UintTy::U8) => u8::MAX as u128,
1808+
LitIntType::Unsigned(UintTy::U16) => u16::MAX as u128,
1809+
LitIntType::Unsigned(UintTy::U32) => u32::MAX as u128,
1810+
LitIntType::Unsigned(UintTy::U64) => u64::MAX as u128,
1811+
LitIntType::Unsigned(UintTy::U128) => u128::MAX,
1812+
LitIntType::Unsigned(UintTy::Usize) => usize::MAX as u128,
1813+
LitIntType::Unsuffixed => u128::MAX,
1814+
}
1815+
}
1816+
}
1817+
17981818
/// Type of the float literal based on provided suffix.
17991819
#[derive(Clone, Copy, Encodable, Decodable, Debug, Hash, Eq, PartialEq)]
18001820
#[derive(HashStable_Generic)]

compiler/rustc_ast_lowering/src/format.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,13 @@ fn inline_literals(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
128128
{
129129
literal = Some(s);
130130
} else if let token::LitKind::Integer = lit.kind
131-
&& let Ok(LitKind::Int(n, _)) = LitKind::from_token_lit(lit)
131+
&& let Ok(LitKind::Int(n, ty)) = LitKind::from_token_lit(lit)
132132
{
133-
literal = Some(Symbol::intern(&n.to_string()));
133+
// Check if n fits in the type of the argument. If it doesn't,
134+
// simply don't inline the literal here, the error will be emitted at a later stage.
135+
if n <= ty.max_literal_value() {
136+
literal = Some(Symbol::intern(&n.to_string()));
137+
}
134138
}
135139
}
136140

tests/ui/fmt/issue-116631.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
format_args!("{}\n", 0xffff_ffff_u8); //~ ERROR literal out of range for `u8`
3+
}

tests/ui/fmt/issue-116631.stderr

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: literal out of range for `u8`
2+
--> $DIR/issue-116631.rs:2:26
3+
|
4+
LL | format_args!("{}\n", 0xffff_ffff_u8);
5+
| ^^^^^^^^^^^^^^ help: consider using the type `u32` instead: `0xffff_ffff_u32`
6+
|
7+
= note: the literal `0xffff_ffff_u8` (decimal `4294967295`) does not fit into the type `u8` and will become `255u8`
8+
= note: `#[deny(overflowing_literals)]` on by default
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)