Skip to content

Commit 8f742eb

Browse files
committed
Auto merge of #134757 - RalfJung:const_swap, r=scottmcm
stabilize const_swap libs-api FCP passed in rust-lang/rust#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
2 parents 59f2b38 + 53d6d53 commit 8f742eb

7 files changed

+60
-10
lines changed

tests/fail/intrinsics/typed-swap-invalid-array.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(core_intrinsics)]
22
#![feature(rustc_attrs)]
33

4-
use std::intrinsics::typed_swap;
4+
use std::intrinsics::typed_swap_nonoverlapping;
55
use std::ptr::addr_of_mut;
66

77
fn invalid_array() {
@@ -10,7 +10,7 @@ fn invalid_array() {
1010
unsafe {
1111
let a = addr_of_mut!(a).cast::<[bool; 100]>();
1212
let b = addr_of_mut!(b).cast::<[bool; 100]>();
13-
typed_swap(a, b); //~ERROR: constructing invalid value
13+
typed_swap_nonoverlapping(a, b); //~ERROR: constructing invalid value
1414
}
1515
}
1616

tests/fail/intrinsics/typed-swap-invalid-array.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: constructing invalid value at [0]: encountered 0x02, but expected a boolean
22
--> tests/fail/intrinsics/typed-swap-invalid-array.rs:LL:CC
33
|
4-
LL | typed_swap(a, b);
5-
| ^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered 0x02, but expected a boolean
4+
LL | typed_swap_nonoverlapping(a, b);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered 0x02, but expected a boolean
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

tests/fail/intrinsics/typed-swap-invalid-scalar.stderr renamed to tests/fail/intrinsics/typed-swap-invalid-scalar.left.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: constructing invalid value: encountered 0x02, but expected a boolean
22
--> tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC
33
|
4-
LL | typed_swap(a, b);
5-
| ^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x02, but expected a boolean
4+
LL | typed_swap_nonoverlapping(a, b);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x02, but expected a boolean
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: Undefined Behavior: constructing invalid value: encountered 0x03, but expected a boolean
2+
--> tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC
3+
|
4+
LL | typed_swap_nonoverlapping(a, b);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x03, but expected a boolean
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `invalid_scalar` at tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC
11+
note: inside `main`
12+
--> tests/fail/intrinsics/typed-swap-invalid-scalar.rs:LL:CC
13+
|
14+
LL | invalid_scalar();
15+
| ^^^^^^^^^^^^^^^^
16+
17+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
18+
19+
error: aborting due to 1 previous error
20+

tests/fail/intrinsics/typed-swap-invalid-scalar.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
//@revisions: left right
12
#![feature(core_intrinsics)]
23
#![feature(rustc_attrs)]
34

4-
use std::intrinsics::typed_swap;
5+
use std::intrinsics::typed_swap_nonoverlapping;
56
use std::ptr::addr_of_mut;
67

78
fn invalid_scalar() {
8-
let mut a = 1_u8;
9-
let mut b = 2_u8;
9+
// We run the test twice, with either the left or the right side being invalid.
10+
let mut a = if cfg!(left) { 2_u8 } else { 1_u8 };
11+
let mut b = if cfg!(right) { 3_u8 } else { 1_u8 };
1012
unsafe {
1113
let a = addr_of_mut!(a).cast::<bool>();
1214
let b = addr_of_mut!(b).cast::<bool>();
13-
typed_swap(a, b); //~ERROR: constructing invalid value
15+
typed_swap_nonoverlapping(a, b); //~ERROR: constructing invalid value
1416
}
1517
}
1618

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(core_intrinsics)]
2+
#![feature(rustc_attrs)]
3+
4+
use std::intrinsics::typed_swap_nonoverlapping;
5+
use std::ptr::addr_of_mut;
6+
7+
fn main() {
8+
let mut a = 0_u8;
9+
unsafe {
10+
let a = addr_of_mut!(a);
11+
typed_swap_nonoverlapping(a, a); //~ERROR: called on overlapping ranges
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: `copy_nonoverlapping` called on overlapping ranges
2+
--> tests/fail/intrinsics/typed-swap-overlap.rs:LL:CC
3+
|
4+
LL | typed_swap_nonoverlapping(a, a);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `copy_nonoverlapping` called on overlapping ranges
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
= note: BACKTRACE:
10+
= note: inside `main` at tests/fail/intrinsics/typed-swap-overlap.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+

0 commit comments

Comments
 (0)