Skip to content

Commit 82a2583

Browse files
committed
Make intrinsics::write_bytes const
1 parent 0460235 commit 82a2583

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

core/src/intrinsics.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2242,13 +2242,29 @@ pub const unsafe fn copy<T>(src: *const T, dst: *mut T, count: usize) {
22422242
/// assert_eq!(*v, 42);
22432243
/// ```
22442244
#[stable(feature = "rust1", since = "1.0.0")]
2245+
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
22452246
#[inline]
2246-
pub unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
2247+
pub const unsafe fn write_bytes<T>(dst: *mut T, val: u8, count: usize) {
22472248
extern "rust-intrinsic" {
2249+
#[rustc_const_unstable(feature = "const_ptr_write", issue = "86302")]
22482250
fn write_bytes<T>(dst: *mut T, val: u8, count: usize);
22492251
}
22502252

2251-
debug_assert!(is_aligned_and_not_null(dst), "attempt to write to unaligned or null pointer");
2253+
#[cfg(debug_assertions)]
2254+
fn runtime_check<T>(ptr: *mut T) {
2255+
debug_assert!(
2256+
is_aligned_and_not_null(ptr),
2257+
"attempt to write to unaligned or null pointer"
2258+
);
2259+
}
2260+
#[cfg(debug_assertions)]
2261+
const fn compiletime_check<T>(_ptr: *mut T) {}
2262+
#[cfg(debug_assertions)]
2263+
// SAFETY: runtime debug-assertions are a best-effort basis; it's fine to
2264+
// not do them during compile time
2265+
unsafe {
2266+
const_eval_select((dst,), compiletime_check, runtime_check);
2267+
}
22522268

22532269
// SAFETY: the safety contract for `write_bytes` must be upheld by the caller.
22542270
unsafe { write_bytes(dst, val, count) }

core/tests/intrinsics.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,33 @@ fn test_assume_can_be_in_const_contexts() {
3535
let rs = unsafe { foo(42, 97) };
3636
assert_eq!(rs, 0);
3737
}
38+
39+
#[test]
40+
#[cfg(not(bootstrap))]
41+
const fn test_write_bytes_in_const_contexts() {
42+
use core::intrinsics::write_bytes;
43+
44+
const TEST: [u32; 3] = {
45+
let mut arr = [1u32, 2, 3];
46+
unsafe {
47+
write_bytes(arr.as_mut_ptr(), 0, 2);
48+
}
49+
arr
50+
};
51+
52+
assert!(TEST[0] == 0);
53+
assert!(TEST[1] == 0);
54+
assert!(TEST[2] == 3);
55+
56+
const TEST2: [u32; 3] = {
57+
let mut arr = [1u32, 2, 3];
58+
unsafe {
59+
write_bytes(arr.as_mut_ptr(), 1, 2);
60+
}
61+
arr
62+
};
63+
64+
assert!(TEST2[0] == 16843009);
65+
assert!(TEST2[1] == 16843009);
66+
assert!(TEST2[2] == 3);
67+
}

0 commit comments

Comments
 (0)