Skip to content

Commit b0d04c3

Browse files
committed
Auto merge of #102315 - RalfJung:assert_unsafe_precondition, r=thomcc
add a few more assert_unsafe_precondition Add debug-assertion checking for `ptr.read()`, `ptr.write(_)`, and `unreachable_unchecked.` This is quite useful for [cargo-careful](https://github.com/RalfJung/cargo-careful).
2 parents 1211bfc + 568162d commit b0d04c3

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

core/src/hint.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ use crate::intrinsics;
100100
pub const unsafe fn unreachable_unchecked() -> ! {
101101
// SAFETY: the safety contract for `intrinsics::unreachable` must
102102
// be upheld by the caller.
103-
unsafe { intrinsics::unreachable() }
103+
unsafe {
104+
intrinsics::assert_unsafe_precondition!(() => false);
105+
intrinsics::unreachable()
106+
}
104107
}
105108

106109
/// Emits a machine instruction to signal the processor that it is running in

core/src/ptr/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,7 @@ pub const unsafe fn read<T>(src: *const T) -> T {
11141114
// Also, since we just wrote a valid value into `tmp`, it is guaranteed
11151115
// to be properly initialized.
11161116
unsafe {
1117+
assert_unsafe_precondition!([T](src: *const T) => is_aligned_and_not_null(src));
11171118
copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
11181119
tmp.assume_init()
11191120
}
@@ -1307,6 +1308,7 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
13071308
// `dst` cannot overlap `src` because the caller has mutable access
13081309
// to `dst` while `src` is owned by this function.
13091310
unsafe {
1311+
assert_unsafe_precondition!([T](dst: *mut T) => is_aligned_and_not_null(dst));
13101312
copy_nonoverlapping(&src as *const T, dst, 1);
13111313
intrinsics::forget(src);
13121314
}

0 commit comments

Comments
 (0)