Skip to content

add a few more assert_unsafe_precondition #102315

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
merged 2 commits into from
Oct 8, 2022
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
5 changes: 4 additions & 1 deletion library/core/src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ use crate::intrinsics;
pub const unsafe fn unreachable_unchecked() -> ! {
// SAFETY: the safety contract for `intrinsics::unreachable` must
// be upheld by the caller.
unsafe { intrinsics::unreachable() }
unsafe {
intrinsics::assert_unsafe_precondition!(() => false);
intrinsics::unreachable()
}
}

/// Emits a machine instruction to signal the processor that it is running in
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,7 @@ pub const unsafe fn read<T>(src: *const T) -> T {
// Also, since we just wrote a valid value into `tmp`, it is guaranteed
// to be properly initialized.
unsafe {
assert_unsafe_precondition!([T](src: *const T) => is_aligned_and_not_null(src));
copy_nonoverlapping(src, tmp.as_mut_ptr(), 1);
tmp.assume_init()
}
Expand Down Expand Up @@ -1307,6 +1308,7 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
// `dst` cannot overlap `src` because the caller has mutable access
// to `dst` while `src` is owned by this function.
unsafe {
assert_unsafe_precondition!([T](dst: *mut T) => is_aligned_and_not_null(dst));
copy_nonoverlapping(&src as *const T, dst, 1);
intrinsics::forget(src);
}
Expand Down
1 change: 1 addition & 0 deletions src/test/codegen/mem-replace-direct-memcpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// known to be `1` after inlining).

// compile-flags: -C no-prepopulate-passes -Zinline-mir=no
// ignore-debug: the debug assertions get in the way

#![crate_type = "lib"]

Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/consts/const_unsafe_unreachable_ub.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
error[E0080]: evaluation of constant value failed
--> $SRC_DIR/core/src/hint.rs:LL:COL
|
LL | unsafe { intrinsics::unreachable() }
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| entering unreachable code
| inside `unreachable_unchecked` at $SRC_DIR/core/src/hint.rs:LL:COL
LL | intrinsics::unreachable()
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| entering unreachable code
| inside `unreachable_unchecked` at $SRC_DIR/core/src/hint.rs:LL:COL
|
::: $DIR/const_unsafe_unreachable_ub.rs:6:18
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

fn main() {
// Try many times as this might work by chance.
for _ in 0..10 {
for _ in 0..20 {
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
let x = &x[0] as *const _ as *const u32;
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ptr;

fn main() {
// Try many times as this might work by chance.
for _ in 0..10 {
for _ in 0..20 {
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
let x = &x[0] as *const _ as *const u32;
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
Expand Down