Skip to content

Commit f5dcce4

Browse files
committed
Auto merge of #122582 - scottmcm:swap-intrinsic-v2, r=oli-obk
Let codegen decide when to `mem::swap` with immediates Making `libcore` decide this is silly; the backend has so much better information about when it's a good idea. Thus this PR introduces a new `typed_swap` intrinsic with a fallback body, and replaces that fallback implementation when swapping immediates or scalar pairs. r? oli-obk Replaces #111744, and means we'll never need more libs PRs like #111803 or #107140
2 parents caa3e75 + 42c2ef0 commit f5dcce4

4 files changed

+78
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(core_intrinsics)]
2+
#![feature(rustc_attrs)]
3+
4+
use std::intrinsics::typed_swap;
5+
use std::ptr::addr_of_mut;
6+
7+
fn invalid_array() {
8+
let mut a = [1_u8; 100];
9+
let mut b = [2_u8; 100];
10+
unsafe {
11+
let a = addr_of_mut!(a).cast::<[bool; 100]>();
12+
let b = addr_of_mut!(b).cast::<[bool; 100]>();
13+
typed_swap(a, b); //~ERROR: constructing invalid value
14+
}
15+
}
16+
17+
fn main() {
18+
invalid_array();
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: Undefined Behavior: constructing invalid value at [0]: encountered 0x02, but expected a boolean
2+
--> $DIR/typed-swap-invalid-array.rs:LL:CC
3+
|
4+
LL | typed_swap(a, b);
5+
| ^^^^^^^^^^^^^^^^ constructing invalid value at [0]: encountered 0x02, 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_array` at $DIR/typed-swap-invalid-array.rs:LL:CC
11+
note: inside `main`
12+
--> $DIR/typed-swap-invalid-array.rs:LL:CC
13+
|
14+
LL | invalid_array();
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(core_intrinsics)]
2+
#![feature(rustc_attrs)]
3+
4+
use std::intrinsics::typed_swap;
5+
use std::ptr::addr_of_mut;
6+
7+
fn invalid_scalar() {
8+
let mut a = 1_u8;
9+
let mut b = 2_u8;
10+
unsafe {
11+
let a = addr_of_mut!(a).cast::<bool>();
12+
let b = addr_of_mut!(b).cast::<bool>();
13+
typed_swap(a, b); //~ERROR: constructing invalid value
14+
}
15+
}
16+
17+
fn main() {
18+
invalid_scalar();
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: Undefined Behavior: constructing invalid value: encountered 0x02, but expected a boolean
2+
--> $DIR/typed-swap-invalid-scalar.rs:LL:CC
3+
|
4+
LL | typed_swap(a, b);
5+
| ^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x02, 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 $DIR/typed-swap-invalid-scalar.rs:LL:CC
11+
note: inside `main`
12+
--> $DIR/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+

0 commit comments

Comments
 (0)