improve test by using intrinsic directly

This commit is contained in:
Ralf Jung 2021-06-18 19:43:03 +02:00
parent 3061777c48
commit 7475661678
2 changed files with 26 additions and 54 deletions

View File

@ -1,5 +1,8 @@
#![feature(const_raw_ptr_deref)] #![feature(const_raw_ptr_deref)]
#![feature(const_ptr_offset_from)] #![feature(const_ptr_offset_from)]
#![feature(core_intrinsics)]
use std::intrinsics::ptr_offset_from;
#[repr(C)] #[repr(C)]
struct Struct { struct Struct {
@ -12,12 +15,12 @@ pub const DIFFERENT_ALLOC: usize = {
let base_ptr: *const Struct = &uninit as *const _ as *const Struct; let base_ptr: *const Struct = &uninit as *const _ as *const Struct;
let uninit2 = std::mem::MaybeUninit::<Struct>::uninit(); let uninit2 = std::mem::MaybeUninit::<Struct>::uninit();
let field_ptr: *const Struct = &uninit2 as *const _ as *const Struct; let field_ptr: *const Struct = &uninit2 as *const _ as *const Struct;
let offset = unsafe { field_ptr.offset_from(base_ptr) }; //~NOTE inside `DIFFERENT_ALLOC` at let offset = unsafe { ptr_offset_from(field_ptr, base_ptr) }; //~ERROR evaluation of constant value failed
//~| cannot compute offset of pointers into different allocations.
offset as usize offset as usize
}; };
pub const NOT_PTR: usize = { pub const NOT_PTR: usize = {
//~^ NOTE
unsafe { (42 as *const u8).offset_from(&5u8) as usize } unsafe { (42 as *const u8).offset_from(&5u8) as usize }
}; };
@ -25,19 +28,21 @@ pub const NOT_MULTIPLE_OF_SIZE: isize = {
let data = [5u8, 6, 7]; let data = [5u8, 6, 7];
let base_ptr = data.as_ptr(); let base_ptr = data.as_ptr();
let field_ptr = &data[1] as *const u8 as *const u16; let field_ptr = &data[1] as *const u8 as *const u16;
unsafe { field_ptr.offset_from(base_ptr as *const u16) } //~NOTE inside `NOT_MULTIPLE_OF_SIZE` at unsafe { ptr_offset_from(field_ptr, base_ptr as *const u16) } //~ERROR evaluation of constant value failed
//~| 1_isize cannot be divided by 2_isize without remainder
}; };
pub const OFFSET_FROM_NULL: isize = { pub const OFFSET_FROM_NULL: isize = {
let ptr = 0 as *const u8; let ptr = 0 as *const u8;
unsafe { ptr.offset_from(ptr) } //~NOTE inside `OFFSET_FROM_NULL` at unsafe { ptr_offset_from(ptr, ptr) } //~ERROR evaluation of constant value failed
//~| null pointer is not a valid pointer
}; };
pub const DIFFERENT_INT: isize = { // offset_from with two different integers: like DIFFERENT_ALLOC pub const DIFFERENT_INT: isize = { // offset_from with two different integers: like DIFFERENT_ALLOC
//~^ NOTE
let ptr1 = 8 as *const u8; let ptr1 = 8 as *const u8;
let ptr2 = 16 as *const u8; let ptr2 = 16 as *const u8;
unsafe { ptr2.offset_from(ptr1) } unsafe { ptr_offset_from(ptr2, ptr1) } //~ERROR any use of this value will cause an error
//~| WARN previously accepted
}; };
fn main() {} fn main() {}

View File

