Skip to content

Commit 55c88f5

Browse files
committed
fix error message for copy(_nonoverlapping) overflow
1 parent a4d9624 commit 55c88f5

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

compiler/rustc_mir/src/interpret/step.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,10 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
160160
let layout = self.layout_of(src.layout.ty.builtin_deref(true).unwrap().ty)?;
161161
let (size, align) = (layout.size, layout.align.abi);
162162
let size = size.checked_mul(count, self).ok_or_else(|| {
163-
err_ub_format!("overflow computing total size of `copy_nonoverlapping`")
163+
err_ub_format!(
164+
"overflow computing total size of `{}`",
165+
if nonoverlapping { "copy_nonoverlapping" } else { "copy" }
166+
)
164167
})?;
165168

166169
// Make sure we check both pointers for an access of the total size and aligment,

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ignore-tidy-linelength
22
#![feature(const_mut_refs, const_intrinsic_copy, const_ptr_offset)]
3-
use std::ptr;
3+
use std::{ptr, mem};
44

55
const COPY_ZERO: () = unsafe {
66
// Since we are not copying anything, this should be allowed.
@@ -26,6 +26,20 @@ const COPY_OOB_2: () = unsafe {
2626
//~| previously accepted
2727
};
2828

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+
};
2943

3044
fn main() {
3145
}

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

+33-1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,37 @@ LL | | };
3333
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
3434
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
3535

36-
error: aborting due to 2 previous errors
36+
error: any use of this value will cause an error
37+
--> $DIR/copy-intrinsic.rs:32:5
38+
|
39+
LL | / const COPY_SIZE_OVERFLOW: () = unsafe {
40+
LL | | let x = 0;
41+
LL | | let mut y = 0;
42+
LL | | ptr::copy(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1));
43+
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overflow computing total size of `copy`
44+
LL | |
45+
LL | |
46+
LL | | };
47+
| |__-
48+
|
49+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
50+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
51+
52+
error: any use of this value will cause an error
53+
--> $DIR/copy-intrinsic.rs:39:5
54+
|
55+
LL | / const COPY_NONOVERLAPPING_SIZE_OVERFLOW: () = unsafe {
56+
LL | | let x = 0;
57+
LL | | let mut y = 0;
58+
LL | | ptr::copy_nonoverlapping(&x, &mut y, 1usize << (mem::size_of::<usize>() * 8 - 1));
59+
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ overflow computing total size of `copy_nonoverlapping`
60+
LL | |
61+
LL | |
62+
LL | | };
63+
| |__-
64+
|
65+
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
66+
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
67+
68+
error: aborting due to 4 previous errors
3769

0 commit comments

Comments
 (0)