Skip to content

Commit df2873f

Browse files
committed
Auto merge of #139916 - RalfJung:intrinsic-wrappers, r=Mark-Simulacrum
make std::intrinsics functions actually be intrinsics Most of the functions in `std::intrinsics` are actually intrinsics, but some are not: for historical reasons, `std::intrinsics::{copy,copy_nonoverlapping,write_bytes}` are accessible on stable, and the versions in `std::ptr` are just re-exports. These functions are not intrinsics, but wrappers around the intrinsic, because they add extra debug assertions. This PR makes the functions in `std::intrinsics` actually be intrinsics. - The advantage is that we can now use it in tests that need to directly call the intrinsic, thus removing a footgun for compiler development. We also remove the extended user-facing doc comments of these functions out of a file that should be largely internal documentation. - The downside is that if users are using those functions directly, they will not get the debug assertions any more. Note however that those users are already ignoring a deprecation warning, so I think this is fine. Furthermore, if someone imports the `intrinsic` name of this function and turns that into a function pointer, that will no longer work, since only the wrapper functions can be turned into a function pointer. I would be rather surprised if anyone did this, though... and again, they must have already ignored a deprecation warning. Still, seems worth a crater run, if there's general agreement that we want to go ahead with this change. (`intrinsics::drop_in_place` also remains not-an-intrinsic, which bugs me, but oh well, not much we can do about it; we can't remove it from the module as the path is accidentally-stable.) Cc `@rust-lang/libs-api` `@saethlin`
2 parents d820eab + 667caa8 commit df2873f

File tree

4 files changed

+10
-16
lines changed

4 files changed

+10
-16
lines changed
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
#![feature(intrinsics)]
2-
3-
// Directly call intrinsic to avoid debug assertions in libstd
4-
#[rustc_intrinsic]
5-
unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
1+
#![feature(core_intrinsics)]
62

73
fn main() {
84
let mut data = [0u8; 16];
95
unsafe {
106
let a = data.as_mut_ptr();
117
let b = a.wrapping_offset(1) as *mut _;
12-
copy_nonoverlapping(a, b, 2); //~ ERROR: `copy_nonoverlapping` called on overlapping ranges
8+
// Directly call intrinsic to avoid debug assertions in the `std::ptr` version.
9+
std::intrinsics::copy_nonoverlapping(a, b, 2); //~ ERROR: `copy_nonoverlapping` called on overlapping ranges
1310
}
1411
}

tests/fail/intrinsics/copy_overlapping.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: `copy_nonoverlapping` called on overlapping ranges
22
--> tests/fail/intrinsics/copy_overlapping.rs:LL:CC
33
|
4-
LL | copy_nonoverlapping(a, b, 2);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `copy_nonoverlapping` called on overlapping ranges
4+
LL | std::intrinsics::copy_nonoverlapping(a, b, 2);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `copy_nonoverlapping` called on overlapping ranges
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
#![feature(intrinsics)]
2-
3-
// Directly call intrinsic to avoid debug assertions in libstd
4-
#[rustc_intrinsic]
5-
unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
1+
#![feature(core_intrinsics)]
62

73
fn main() {
84
let mut data = [0u16; 8];
95
let ptr = (&mut data[0] as *mut u16 as *mut u8).wrapping_add(1) as *mut u16;
106
// Even copying 0 elements to something unaligned should error
117
unsafe {
12-
copy_nonoverlapping(&data[5], ptr, 0); //~ ERROR: accessing memory with alignment 1, but alignment 2 is required
8+
// Directly call intrinsic to avoid debug assertions in the `std::ptr` version.
9+
std::intrinsics::copy_nonoverlapping(&data[5], ptr, 0); //~ ERROR: accessing memory with alignment 1, but alignment 2 is required
1310
}
1411
}

tests/fail/intrinsics/copy_unaligned.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
22
--> tests/fail/intrinsics/copy_unaligned.rs:LL:CC
33
|
4-
LL | copy_nonoverlapping(&data[5], ptr, 0);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
4+
LL | std::intrinsics::copy_nonoverlapping(&data[5], ptr, 0);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

0 commit comments

Comments
 (0)