Skip to content

Commit 3a894e3

Browse files
committed
Bring back tests removed in 'Revert PRs 81238 and 82967 (which made copy and copy_nonoverlapping' 5f6016f
1 parent 4b64baf commit 3a894e3

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

src/test/ui/consts/copy-intrinsic.rs

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// ignore-tidy-linelength
2+
#![feature(const_mut_refs, const_intrinsic_copy, const_ptr_offset)]
3+
use std::{ptr, mem};
4+
5+
const COPY_ZERO: () = unsafe {
6+
// Since we are not copying anything, this should be allowed.
7+
let src = ();
8+
let mut dst = ();
9+
ptr::copy_nonoverlapping(&src as *const _ as *const i32, &mut dst as *mut _ as *mut i32, 0);
10+
};
11+
12+
const COPY_OOB_1: () = unsafe {
13+
let mut x = 0i32;
14+
let dangle = (&mut x as *mut i32).wrapping_add(10);
15+
// Even if the first ptr is an int ptr and this is a ZST copy, we should detect dangling 2nd ptrs.
16+
ptr::copy_nonoverlapping(0x100 as *const i32, dangle, 0); //~ ERROR any use of this value will cause an error
17+
//~| memory access failed: pointer must be in-bounds
18+
//~| previously accepted
19+
};
20+
const COPY_OOB_2: () = unsafe {
21+
let x = 0i32;
22+
let dangle = (&x as *const i32).wrapping_add(10);
23+
// Even if the second ptr is an int ptr and this is a ZST copy, we should detect dangling 1st ptrs.
24+
ptr::copy_nonoverlapping(dangle, 0x100 as *mut i32, 0); //~ ERROR any use of this value will cause an error
25+
//~| memory access failed: pointer must be in-bounds
26+
//~| previously accepted
27+
};
28+
29+
const COPY_SIZE_OVERFLOW: () = unsafe {
30+
let x = 0;
31+
let mut y = 0;
32+
ptr::copy(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1)); //~ ERROR any use of this value will cause an error
33+
//~| overflow computing total size of `copy`
34+
//~| previously accepted
35+
};
36+
const COPY_NONOVERLAPPING_SIZE_OVERFLOW: () = unsafe {
37+
let x = 0;
38+
let mut y = 0;
39+
ptr::copy_nonoverlapping(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1)); //~ ERROR any use of this value will cause an error
40+
//~| overflow computing total size of `copy_nonoverlapping`
41+
//~| previously accepted
42+
};
43+
44+
fn main() {
45+
}
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
error: any use of this value will cause an error
2+
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
3+
|
4+
LL | unsafe { copy_nonoverlapping(src, dst, count) }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| memory access failed: pointer must be in-bounds at offset 40, but is outside bounds of alloc4 which has size 4
8+
| inside `copy_nonoverlapping::<i32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
9+
| inside `COPY_OOB_1` at $DIR/copy-intrinsic.rs:16:5
10+
|
11+
::: $DIR/copy-intrinsic.rs:12:1
12+
|
13+
LL | / const COPY_OOB_1: () = unsafe {
14+
LL | | let mut x = 0i32;
15+
LL | | let dangle = (&mut x as *mut i32).wrapping_add(10);
16+
LL | | // Even if the first ptr is an int ptr and this is a ZST copy, we should detect dangling 2nd ptrs.
17+
... |
18+
LL | |
19+
LL | | };
20+
| |__-
21+
|
22+
= note: `#[deny(const_err)]` on by default
23+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
24+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
25+
26+
error: any use of this value will cause an error
27+
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
28+
|
29+
LL | unsafe { copy_nonoverlapping(src, dst, count) }
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
| |
32+
| memory access failed: pointer must be in-bounds at offset 40, but is outside bounds of alloc6 which has size 4
33+
| inside `copy_nonoverlapping::<i32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
34+
| inside `COPY_OOB_2` at $DIR/copy-intrinsic.rs:24:5
35+
|
36+
::: $DIR/copy-intrinsic.rs:20:1
37+
|
38+
LL | / const COPY_OOB_2: () = unsafe {
39+
LL | | let x = 0i32;
40+
LL | | let dangle = (&x as *const i32).wrapping_add(10);
41+
LL | | // Even if the second ptr is an int ptr and this is a ZST copy, we should detect dangling 1st ptrs.
42+
... |
43+
LL | |
44+
LL | | };
45+
| |__-
46+
|
47+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
48+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
49+
50+
error: any use of this value will cause an error
51+
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
52+
|
53+
LL | unsafe { copy(src, dst, count) }
54+
| ^^^^^^^^^^^^^^^^^^^^^
55+
| |
56+
| overflow computing total size of `copy`
57+
| inside `std::intrinsics::copy::<i32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
58+
| inside `COPY_SIZE_OVERFLOW` at $DIR/copy-intrinsic.rs:32:5
59+
|
60+
::: $DIR/copy-intrinsic.rs:29:1
61+
|
62+
LL | / const COPY_SIZE_OVERFLOW: () = unsafe {
63+
LL | | let x = 0;
64+
LL | | let mut y = 0;
65+
LL | | ptr::copy(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1));
66+
LL | |
67+
LL | |
68+
LL | | };
69+
| |__-
70+
|
71+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
72+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
73+
74+
error: any use of this value will cause an error
75+
--> $SRC_DIR/core/src/intrinsics.rs:LL:COL
76+
|
77+
LL | unsafe { copy_nonoverlapping(src, dst, count) }
78+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
79+
| |
80+
| overflow computing total size of `copy_nonoverlapping`
81+
| inside `copy_nonoverlapping::<i32>` at $SRC_DIR/core/src/intrinsics.rs:LL:COL
82+
| inside `COPY_NONOVERLAPPING_SIZE_OVERFLOW` at $DIR/copy-intrinsic.rs:39:5
83+
|
84+
::: $DIR/copy-intrinsic.rs:36:1
85+
|
86+
LL | / const COPY_NONOVERLAPPING_SIZE_OVERFLOW: () = unsafe {
87+
LL | | let x = 0;
88+
LL | | let mut y = 0;
89+
LL | | ptr::copy_nonoverlapping(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1));
90+
LL | |
91+
LL | |
92+
LL | | };
93+
| |__-
94+
|
95+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
96+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
97+
98+
error: aborting due to 4 previous errors
99+

0 commit comments

Comments
 (0)