Skip to content

Commit daaf9f7

Browse files
committed
Auto merge of #1177 - RalfJung:debug-assert, r=RalfJung
Make sure we evaluate debug assertions in local crate and libstd Fixes #1126
2 parents 0a803c9 + 8acf52b commit daaf9f7

File tree

6 files changed

+34
-7
lines changed

6 files changed

+34
-7
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ pub use crate::stacked_borrows::{
6262
/// Insert rustc arguments at the beginning of the argument list that Miri wants to be
6363
/// set per default, for maximal validation power.
6464
pub fn miri_default_args() -> &'static [&'static str] {
65-
&["-Zalways-encode-mir", "-Zmir-emit-retag", "-Zmir-opt-level=0", "--cfg=miri"]
65+
&["-Zalways-encode-mir", "-Zmir-emit-retag", "-Zmir-opt-level=0", "--cfg=miri", "-Cdebug-assertions=on"]
6666
}

tests/compile-fail/copy_null.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
//error-pattern: invalid use of NULL pointer
2+
#![feature(intrinsics)]
3+
4+
// Directly call intrinsic to avoid debug assertions in libstd
5+
extern "rust-intrinsic" {
6+
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
7+
}
28

39
fn main() {
410
let mut data = [0u16; 4];
511
let ptr = &mut data[0] as *mut u16;
612
// Even copying 0 elements from NULL should error.
7-
unsafe { ptr.copy_from(std::ptr::null(), 0); }
13+
unsafe { copy_nonoverlapping(std::ptr::null(), ptr, 0); }
814
}
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
#![feature(core_intrinsics)]
2-
31
//error-pattern: copy_nonoverlapping called on overlapping ranges
2+
#![feature(intrinsics)]
3+
4+
// Directly call intrinsic to avoid debug assertions in libstd
5+
extern "rust-intrinsic" {
6+
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
7+
}
48

59
fn main() {
610
let mut data = [0u8; 16];
711
unsafe {
812
let a = data.as_mut_ptr();
913
let b = a.wrapping_offset(1) as *mut _;
10-
std::ptr::copy_nonoverlapping(a, b, 2);
14+
copy_nonoverlapping(a, b, 2);
1115
}
1216
}

tests/compile-fail/copy_unaligned.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
//error-pattern: tried to access memory with alignment 1, but alignment 2 is required
2+
#![feature(intrinsics)]
3+
4+
// Directly call intrinsic to avoid debug assertions in libstd
5+
extern "rust-intrinsic" {
6+
fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
7+
}
28

39
fn main() {
410
let mut data = [0u16; 8];
511
let ptr = (&mut data[0] as *mut u16 as *mut u8).wrapping_add(1) as *mut u16;
612
// Even copying 0 elements to something unaligned should error
7-
unsafe { ptr.copy_from(&data[5], 0); }
13+
unsafe { copy_nonoverlapping(&data[5], ptr, 0); }
814
}

tests/run-pass/panic/catch_panic.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// ignore-windows: Unwind panicking does not currently work on Windows
2+
// normalize-stderr-test "[^ ]*libcore/macros/mod.rs[0-9:]*" -> "$$LOC"
23
#![feature(never_type)]
34
#![allow(const_err)]
45
use std::panic::{catch_unwind, AssertUnwindSafe};
@@ -11,7 +12,6 @@ thread_local! {
1112
}
1213

1314
struct DropTester;
14-
1515
impl Drop for DropTester {
1616
fn drop(&mut self) {
1717
DROPPED.with(|c| {
@@ -61,6 +61,11 @@ fn main() {
6161
test(|_old_val| { let _val = [0, 1, 2][4]; loop {} });
6262
test(|_old_val| { let _val = 1/0; loop {} });
6363

64+
// Assertion and debug assertion
65+
test(|_old_val| { assert!(false); loop {} });
66+
test(|_old_val| { debug_assert!(false); loop {} });
67+
test(|_old_val| { unsafe { (1 as *const i32).read() }; loop {} }); // trigger debug-assertion in libstd
68+
6469
// Cleanup: reset to default hook.
6570
drop(std::panic::take_hook());
6671

tests/run-pass/panic/catch_panic.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 4'
1616
Caught panic message (String): index out of bounds: the len is 3 but the index is 4
1717
thread 'main' panicked at 'attempt to divide by zero', $DIR/catch_panic.rs:62:34
1818
Caught panic message (String): attempt to divide by zero
19+
thread 'main' panicked at 'assertion failed: false', $DIR/catch_panic.rs:65:23
20+
Caught panic message (&str): assertion failed: false
21+
thread 'main' panicked at 'assertion failed: false', $DIR/catch_panic.rs:66:23
22+
Caught panic message (&str): assertion failed: false
23+
thread 'main' panicked at 'attempt to copy from unaligned or null pointer', $LOC
24+
Caught panic message (String): attempt to copy from unaligned or null pointer
1925
Success!

0 commit comments

Comments
 (0)