-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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, | ||||||
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, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Unsuffixed and unconstrained integer literals should have type 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)] | ||||||
|
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` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this have a test for each of the different integer types? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). |
||
} |
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 | ||
|
There was a problem hiding this comment.
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'sisize
. Same forusize
below.There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
andusize
fromtcx.data_layout.pointer_size
. That will return aSize
, which has thesigned_int_max
andunsigned_int_max
methods that seem useful. It looks like the nearesttcx
is the one in theLoweringContext
, which you get asself
from here:rust/compiler/rustc_ast_lowering/src/format.rs
Line 15 in b0ae218