Skip to content

Commit 7475661

Browse files
committed
improve test by using intrinsic directly
1 parent 3061777 commit 7475661

File tree

2 files changed

+26
-54
lines changed

2 files changed

+26
-54
lines changed

src/test/ui/consts/offset_from_ub.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#![feature(const_raw_ptr_deref)]
22
#![feature(const_ptr_offset_from)]
3+
#![feature(core_intrinsics)]
4+
5+
use std::intrinsics::ptr_offset_from;
36

47
#[repr(C)]
58
struct Struct {
@@ -12,32 +15,34 @@ pub const DIFFERENT_ALLOC: usize = {
1215
let base_ptr: *const Struct = &uninit as *const _ as *const Struct;
1316
let uninit2 = std::mem::MaybeUninit::<Struct>::uninit();
1417
let field_ptr: *const Struct = &uninit2 as *const _ as *const Struct;
15-
let offset = unsafe { field_ptr.offset_from(base_ptr) }; //~NOTE inside `DIFFERENT_ALLOC` at
18+
let offset = unsafe { ptr_offset_from(field_ptr, base_ptr) }; //~ERROR evaluation of constant value failed
19+
//~| cannot compute offset of pointers into different allocations.
1620
offset as usize
1721
};
1822

1923
pub const NOT_PTR: usize = {
20-
//~^ NOTE
2124
unsafe { (42 as *const u8).offset_from(&5u8) as usize }
2225
};
2326

2427
pub const NOT_MULTIPLE_OF_SIZE: isize = {
2528
let data = [5u8, 6, 7];
2629
let base_ptr = data.as_ptr();
2730
let field_ptr = &data[1] as *const u8 as *const u16;
28-
unsafe { field_ptr.offset_from(base_ptr as *const u16) } //~NOTE inside `NOT_MULTIPLE_OF_SIZE` at
31+
unsafe { ptr_offset_from(field_ptr, base_ptr as *const u16) } //~ERROR evaluation of constant value failed
32+
//~| 1_isize cannot be divided by 2_isize without remainder
2933
};
3034

3135
pub const OFFSET_FROM_NULL: isize = {
3236
let ptr = 0 as *const u8;
33-
unsafe { ptr.offset_from(ptr) } //~NOTE inside `OFFSET_FROM_NULL` at
37+
unsafe { ptr_offset_from(ptr, ptr) } //~ERROR evaluation of constant value failed
38+
//~| null pointer is not a valid pointer
3439
};
3540

3641
pub const DIFFERENT_INT: isize = { // offset_from with two different integers: like DIFFERENT_ALLOC
37-
//~^ NOTE
3842
let ptr1 = 8 as *const u8;
3943
let ptr2 = 16 as *const u8;
40-
unsafe { ptr2.offset_from(ptr1) }
44+
unsafe { ptr_offset_from(ptr2, ptr1) } //~ERROR any use of this value will cause an error
45+
//~| WARN previously accepted
4146
};
4247

4348
fn main() {}

src/test/ui/consts/offset_from_ub.stderr

+15-48
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
3-
|
4-
LL | unsafe { intrinsics::ptr_offset_from(self, origin) }
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| ptr_offset_from cannot compute offset of pointers into different allocations.
8-
| inside `ptr::const_ptr::<impl *const Struct>::offset_from` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
9-
|
10-
::: $DIR/offset_from_ub.rs:15:27
2+
--> $DIR/offset_from_ub.rs:18:27
113
|
12-
LL | let offset = unsafe { field_ptr.offset_from(base_ptr) };
13-
| ------------------------------- inside `DIFFERENT_ALLOC` at $DIR/offset_from_ub.rs:15:27
4+
LL | let offset = unsafe { ptr_offset_from(field_ptr, base_ptr) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ptr_offset_from cannot compute offset of pointers into different allocations.
146

157
error: any use of this value will cause an error
168
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
@@ -20,12 +12,11 @@ LL | unsafe { intrinsics::ptr_offset_from(self, origin) }
2012
| |
2113
| unable to turn bytes into a pointer
2214
| inside `ptr::const_ptr::<impl *const u8>::offset_from` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
23-
| inside `NOT_PTR` at $DIR/offset_from_ub.rs:21:14
15+
| inside `NOT_PTR` at $DIR/offset_from_ub.rs:24:14
2416
|
25-
::: $DIR/offset_from_ub.rs:19:1
17+
::: $DIR/offset_from_ub.rs:23:1
2618
|
2719
LL | / pub const NOT_PTR: usize = {
28-
LL | |
2920
LL | | unsafe { (42 as *const u8).offset_from(&5u8) as usize }
3021
LL | | };
3122
| |__-
@@ -35,50 +26,26 @@ LL | | };
3526
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
3627

3728
error[E0080]: evaluation of constant value failed
38-
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
29+
--> $DIR/offset_from_ub.rs:31:14
3930
|
40-
LL | unsafe { intrinsics::ptr_offset_from(self, origin) }
41-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42-
| |
43-
| exact_div: 1_isize cannot be divided by 2_isize without remainder
44-
| inside `ptr::const_ptr::<impl *const u16>::offset_from` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
45-
|
46-
::: $DIR/offset_from_ub.rs:28:14
47-
|
48-
LL | unsafe { field_ptr.offset_from(base_ptr as *const u16) }
49-
| --------------------------------------------- inside `NOT_MULTIPLE_OF_SIZE` at $DIR/offset_from_ub.rs:28:14
31+
LL | unsafe { ptr_offset_from(field_ptr, base_ptr as *const u16) }
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ exact_div: 1_isize cannot be divided by 2_isize without remainder
5033

5134
error[E0080]: evaluation of constant value failed
52-
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
53-
|
54-
LL | unsafe { intrinsics::ptr_offset_from(self, origin) }
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
56-
| |
57-
| null pointer is not a valid pointer for this operation
58-
| inside `ptr::const_ptr::<impl *const u8>::offset_from` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
59-
|
60-
::: $DIR/offset_from_ub.rs:33:14
35+
--> $DIR/offset_from_ub.rs:37:14
6136
|
62-
LL | unsafe { ptr.offset_from(ptr) }
63-
| -------------------- inside `OFFSET_FROM_NULL` at $DIR/offset_from_ub.rs:33:14
37+
LL | unsafe { ptr_offset_from(ptr, ptr) }
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer for this operation
6439

6540
error: any use of this value will cause an error
66-
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
67-
|
68-
LL | unsafe { intrinsics::ptr_offset_from(self, origin) }
69-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
70-
| |
71-
| unable to turn bytes into a pointer
72-
| inside `ptr::const_ptr::<impl *const u8>::offset_from` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
73-
| inside `DIFFERENT_INT` at $DIR/offset_from_ub.rs:40:14
74-
|
75-
::: $DIR/offset_from_ub.rs:36:1
41+
--> $DIR/offset_from_ub.rs:44:14
7642
|
7743
LL | / pub const DIFFERENT_INT: isize = { // offset_from with two different integers: like DIFFERENT_ALLOC
78-
LL | |
7944
LL | | let ptr1 = 8 as *const u8;
8045
LL | | let ptr2 = 16 as *const u8;
81-
LL | | unsafe { ptr2.offset_from(ptr1) }
46+
LL | | unsafe { ptr_offset_from(ptr2, ptr1) }
47+
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn bytes into a pointer
48+
LL | |
8249
LL | | };
8350
| |__-
8451
|

0 commit comments

Comments
 (0)