Skip to content

Commit 80b6a04

Browse files
committed
Under Miri, disable debug asserts for things Miri always checks
1 parent 0da281b commit 80b6a04

File tree

5 files changed

+18
-4
lines changed

5 files changed

+18
-4
lines changed

library/core/src/intrinsics.rs

+5
Original file line numberDiff line numberDiff line change
@@ -2227,6 +2227,7 @@ pub(crate) use assert_unsafe_precondition;
22272227

22282228
/// Checks whether `ptr` is properly aligned with respect to
22292229
/// `align_of::<T>()`.
2230+
#[cfg(not(miri))]
22302231
pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
22312232
!ptr.is_null() && ptr.is_aligned()
22322233
}
@@ -2243,6 +2244,7 @@ pub(crate) fn is_valid_allocation_size<T>(len: usize) -> bool {
22432244

22442245
/// Checks whether the regions of memory starting at `src` and `dst` of size
22452246
/// `count * size_of::<T>()` do *not* overlap.
2247+
#[cfg(not(miri))]
22462248
pub(crate) fn is_nonoverlapping<T>(src: *const T, dst: *const T, count: usize) -> bool {
22472249
let src_usize = src.addr();
22482250
let dst_usize = dst.addr();
@@ -2352,6 +2354,7 @@ pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: us
23522354
// SAFETY: the safety contract for `copy_nonoverlapping` must be
23532355
// upheld by the caller.
23542356
unsafe {
2357+
#[cfg(not(miri))] // This precondition is already always checked by Miri
23552358
assert_unsafe_precondition!(
23562359
"ptr::copy_nonoverlapping requires that both pointer arguments are aligned and non-null \
23572360
and the specified memory ranges do not overlap",
@@ -2441,6 +2444,7 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
24412444

24422445
// SAFETY: the safety contract for `copy` must be upheld by the caller.
24432446
unsafe {
2447+
#[cfg(not(miri))] // This precondition is already always checked by Miri
24442448
assert_unsafe_precondition!(
24452449
"ptr::copy requires that both pointer arguments are aligned aligned and non-null",
24462450
[T](src: *const T, dst: *mut T) =>
@@ -2513,6 +2517,7 @@ pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
25132517

25142518
// SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
25152519
unsafe {
2520+
#[cfg(not(miri))] // This precondition is already always checked by Miri
25162521
assert_unsafe_precondition!(
25172522
"ptr::write_bytes requires that the destination pointer is aligned and non-null",
25182523
[T](dst: *mut T) => is_aligned_and_not_null(dst)

library/core/src/num/nonzero.rs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ macro_rules! nonzero_integers {
5656
pub const unsafe fn new_unchecked(n: $Int) -> Self {
5757
// SAFETY: this is guaranteed to be safe by the caller.
5858
unsafe {
59+
#[cfg(not(miri))] // This precondition is already always checked by Miri
5960
core::intrinsics::assert_unsafe_precondition!(
6061
concat!(stringify!($Ty), "::new_unchecked requires a non-zero argument"),
6162
(n: $Int) => n != 0

library/core/src/ptr/const_ptr.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use super::*;
22
use crate::cmp::Ordering::{self, Equal, Greater, Less};
3-
use crate::intrinsics;
3+
use crate::intrinsics::{self, assert_unsafe_precondition};
44
use crate::mem;
55
use crate::slice::{self, SliceIndex};
66

@@ -761,6 +761,7 @@ impl<T: ?Sized> *const T {
761761
// SAFETY: The comparison has no side-effects, and the intrinsic
762762
// does this check internally in the CTFE implementation.
763763
unsafe {
764+
#[cfg(not(miri))]
764765
assert_unsafe_precondition!(
765766
"ptr::sub_ptr requires `this >= origin`",
766767
[T](this: *const T, origin: *const T) => this >= origin

library/core/src/ptr/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,9 @@
371371
use crate::cmp::Ordering;
372372
use crate::fmt;
373373
use crate::hash;
374-
use crate::intrinsics::{
375-
self, assert_unsafe_precondition, is_aligned_and_not_null, is_nonoverlapping,
376-
};
374+
use crate::intrinsics;
375+
#[cfg(not(miri))]
376+
use crate::intrinsics::{assert_unsafe_precondition, is_aligned_and_not_null, is_nonoverlapping};
377377

378378
use crate::mem::{self, MaybeUninit};
379379

@@ -895,6 +895,7 @@ pub const unsafe fn swap_nonoverlapping<T>(x: *mut T, y: *mut T, count: usize) {
895895
};
896896
}
897897

898+
#[cfg(not(miri))] // This precondition is already always checked by Miri
898899
// SAFETY: the caller must guarantee that `x` and `y` are
899900
// valid for writes and properly aligned.
900901
unsafe {
@@ -998,6 +999,7 @@ pub const unsafe fn replace<T>(dst: *mut T, mut src: T) -> T {
998999
// and cannot overlap `src` since `dst` must point to a distinct
9991000
// allocated object.
10001001
unsafe {
1002+
#[cfg(not(miri))] // This precondition is already always checked by Miri
10011003
assert_unsafe_precondition!(
10021004
"ptr::replace requires that the pointer argument is aligned and non-null",
10031005
[T](dst: *mut T) => is_aligned_and_not_null(dst)
@@ -1496,6 +1498,7 @@ pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
14961498
pub unsafe fn read_volatile<T>(src: *const T) -> T {
14971499
// SAFETY: the caller must uphold the safety contract for `volatile_load`.
14981500
unsafe {
1501+
#[cfg(not(miri))] // This precondition is already always checked by Miri
14991502
assert_unsafe_precondition!(
15001503
"ptr::read_volatile requires that the pointer argument is aligned and non-null",
15011504
[T](src: *const T) => is_aligned_and_not_null(src)
@@ -1570,6 +1573,7 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
15701573
pub unsafe fn write_volatile<T>(dst: *mut T, src: T) {
15711574
// SAFETY: the caller must uphold the safety contract for `volatile_store`.
15721575
unsafe {
1576+
#[cfg(not(miri))] // This precondition is already always checked by Miri
15731577
assert_unsafe_precondition!(
15741578
"ptr::write_volatile requires that the pointer argument is aligned and non-null",
15751579
[T](dst: *mut T) => is_aligned_and_not_null(dst)

library/core/src/slice/raw.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Free functions to create `&[T]` and `&mut [T]`.
22
33
use crate::array;
4+
#[cfg(not(miri))]
45
use crate::intrinsics::{
56
assert_unsafe_precondition, is_aligned_and_not_null, is_valid_allocation_size,
67
};
@@ -92,6 +93,7 @@ use crate::ptr;
9293
pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T] {
9394
// SAFETY: the caller must uphold the safety contract for `from_raw_parts`.
9495
unsafe {
96+
#[cfg(not(miri))] // This precondition is already always checked by Miri
9597
assert_unsafe_precondition!(
9698
"slice::from_raw_parts requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`",
9799
[T](data: *const T, len: usize) => is_aligned_and_not_null(data)
@@ -137,6 +139,7 @@ pub const unsafe fn from_raw_parts<'a, T>(data: *const T, len: usize) -> &'a [T]
137139
pub const unsafe fn from_raw_parts_mut<'a, T>(data: *mut T, len: usize) -> &'a mut [T] {
138140
// SAFETY: the caller must uphold the safety contract for `from_raw_parts_mut`.
139141
unsafe {
142+
#[cfg(not(miri))] // This precondition is already always checked by Miri
140143
assert_unsafe_precondition!(
141144
"slice::from_raw_parts_mut requires the pointer to be aligned and non-null, and the total size of the slice not to exceed `isize::MAX`",
142145
[T](data: *mut T, len: usize) => is_aligned_and_not_null(data)

0 commit comments

Comments
 (0)