Auto merge of #134757 - RalfJung:const_swap, r=scottmcm
stabilize const_swap libs-api FCP passed in https://github.com/rust-lang/rust/issues/83163. However, I only just realized that this actually involves an intrinsic. The intrinsic could be implemented entirely with existing stable const functionality, but we choose to make it a primitive to be able to detect more UB. So nominating for `@rust-lang/lang` to make sure they are aware; I leave it up to them whether they want to FCP this. While at it I also renamed the intrinsic to make the "nonoverlapping" constraint more clear. Fixes #83163
This commit is contained in:
commit
4e5fec2f1e
28 changed files with 139 additions and 95 deletions
|
|
@ -1,7 +1,7 @@
|
|||
#![feature(core_intrinsics)]
|
||||
#![feature(rustc_attrs)]
|
||||
|
||||
use std::intrinsics::typed_swap;
|
||||
use std::intrinsics::typed_swap_nonoverlapping;
|
||||
use std::ptr::addr_of_mut;
|
||||
|
||||
fn invalid_array() {
|
||||
|
|
@ -10,7 +10,7 @@ fn invalid_array() {
|
|||
unsafe {
|
||||
let a = addr_of_mut!(a).cast::<[bool; 100]>();
|
||||
let b = addr_of_mut!(b).cast::<[bool; 100]>();
|
||||
typed_swap(a, b); //~ERROR: constructing invalid value
|
||||
typed_swap_nonoverlapping(a, b); //~ERROR: constructing invalid value
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
error: Undefined Behavior: constructing invalid value at [0]: encountered 0x02, but expected a boolean
|
||||
--> tests/fail/intrinsics/typed-swap-invalid-array.rs:LL:CC
|
||||
|
|
||||
LL | typed_swap(a, b);
|
||||
| ^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered 0x02, but expected a boolean
|
||||
LL | typed_swap_nonoverlapping(a, b);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered 0x02, but expected a boolean
|
||||
|
|
||||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
|
||||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
error: Undefined Behavior: constructing invalid value: encountered 0x02, but expected a boolean
|
||||
--> tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC
|
||||
|
|
||||
LL | typed_swap(a, b);
|
||||
| ^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x02, but expected a boolean
|
||||
LL | typed_swap_nonoverlapping(a, b);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x02, but expected a boolean
|
||||
|
|
||||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
|
||||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
error: Undefined Behavior: constructing invalid value: encountered 0x03, but expected a boolean
|
||||
--> tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC
|
||||
|
|
||||
LL | typed_swap_nonoverlapping(a, b);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x03, but expected a boolean
|
||||
|
|
||||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
|
||||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
|
||||
= note: BACKTRACE:
|
||||
= note: inside `invalid_scalar` at tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC
|
||||
note: inside `main`
|
||||
--> tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC
|
||||
|
|
||||
LL | invalid_scalar();
|
||||
| ^^^^^^^^^^^^^^^^
|
||||
|
||||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
|
|
@ -1,16 +1,18 @@
|
|||
//@revisions: left right
|
||||
#![feature(core_intrinsics)]
|
||||
#![feature(rustc_attrs)]
|
||||
|
||||
use std::intrinsics::typed_swap;
|
||||
use std::intrinsics::typed_swap_nonoverlapping;
|
||||
use std::ptr::addr_of_mut;
|
||||
|
||||
fn invalid_scalar() {
|
||||
let mut a = 1_u8;
|
||||
let mut b = 2_u8;
|
||||
// We run the test twice, with either the left or the right side being invalid.
|
||||
let mut a = if cfg!(left) { 2_u8 } else { 1_u8 };
|
||||
let mut b = if cfg!(right) { 3_u8 } else { 1_u8 };
|
||||
unsafe {
|
||||
let a = addr_of_mut!(a).cast::<bool>();
|
||||
let b = addr_of_mut!(b).cast::<bool>();
|
||||
typed_swap(a, b); //~ERROR: constructing invalid value
|
||||
typed_swap_nonoverlapping(a, b); //~ERROR: constructing invalid value
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
13
src/tools/miri/tests/fail/intrinsics/typed-swap-overlap.rs
Normal file
13
src/tools/miri/tests/fail/intrinsics/typed-swap-overlap.rs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#![feature(core_intrinsics)]
|
||||
#![feature(rustc_attrs)]
|
||||
|
||||
use std::intrinsics::typed_swap_nonoverlapping;
|
||||
use std::ptr::addr_of_mut;
|
||||
|
||||
fn main() {
|
||||
let mut a = 0_u8;
|
||||
unsafe {
|
||||
let a = addr_of_mut!(a);
|
||||
typed_swap_nonoverlapping(a, a); //~ERROR: called on overlapping ranges
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
error: Undefined Behavior: `copy_nonoverlapping` called on overlapping ranges
|
||||
--> tests/fail/intrinsics/typed-swap-overlap.rs:LL:CC
|
||||
|
|
||||
LL | typed_swap_nonoverlapping(a, a);
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `copy_nonoverlapping` called on overlapping ranges
|
||||
|
|
||||
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
|
||||
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
|
||||
= note: BACKTRACE:
|
||||
= note: inside `main` at tests/fail/intrinsics/typed-swap-overlap.rs:LL:CC
|
||||
|
||||
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
|
||||
|
||||
error: aborting due to 1 previous error
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue