Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not inline integer literals which are out of range in format_args! #116633

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1795,6 +1795,26 @@ pub enum LitIntType {
Unsuffixed,
}

impl LitIntType {
pub const fn max_literal_value(&self) -> u128 {
match self {
LitIntType::Signed(IntTy::I8) => i8::MAX as u128,
LitIntType::Signed(IntTy::I16) => i16::MAX as u128,
LitIntType::Signed(IntTy::I32) => i32::MAX as u128,
LitIntType::Signed(IntTy::I64) => i64::MAX as u128,
LitIntType::Signed(IntTy::I128) => i128::MAX as u128,
LitIntType::Signed(IntTy::Isize) => isize::MAX as u128,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This uses the host's isize, but should be using use the target's isize. Same for usize below.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. What should I do to obtain these values for the target?

Copy link
Member

@lukas-code lukas-code Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get size of isize and usize from tcx.data_layout.pointer_size. That will return a Size, which has the signed_int_max and unsigned_int_max methods that seem useful. It looks like the nearest tcx is the one in the LoweringContext, which you get as self from here:

pub(crate) fn lower_format_args(&mut self, sp: Span, fmt: &FormatArgs) -> hir::ExprKind<'hir> {

LitIntType::Unsigned(UintTy::U8) => u8::MAX as u128,
LitIntType::Unsigned(UintTy::U16) => u16::MAX as u128,
LitIntType::Unsigned(UintTy::U32) => u32::MAX as u128,
LitIntType::Unsigned(UintTy::U64) => u64::MAX as u128,
LitIntType::Unsigned(UintTy::U128) => u128::MAX,
LitIntType::Unsigned(UintTy::Usize) => usize::MAX as u128,
LitIntType::Unsuffixed => u128::MAX,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LitIntType::Unsuffixed => u128::MAX,
LitIntType::Unsuffixed => i32::MAX,

Unsuffixed and unconstrained integer literals should have type i32, even if they overflow.

See the old behavior before inlining: https://rust.godbolt.org/z/PMG7ac4d4

}
}
}

/// Type of the float literal based on provided suffix.
#[derive(Clone, Copy, Encodable, Decodable, Debug, Hash, Eq, PartialEq)]
#[derive(HashStable_Generic)]
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_ast_lowering/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,13 @@ fn inline_literals(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> {
{
literal = Some(s);
} else if let token::LitKind::Integer = lit.kind
&& let Ok(LitKind::Int(n, _)) = LitKind::from_token_lit(lit)
&& let Ok(LitKind::Int(n, ty)) = LitKind::from_token_lit(lit)
{
literal = Some(Symbol::intern(&n.to_string()));
// Check if n fits in the type of the argument. If it doesn't,
// simply don't inline the literal here, the error will be emitted at a later stage.
if n <= ty.max_literal_value() {
literal = Some(Symbol::intern(&n.to_string()));
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/fmt/issue-116631.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
format_args!("{}\n", 0xffff_ffff_u8); //~ ERROR literal out of range for `u8`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have a test for each of the different integer types?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At the very least it should have a test case that covers #115423 (which I don't interpet is about being out of range, but rather about not respecting the type of the literal).

}
11 changes: 11 additions & 0 deletions tests/ui/fmt/issue-116631.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error: literal out of range for `u8`
--> $DIR/issue-116631.rs:2:26
|
LL | format_args!("{}\n", 0xffff_ffff_u8);
| ^^^^^^^^^^^^^^ help: consider using the type `u32` instead: `0xffff_ffff_u32`
|
= note: the literal `0xffff_ffff_u8` (decimal `4294967295`) does not fit into the type `u8` and will become `255u8`
= note: `#[deny(overflowing_literals)]` on by default

error: aborting due to previous error