about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRalf Jung <post@ralfj.de>2021-06-18 19:43:03 +0200
committerRalf Jung <post@ralfj.de>2021-06-18 19:43:03 +0200
commit7475661678359a081f8a66cd35f3b3993cf788ba (patch)
treec547d4d7ec2adffd48dcb489a24b42d9b8687eba
parent3061777c4815ebaac8eff14d3fc1773045ce359d (diff)
downloadrust-7475661678359a081f8a66cd35f3b3993cf788ba.tar.gz
rust-7475661678359a081f8a66cd35f3b3993cf788ba.zip
improve test by using intrinsic directly
-rw-r--r--src/test/ui/consts/offset_from_ub.rs17
-rw-r--r--src/test/ui/consts/offset_from_ub.stderr63
2 files changed, 26 insertions, 54 deletions
diff --git a/src/test/ui/consts/offset_from_ub.rs b/src/test/ui/consts/offset_from_ub.rs
index 563535f4e38..f8fa2c5f177 100644
--- a/src/test/ui/consts/offset_from_ub.rs
+++ b/src/test/ui/consts/offset_from_ub.rs
@@ -1,5 +1,8 @@
 #![feature(const_raw_ptr_deref)]
 #![feature(const_ptr_offset_from)]
+#![feature(core_intrinsics)]
+
+use std::intrinsics::ptr_offset_from;
 
 #[repr(C)]
 struct Struct {
@@ -12,12 +15,12 @@ pub const DIFFERENT_ALLOC: usize = {
     let base_ptr: *const Struct = &uninit as *const _ as *const Struct;
     let uninit2 = std::mem::MaybeUninit::<Struct>::uninit();
     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
 };
 
 pub const NOT_PTR: usize = {
-    //~^ NOTE
     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 base_ptr = data.as_ptr();
     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 = {
     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
-    //~^ NOTE
     let ptr1 = 8 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() {}
diff --git a/src/test/ui/consts/offset_from_ub.stderr b/src/test/ui/consts/offset_from_ub.stderr
index f39fee8a62a..4c2ba9297d8 100644
--- a/src/test/ui/consts/offset_from_ub.stderr
+++ b/src/test/ui/consts/offset_from_ub.stderr
@@ -1,16 +1,8 @@
 error[E0080]: evaluation of constant value failed
-  --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
-   |
-LL |         unsafe { intrinsics::ptr_offset_from(self, origin) }
-   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   |                  |
-   |                  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
+  --> $DIR/offset_from_ub.rs:18:27
    |
-LL |     let offset = unsafe { field_ptr.offset_from(base_ptr) };
-   |                           ------------------------------- inside `DIFFERENT_ALLOC` at $DIR/offset_from_ub.rs:15:27
+LL |     let offset = unsafe { ptr_offset_from(field_ptr, base_ptr) };
+   |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ptr_offset_from cannot compute offset of pointers into different allocations.
 
 error: any use of this value will cause an error
   --> $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
    |                    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 | |
 LL | |     unsafe { (42 as *const u8).offset_from(&5u8) as usize }
 LL | | };
    | |__-
@@ -35,50 +26,26 @@ LL | | };
    = note: for more information, see issue #71800 <https://github.com/rust-lang/rust/issues/71800>
 
 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) }
-   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   |                  |
-   |                  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
+LL |     unsafe { ptr_offset_from(field_ptr, base_ptr as *const u16) }
+   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ exact_div: 1_isize cannot be divided by 2_isize without remainder
 
 error[E0080]: evaluation of constant value failed
-  --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
-   |
-LL |         unsafe { intrinsics::ptr_offset_from(self, origin) }
-   |                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
-   |                  |
-   |                  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
+  --> $DIR/offset_from_ub.rs:37:14
    |
-LL |     unsafe { ptr.offset_from(ptr) }
-   |              -------------------- inside `OFFSET_FROM_NULL` at $DIR/offset_from_ub.rs:33:14
+LL |     unsafe { ptr_offset_from(ptr, ptr) }
+   |              ^^^^^^^^^^^^^^^^^^^^^^^^^ null pointer is not a valid pointer for this operation
 
 error: any use of this value will cause an error
-  --> $SRC_DIR/core/src/ptr/const_ptr.rs:LL:COL
-   |
-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
+  --> $DIR/offset_from_ub.rs:44:14
    |
 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 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 | | };
    | |__-
    |