-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Show the values and computation that would overflow a const evaluation or propagation #73513
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
Merged
+1,273
−986
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
use crate::mir::interpret::truncate; | ||
use rustc_target::abi::Size; | ||
|
||
#[derive(Copy, Clone)] | ||
/// A type for representing any integer. Only used for printing. | ||
// FIXME: Use this for the integer-tree representation needed for type level ints and | ||
// const generics? | ||
pub struct ConstInt { | ||
/// Number of bytes of the integer. Only 1, 2, 4, 8, 16 are legal values. | ||
size: u8, | ||
/// Whether the value is of a signed integer type. | ||
signed: bool, | ||
/// Whether the value is a `usize` or `isize` type. | ||
is_ptr_sized_integral: bool, | ||
/// Raw memory of the integer. All bytes beyond the `size` are unused and must be zero. | ||
raw: u128, | ||
} | ||
|
||
impl ConstInt { | ||
pub fn new(raw: u128, size: Size, signed: bool, is_ptr_sized_integral: bool) -> Self { | ||
assert!(raw <= truncate(u128::MAX, size)); | ||
Self { raw, size: size.bytes() as u8, signed, is_ptr_sized_integral } | ||
} | ||
} | ||
|
||
impl std::fmt::Debug for ConstInt { | ||
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let Self { size, signed, raw, is_ptr_sized_integral } = *self; | ||
if signed { | ||
let bit_size = size * 8; | ||
let min = 1u128 << (bit_size - 1); | ||
let max = min - 1; | ||
if raw == min { | ||
match (size, is_ptr_sized_integral) { | ||
(_, true) => write!(fmt, "isize::MIN"), | ||
(1, _) => write!(fmt, "i8::MIN"), | ||
(2, _) => write!(fmt, "i16::MIN"), | ||
(4, _) => write!(fmt, "i32::MIN"), | ||
(8, _) => write!(fmt, "i64::MIN"), | ||
(16, _) => write!(fmt, "i128::MIN"), | ||
_ => bug!("ConstInt 0x{:x} with size = {} and signed = {}", raw, size, signed), | ||
} | ||
} else if raw == max { | ||
match (size, is_ptr_sized_integral) { | ||
(_, true) => write!(fmt, "isize::MAX"), | ||
(1, _) => write!(fmt, "i8::MAX"), | ||
(2, _) => write!(fmt, "i16::MAX"), | ||
(4, _) => write!(fmt, "i32::MAX"), | ||
(8, _) => write!(fmt, "i64::MAX"), | ||
(16, _) => write!(fmt, "i128::MAX"), | ||
_ => bug!("ConstInt 0x{:x} with size = {} and signed = {}", raw, size, signed), | ||
} | ||
} else { | ||
match size { | ||
1 => write!(fmt, "{}", raw as i8)?, | ||
2 => write!(fmt, "{}", raw as i16)?, | ||
4 => write!(fmt, "{}", raw as i32)?, | ||
8 => write!(fmt, "{}", raw as i64)?, | ||
16 => write!(fmt, "{}", raw as i128)?, | ||
_ => bug!("ConstInt 0x{:x} with size = {} and signed = {}", raw, size, signed), | ||
} | ||
if fmt.alternate() { | ||
match (size, is_ptr_sized_integral) { | ||
(_, true) => write!(fmt, "_isize")?, | ||
(1, _) => write!(fmt, "_i8")?, | ||
(2, _) => write!(fmt, "_i16")?, | ||
(4, _) => write!(fmt, "_i32")?, | ||
(8, _) => write!(fmt, "_i64")?, | ||
(16, _) => write!(fmt, "_i128")?, | ||
_ => bug!(), | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} else { | ||
let max = truncate(u128::MAX, Size::from_bytes(size)); | ||
if raw == max { | ||
match (size, is_ptr_sized_integral) { | ||
(_, true) => write!(fmt, "usize::MAX"), | ||
(1, _) => write!(fmt, "u8::MAX"), | ||
(2, _) => write!(fmt, "u16::MAX"), | ||
(4, _) => write!(fmt, "u32::MAX"), | ||
(8, _) => write!(fmt, "u64::MAX"), | ||
(16, _) => write!(fmt, "u128::MAX"), | ||
_ => bug!("ConstInt 0x{:x} with size = {} and signed = {}", raw, size, signed), | ||
} | ||
} else { | ||
match size { | ||
1 => write!(fmt, "{}", raw as u8)?, | ||
2 => write!(fmt, "{}", raw as u16)?, | ||
4 => write!(fmt, "{}", raw as u32)?, | ||
8 => write!(fmt, "{}", raw as u64)?, | ||
16 => write!(fmt, "{}", raw as u128)?, | ||
_ => bug!("ConstInt 0x{:x} with size = {} and signed = {}", raw, size, signed), | ||
} | ||
if fmt.alternate() { | ||
match (size, is_ptr_sized_integral) { | ||
(_, true) => write!(fmt, "_usize")?, | ||
(1, _) => write!(fmt, "_u8")?, | ||
(2, _) => write!(fmt, "_u16")?, | ||
(4, _) => write!(fmt, "_u32")?, | ||
(8, _) => write!(fmt, "_u64")?, | ||
(16, _) => write!(fmt, "_u128")?, | ||
_ => bug!(), | ||
} | ||
} | ||
Ok(()) | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.