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

finally stop using min/max_value and the integer modules #1206

Merged
merged 1 commit into from
Mar 4, 2020
Merged
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
2 changes: 1 addition & 1 deletion rust-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4d71c164a89b705df6affd31a5262c832d1bc48d
7a3700c37132385e8e965c18e73d0a09f9146335
8 changes: 4 additions & 4 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,10 +443,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

// Saturating cast to i16. Even those are outside the valid exponent range to
// `scalbn` below will do its over/underflow handling.
let exp = if exp > i16::max_value() as i32 {
i16::max_value()
} else if exp < i16::min_value() as i32 {
i16::min_value()
let exp = if exp > i16::MAX as i32 {
i16::MAX
} else if exp < i16::MIN as i32 {
i16::MIN
Copy link
Member

Choose a reason for hiding this comment

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

Hmm I wonder if we can also make similar changes to rustc_apfloat. Ugh the licensing mess there isn't sorted out yet, is it...?

Copy link
Member Author

Choose a reason for hiding this comment

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

I mean after the hard time you gave me for editing a comment in apfloat, I am not touching that code ever again if I can help it.^^

Copy link
Member

Choose a reason for hiding this comment

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

I am not going to touch it either until I hear back about the licensing, don't worry, this was more of a note to myself.

} else {
exp.try_into().unwrap()
};
Expand Down
8 changes: 4 additions & 4 deletions src/shims/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

// We cap the number of read bytes to the largest value that we are able to fit in both the
// host's and target's `isize`. This saves us from having to handle overflows later.
let count = count.min(this.isize_max() as u64).min(isize::max_value() as u64);
let count = count.min(this.isize_max() as u64).min(isize::MAX as u64);

if let Some(FileHandle { file, writable: _ }) = this.machine.file_handler.handles.get_mut(&fd) {
// This can never fail because `count` was capped to be smaller than
// `isize::max_value()`.
// `isize::MAX`.
let count = isize::try_from(count).unwrap();
// We want to read at most `count` bytes. We are sure that `count` is not negative
// because it was a target's `usize`. Also we are sure that its smaller than
// `usize::max_value()` because it is a host's `isize`.
// `usize::MAX` because it is a host's `isize`.
let mut bytes = vec![0; count as usize];
let result = file
.read(&mut bytes)
Expand Down Expand Up @@ -481,7 +481,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx

// We cap the number of written bytes to the largest value that we are able to fit in both the
// host's and target's `isize`. This saves us from having to handle overflows later.
let count = count.min(this.isize_max() as u64).min(isize::max_value() as u64);
let count = count.min(this.isize_max() as u64).min(isize::MAX as u64);

if let Some(FileHandle { file, writable: _ }) = this.machine.file_handler.handles.get_mut(&fd) {
let bytes = this.memory.read_bytes(buf, Size::from_bytes(count))?;
Expand Down
2 changes: 1 addition & 1 deletion src/shims/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
let (dest, ret) = ret.unwrap();
let n = this
.align_offset(args[0], args[1])?
.unwrap_or_else(|| this.truncate(u128::max_value(), dest.layout));
.unwrap_or_else(|| this.truncate(u128::MAX, dest.layout));
this.write_scalar(Scalar::from_uint(n, dest.layout.size), dest)?;
this.go_to_block(ret);
return Ok(None);
Expand Down
4 changes: 2 additions & 2 deletions tests/compile-fail/exact_div4.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![feature(core_intrinsics)]
fn main() {
// divison of min_value by -1
unsafe { std::intrinsics::exact_div(i64::min_value(), -1); } //~ ERROR result of dividing MIN by -1 cannot be represented
// divison of MIN by -1
unsafe { std::intrinsics::exact_div(i64::MIN, -1); } //~ ERROR result of dividing MIN by -1 cannot be represented
}
2 changes: 1 addition & 1 deletion tests/compile-fail/ptr_offset_overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
fn main() {
let v = [1i8, 2];
let x = &v[1] as *const i8;
let _val = unsafe { x.offset(isize::min_value()) };
let _val = unsafe { x.offset(isize::MIN) };
}
1 change: 0 additions & 1 deletion tests/compile-fail/slice-too-big.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::mem;
use std::usize;

fn main() { unsafe {
let ptr = Box::into_raw(Box::new(0u8));
Expand Down
2 changes: 1 addition & 1 deletion tests/compile-fail/unchecked_div1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![feature(core_intrinsics)]
fn main() {
// MIN/-1 cannot be represented
unsafe { std::intrinsics::unchecked_div(i16::min_value(), -1); } //~ ERROR Overflow executing `unchecked_div`
unsafe { std::intrinsics::unchecked_div(i16::MIN, -1); } //~ ERROR Overflow executing `unchecked_div`
}
6 changes: 3 additions & 3 deletions tests/run-pass/align_offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ fn test_align_offset() {

assert_eq!(raw.align_offset(2), 0);
assert_eq!(raw.align_offset(4), 0);
assert_eq!(raw.align_offset(8), usize::max_value()); // requested alignment higher than allocation alignment
assert_eq!(raw.align_offset(8), usize::MAX); // requested alignment higher than allocation alignment

assert_eq!(raw.wrapping_offset(1).align_offset(2), 1);
assert_eq!(raw.wrapping_offset(1).align_offset(4), 3);
assert_eq!(raw.wrapping_offset(1).align_offset(8), usize::max_value()); // requested alignment higher than allocation alignment
assert_eq!(raw.wrapping_offset(1).align_offset(8), usize::MAX); // requested alignment higher than allocation alignment

assert_eq!(raw.wrapping_offset(2).align_offset(2), 0);
assert_eq!(raw.wrapping_offset(2).align_offset(4), 2);
assert_eq!(raw.wrapping_offset(2).align_offset(8), usize::max_value()); // requested alignment higher than allocation alignment
assert_eq!(raw.wrapping_offset(2).align_offset(8), usize::MAX); // requested alignment higher than allocation alignment
}

fn test_align_to() {
Expand Down
11 changes: 4 additions & 7 deletions tests/run-pass/integer-ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::i32;

pub fn main() {
// This tests that do (not) do sign extension properly when loading integers
assert_eq!(u32::max_value() as i64, 4294967295);
assert_eq!(i32::min_value() as i64, -2147483648);

assert_eq!(i8::min_value(), -128);
assert_eq!(u32::MAX as i64, 4294967295);
assert_eq!(i32::MIN as i64, -2147483648);

assert_eq!(i8::max_value(), 127);
assert_eq!(i8::MAX, 127);
assert_eq!(i8::MIN, -128);

assert_eq!(i32::from_str_radix("A", 16), Ok(10));

Expand Down
8 changes: 4 additions & 4 deletions tests/run-pass/ints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ fn main() {
assert_eq!(arith(), 5*5);
assert_eq!(match_int(), 20);
assert_eq!(match_int_range(), 4);
assert_eq!(i64::min_value().overflowing_mul(-1), (i64::min_value(), true));
assert_eq!(i32::min_value().overflowing_mul(-1), (i32::min_value(), true));
assert_eq!(i16::min_value().overflowing_mul(-1), (i16::min_value(), true));
assert_eq!(i8::min_value().overflowing_mul(-1), (i8::min_value(), true));
assert_eq!(i64::MIN.overflowing_mul(-1), (i64::MIN, true));
assert_eq!(i32::MIN.overflowing_mul(-1), (i32::MIN, true));
assert_eq!(i16::MIN.overflowing_mul(-1), (i16::MIN, true));
assert_eq!(i8::MIN.overflowing_mul(-1), (i8::MIN, true));
}
2 changes: 1 addition & 1 deletion tests/run-pass/panic/overflowing-rsh-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

fn main() {
// Make sure we catch overflows that would be hidden by first casting the RHS to u32
let _n = 1i64 >> (u32::max_value() as i64 + 1);
let _n = 1i64 >> (u32::MAX as i64 + 1);
}
2 changes: 0 additions & 2 deletions tests/run-pass/pointers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::usize;

fn one_line_ref() -> i16 {
*&1
}
Expand Down
4 changes: 2 additions & 2 deletions tests/run-pass/ptr_arith_offset_overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ fn main() {
let x = &mut ptr::null(); // going through memory as there are more sanity checks along that path
*x = v.as_ptr().wrapping_offset(1); // ptr to the 2nd element
// Adding 2*isize::max and then 1 is like substracting 1
*x = x.wrapping_offset(isize::max_value());
*x = x.wrapping_offset(isize::max_value());
*x = x.wrapping_offset(isize::MAX);
*x = x.wrapping_offset(isize::MAX);
*x = x.wrapping_offset(1);
assert_eq!(unsafe { **x }, 1);
}
2 changes: 1 addition & 1 deletion tests/run-pass/ptr_int_casts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
// this used to trigger an ICE on 32bit)
let val = &mut ptr::null();
*val = (1 as *const u8).wrapping_offset(-4);
assert_eq!(*val as usize, usize::max_value() - 2);
assert_eq!(*val as usize, usize::MAX - 2);

{ // ptr-int-ptr
let x = 13;
Expand Down
4 changes: 2 additions & 2 deletions tests/run-pass/u128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ fn main() {
assert_eq!("10000000000000000000000000000000000000000000000000000000000000000000",
format!("{:b}", j));
assert_eq!("340282366920938463463374607431768211455",
format!("{}", u128::max_value()));
format!("{}", u128::MAX));
assert_eq!("147573952589676412928", format!("{:?}", j));
// common traits
assert_eq!(x, b(x.clone()));
// overflow checks
assert_eq!((z).checked_mul(z), Some(0x734C_C2F2_A521));
assert_eq!((k).checked_mul(k), None);
let l: u128 = b(u128::max_value() - 10);
let l: u128 = b(u128::MAX - 10);
let o: u128 = b(17);
assert_eq!(l.checked_add(b(11)), None);
assert_eq!(l.checked_sub(l), Some(0));
Expand Down