Skip to content

Commit 7291b1e

Browse files
committed
rename typed_swap → typed_swap_nonoverlapping
1 parent 00dfa3b commit 7291b1e

File tree

14 files changed

+43
-26
lines changed

14 files changed

+43
-26
lines changed

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
7575
// If we're swapping something that's *not* an `OperandValue::Ref`,
7676
// then we can do it directly and avoid the alloca.
7777
// Otherwise, we'll let the fallback MIR body take care of it.
78-
if let sym::typed_swap = name {
78+
if let sym::typed_swap_nonoverlapping = name {
7979
let pointee_ty = fn_args.type_at(0);
8080
let pointee_layout = bx.layout_of(pointee_ty);
8181
if !bx.is_backend_ref(pointee_layout)

compiler/rustc_codegen_ssa/src/traits/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ pub trait BuilderMethods<'a, 'tcx>:
382382
/// Avoids `alloca`s for Immediates and ScalarPairs.
383383
///
384384
/// FIXME: Maybe do something smarter for Ref types too?
385-
/// For now, the `typed_swap` intrinsic just doesn't call this for those
385+
/// For now, the `typed_swap_nonoverlapping` intrinsic just doesn't call this for those
386386
/// cases (in non-debug), preferring the fallback body instead.
387387
fn typed_place_swap(
388388
&mut self,

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
424424
let result = self.raw_eq_intrinsic(&args[0], &args[1])?;
425425
self.write_scalar(result, dest)?;
426426
}
427-
sym::typed_swap => {
427+
sym::typed_swap_nonoverlapping => {
428428
self.typed_swap_intrinsic(&args[0], &args[1])?;
429429
}
430430

compiler/rustc_hir_analysis/src/check/intrinsic.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,9 @@ pub fn check_intrinsic_type(
496496
(1, 0, vec![Ty::new_mut_ptr(tcx, param(0)), param(0)], tcx.types.unit)
497497
}
498498

499-
sym::typed_swap => (1, 0, vec![Ty::new_mut_ptr(tcx, param(0)); 2], tcx.types.unit),
499+
sym::typed_swap_nonoverlapping => {
500+
(1, 0, vec![Ty::new_mut_ptr(tcx, param(0)); 2], tcx.types.unit)
501+
}
500502

501503
sym::discriminant_value => {
502504
let assoc_items = tcx.associated_item_def_ids(

compiler/rustc_span/src/symbol.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,7 @@ symbols! {
20582058
type_macros,
20592059
type_name,
20602060
type_privacy_lints,
2061-
typed_swap,
2061+
typed_swap_nonoverlapping,
20622062
u128,
20632063
u128_legacy_const_max,
20642064
u128_legacy_const_min,

library/core/src/intrinsics/mod.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -3940,6 +3940,21 @@ pub const fn is_val_statically_known<T: Copy>(_arg: T) -> bool {
39403940
false
39413941
}
39423942

3943+
#[rustc_nounwind]
3944+
#[inline]
3945+
#[rustc_intrinsic]
3946+
#[rustc_intrinsic_const_stable_indirect]
3947+
#[rustc_allow_const_fn_unstable(const_swap_nonoverlapping)] // this is anyway not called since CTFE implements the intrinsic
3948+
#[cfg(bootstrap)]
3949+
pub const unsafe fn typed_swap<T>(x: *mut T, y: *mut T) {
3950+
// SAFETY: The caller provided single non-overlapping items behind
3951+
// pointers, so swapping them with `count: 1` is fine.
3952+
unsafe { ptr::swap_nonoverlapping(x, y, 1) };
3953+
}
3954+
3955+
#[cfg(bootstrap)]
3956+
pub use typed_swap as typed_swap_nonoverlapping;
3957+
39433958
/// Non-overlapping *typed* swap of a single value.
39443959
///
39453960
/// The codegen backends will replace this with a better implementation when
@@ -3953,10 +3968,10 @@ pub const fn is_val_statically_known<T: Copy>(_arg: T) -> bool {
39533968
#[rustc_nounwind]
39543969
#[inline]
39553970
#[rustc_intrinsic]
3956-
// Const-unstable because `swap_nonoverlapping` is const-unstable.
39573971
#[rustc_intrinsic_const_stable_indirect]
39583972
#[rustc_allow_const_fn_unstable(const_swap_nonoverlapping)] // this is anyway not called since CTFE implements the intrinsic
3959-
pub const unsafe fn typed_swap<T>(x: *mut T, y: *mut T) {
3973+
#[cfg(not(bootstrap))]
3974+
pub const unsafe fn typed_swap_nonoverlapping<T>(x: *mut T, y: *mut T) {
39603975
// SAFETY: The caller provided single non-overlapping items behind
39613976
// pointers, so swapping them with `count: 1` is fine.
39623977
unsafe { ptr::swap_nonoverlapping(x, y, 1) };

library/core/src/mem/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ pub unsafe fn uninitialized<T>() -> T {
730730
pub const fn swap<T>(x: &mut T, y: &mut T) {
731731
// SAFETY: `&mut` guarantees these are typed readable and writable
732732
// as well as non-overlapping.
733-
unsafe { intrinsics::typed_swap(x, y) }
733+
unsafe { intrinsics::typed_swap_nonoverlapping(x, y) }
734734
}
735735

736736
/// Replaces `dest` with the default value of `T`, returning the previous `dest` value.

src/tools/miri/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

src/tools/miri/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

src/tools/miri/tests/fail/intrinsics/typed-swap-invalid-scalar.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_scalar() {
@@ -10,7 +10,7 @@ fn invalid_scalar() {
1010
unsafe {
1111
let a = addr_of_mut!(a).cast::<bool>();
1212
let b = addr_of_mut!(b).cast::<bool>();
13-
typed_swap(a, b); //~ERROR: constructing invalid value
13+
typed_swap_nonoverlapping(a, b); //~ERROR: constructing invalid value
1414
}
1515
}
1616

src/tools/miri/tests/fail/intrinsics/typed-swap-invalid-scalar.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
@@ -1,13 +1,13 @@
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 main() {
88
let mut a = [0_u8; 100];
99
unsafe {
1010
let a = addr_of_mut!(a);
11-
typed_swap(a, a); //~ERROR: called on overlapping ranges
11+
typed_swap_nonoverlapping(a, a); //~ERROR: called on overlapping ranges
1212
}
1313
}

src/tools/miri/tests/fail/intrinsics/typed-swap-overlap.stderr

+2-2
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/typed-swap-overlap.rs:LL:CC
33
|
4-
LL | typed_swap(a, a);
5-
| ^^^^^^^^^^^^^^^^ `copy_nonoverlapping` called on overlapping ranges
4+
LL | typed_swap_nonoverlapping(a, a);
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

tests/codegen/intrinsics/typed_swap.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
#![crate_type = "lib"]
99
#![feature(core_intrinsics)]
1010

11-
use std::intrinsics::typed_swap;
11+
use std::intrinsics::typed_swap_nonoverlapping;
1212

1313
// CHECK-LABEL: @swap_unit(
1414
#[no_mangle]
1515
pub unsafe fn swap_unit(x: &mut (), y: &mut ()) {
1616
// CHECK: start
1717
// CHECK-NEXT: ret void
18-
typed_swap(x, y)
18+
typed_swap_nonoverlapping(x, y)
1919
}
2020

2121
// CHECK-LABEL: @swap_i32(
@@ -32,7 +32,7 @@ pub unsafe fn swap_i32(x: &mut i32, y: &mut i32) {
3232
// OPT3: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %x, ptr align 4 %y, i64 4, i1 false)
3333
// CHECK: store i32 %[[TEMP]], ptr %y, align 4
3434
// CHECK: ret void
35-
typed_swap(x, y)
35+
typed_swap_nonoverlapping(x, y)
3636
}
3737

3838
// CHECK-LABEL: @swap_pair(
@@ -47,7 +47,7 @@ pub unsafe fn swap_pair(x: &mut (i32, u32), y: &mut (i32, u32)) {
4747
// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %x, ptr align 4 %y, i64 8, i1 false)
4848
// CHECK: store i32
4949
// CHECK: store i32
50-
typed_swap(x, y)
50+
typed_swap_nonoverlapping(x, y)
5151
}
5252

5353
// CHECK-LABEL: @swap_str(
@@ -63,7 +63,7 @@ pub unsafe fn swap_str<'a>(x: &mut &'a str, y: &mut &'a str) {
6363
// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %x, ptr align 8 %y, i64 16, i1 false)
6464
// CHECK: store ptr
6565
// CHECK: store i64
66-
typed_swap(x, y)
66+
typed_swap_nonoverlapping(x, y)
6767
}
6868

6969
// OPT0-LABEL: @swap_string(
@@ -73,5 +73,5 @@ pub unsafe fn swap_string(x: &mut String, y: &mut String) {
7373
// OPT0: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %[[TEMP]], ptr align 8 %x, i64 24, i1 false)
7474
// OPT0: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %x, ptr align 8 %y, i64 24, i1 false)
7575
// OPT0: call void @llvm.memcpy.p0.p0.i64(ptr align 8 %y, ptr align 8 %[[TEMP]], i64 24, i1 false)
76-
typed_swap(x, y)
76+
typed_swap_nonoverlapping(x, y)
7777
}

0 commit comments

Comments
 (0)