@ -1,16 +1,8 @@
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $DIR/offset_from_ub.rs:18:27
| |
LL | unsafe { intrinsics::ptr_offset_from(self, origin) } LL | let offset = unsafe { ptr_offset_from(field_ptr, base_ptr) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ptr_offset_from cannot compute offset of pointers into different allocations.
| |
| ptr_offset_from cannot compute offset of pointers into different allocations.
| inside `ptr::const_ptr::<impl *const Struct>::offset_from` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
::: $DIR/offset_from_ub.rs:15:27
|
LL | let offset = unsafe { field_ptr.offset_from(base_ptr) };
| ------------------------------- inside `DIFFERENT_ALLOC` at $DIR/offset_from_ub.rs:15:27
error: any use of this value will cause an error error: any use of this value will cause an error
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
@ -20,12 +12,11 @@ LL | unsafe { intrinsics::ptr_offset_from(self, origin) }
| | | |
| unable to turn bytes into a pointer | unable to turn bytes into a pointer
| inside `ptr::const_ptr::<impl *const u8>::offset_from` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL | inside `ptr::const_ptr::<impl *const u8>::offset_from` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
| inside `NOT_PTR` at $DIR/offset_from_ub.rs:21:14 | inside `NOT_PTR` at $DIR/offset_from_ub.rs:24:14
| |
::: $DIR/offset_from_ub.rs:19:1 ::: $DIR/offset_from_ub.rs:23:1
| |
LL | / pub const NOT_PTR: usize = { LL | / pub const NOT_PTR: usize = {
LL | |
LL | | unsafe { (42 as *const u8).offset_from(&5u8) as usize } LL | | unsafe { (42 as *const u8).offset_from(&5u8) as usize }
LL | | }; LL | | };
| |__- | |__-
@ -35,50 +26,26 @@ LL | | };
= note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800> = note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $DIR/offset_from_ub.rs:31:14
| |
LL | unsafe { intrinsics::ptr_offset_from(self, origin) } LL | unsafe { ptr_offset_from(field_ptr, base_ptr as *const u16) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ exact_div: 1_isize cannot be divided by 2_isize without remainder
| |
| exact_div: 1_isize cannot be divided by 2_isize without remainder
| inside `ptr::const_ptr::<impl *const u16>::offset_from` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
::: $DIR/offset_from_ub.rs:28:14
|
LL | unsafe { field_ptr.offset_from(base_ptr as *const u16) }
| --------------------------------------------- inside `NOT_MULTIPLE_OF_SIZE` at $DIR/offset_from_ub.rs:28:14
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $DIR/offset_from_ub.rs:37:14
| |
LL | unsafe { intrinsics::ptr_offset_from(self, origin) } LL | unsafe { ptr_offset_from(ptr, ptr) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer for this operation
| |
| null pointer is not a valid pointer for this operation
| inside `ptr::const_ptr::<impl *const u8>::offset_from` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
|
::: $DIR/offset_from_ub.rs:33:14
|
LL | unsafe { ptr.offset_from(ptr) }
| -------------------- inside `OFFSET_FROM_NULL` at $DIR/offset_from_ub.rs:33:14
error: any use of this value will cause an error error: any use of this value will cause an error
--> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL --> $DIR/offset_from_ub.rs:44:14
|
LL | unsafe { intrinsics::ptr_offset_from(self, origin) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| unable to turn bytes into a pointer
| inside `ptr::const_ptr::<impl *const u8>::offset_from` at $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
| inside `DIFFERENT_INT` at $DIR/offset_from_ub.rs:40:14
|
::: $DIR/offset_from_ub.rs:36:1
| |
LL | / pub const DIFFERENT_INT: isize = { // offset_from with two different integers: like DIFFERENT_ALLOC LL | / pub const DIFFERENT_INT: isize = { // offset_from with two different integers: like DIFFERENT_ALLOC
LL | |
LL | | let ptr1 = 8 as *const u8; LL | | let ptr1 = 8 as *const u8;
LL | | let ptr2 = 16 as *const u8; LL | | let ptr2 = 16 as *const u8;
LL | | unsafe { ptr2.offset_from(ptr1) } LL | | unsafe { ptr_offset_from(ptr2, ptr1) }
| | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ unable to turn bytes into a pointer
LL | |
LL | | }; LL | | };
| |__- | |__-
| |