@@ -374,6 +374,7 @@ use crate::hash;
374
374
use crate :: intrinsics:: {
375
375
self , assert_unsafe_precondition, is_aligned_and_not_null, is_nonoverlapping,
376
376
} ;
377
+ use crate :: panic:: debug_assert_nounwind;
377
378
378
379
use crate :: mem:: { self , MaybeUninit } ;
379
380
@@ -1161,13 +1162,13 @@ pub const unsafe fn read<T>(src: *const T) -> T {
1161
1162
// Future enhancements to MIR optimizations might well allow this to return
1162
1163
// to the previous implementation, rather than using an intrinsic.
1163
1164
1165
+ debug_assert_nounwind ! (
1166
+ is_aligned_and_not_null( src) ,
1167
+ "ptr::read requires that the pointer argument is aligned and non-null" ,
1168
+ ) ;
1169
+
1164
1170
// SAFETY: the caller must guarantee that `src` is valid for reads.
1165
1171
unsafe {
1166
- assert_unsafe_precondition ! (
1167
- "ptr::read requires that the pointer argument is aligned and non-null" ,
1168
- [ T ] ( src: * const T ) => is_aligned_and_not_null( src)
1169
- ) ;
1170
-
1171
1172
#[ cfg( bootstrap) ]
1172
1173
{
1173
1174
// We are calling the intrinsics directly to avoid function calls in the
@@ -1375,14 +1376,15 @@ pub const unsafe fn write<T>(dst: *mut T, src: T) {
1375
1376
fn copy_nonoverlapping < T > ( src : * const T , dst : * mut T , count : usize ) ;
1376
1377
}
1377
1378
1379
+ debug_assert_nounwind ! (
1380
+ is_aligned_and_not_null( dst) ,
1381
+ "ptr::write requires that the pointer argument is aligned and non-null" ,
1382
+ ) ;
1383
+
1378
1384
// SAFETY: the caller must guarantee that `dst` is valid for writes.
1379
1385
// `dst` cannot overlap `src` because the caller has mutable access
1380
1386
// to `dst` while `src` is owned by this function.
1381
1387
unsafe {
1382
- assert_unsafe_precondition ! (
1383
- "ptr::write requires that the pointer argument is aligned and non-null" ,
1384
- [ T ] ( dst: * mut T ) => is_aligned_and_not_null( dst)
1385
- ) ;
1386
1388
copy_nonoverlapping ( & src as * const T , dst, 1 ) ;
1387
1389
intrinsics:: forget ( src) ;
1388
1390
}
@@ -1544,14 +1546,13 @@ pub const unsafe fn write_unaligned<T>(dst: *mut T, src: T) {
1544
1546
#[ stable( feature = "volatile" , since = "1.9.0" ) ]
1545
1547
#[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
1546
1548
pub unsafe fn read_volatile < T > ( src : * const T ) -> T {
1549
+ debug_assert_nounwind ! (
1550
+ is_aligned_and_not_null( src) ,
1551
+ "ptr::read_volatile requires that the pointer argument is aligned and non-null" ,
1552
+ ) ;
1553
+
1547
1554
// SAFETY: the caller must uphold the safety contract for `volatile_load`.
1548
- unsafe {
1549
- assert_unsafe_precondition ! (
1550
- "ptr::read_volatile requires that the pointer argument is aligned and non-null" ,
1551
- [ T ] ( src: * const T ) => is_aligned_and_not_null( src)
1552
- ) ;
1553
- intrinsics:: volatile_load ( src)
1554
- }
1555
+ unsafe { intrinsics:: volatile_load ( src) }
1555
1556
}
1556
1557
1557
1558
/// Performs a volatile write of a memory location with the given value without
@@ -1618,12 +1619,13 @@ pub unsafe fn read_volatile<T>(src: *const T) -> T {
1618
1619
#[ stable( feature = "volatile" , since = "1.9.0" ) ]
1619
1620
#[ cfg_attr( miri, track_caller) ] // even without panics, this helps for Miri backtraces
1620
1621
pub unsafe fn write_volatile < T > ( dst : * mut T , src : T ) {
1622
+ debug_assert_nounwind ! (
1623
+ is_aligned_and_not_null( dst) ,
1624
+ "ptr::write_volatile requires that the pointer argument is aligned and non-null" ,
1625
+ ) ;
1626
+
1621
1627
// SAFETY: the caller must uphold the safety contract for `volatile_store`.
1622
1628
unsafe {
1623
- assert_unsafe_precondition ! (
1624
- "ptr::write_volatile requires that the pointer argument is aligned and non-null" ,
1625
- [ T ] ( dst: * mut T ) => is_aligned_and_not_null( dst)
1626
- ) ;
1627
1629
intrinsics:: volatile_store ( dst, src) ;
1628
1630
}
1629
1631
}
0 commit comments