about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
author许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com>2023-06-04 10:18:09 +0800
committer许杰友 Jieyou Xu (Joe) <jieyouxu@outlook.com>2023-06-09 10:01:16 +0800
commit63d643da841bedd6b855f617edecc0b13c0cd71e (patch)
treedd76f9ba433420e012df2988e2c77836146f352d /tests
parent8b35c0bb0f833c0077dc57006eb317edde2a2d1e (diff)
downloadrust-63d643da841bedd6b855f617edecc0b13c0cd71e.tar.gz
rust-63d643da841bedd6b855f617edecc0b13c0cd71e.zip
Add help for trying to do C-like pointer arithmetics
Diffstat (limited to 'tests')
-rw-r--r--tests/ui/typeck/issue-112252-ptr-arithmetics-help.fixed10
-rw-r--r--tests/ui/typeck/issue-112252-ptr-arithmetics-help.rs10
-rw-r--r--tests/ui/typeck/issue-112252-ptr-arithmetics-help.stderr54
3 files changed, 74 insertions, 0 deletions
diff --git a/tests/ui/typeck/issue-112252-ptr-arithmetics-help.fixed b/tests/ui/typeck/issue-112252-ptr-arithmetics-help.fixed
new file mode 100644
index 00000000000..bdb884f5431
--- /dev/null
+++ b/tests/ui/typeck/issue-112252-ptr-arithmetics-help.fixed
@@ -0,0 +1,10 @@
+// run-rustfix
+
+fn main() {
+    let _ptr1: *const u32 = std::ptr::null();
+    let _ptr2: *const u32 = std::ptr::null();
+    let _a = _ptr1.wrapping_add(5); //~ ERROR cannot add
+    let _b = _ptr1.wrapping_sub(5); //~ ERROR cannot subtract
+    let _c = unsafe { _ptr2.offset_from(_ptr1) }; //~ ERROR cannot subtract
+    let _d = _ptr1.wrapping_add(5); //~ ERROR cannot index
+}
diff --git a/tests/ui/typeck/issue-112252-ptr-arithmetics-help.rs b/tests/ui/typeck/issue-112252-ptr-arithmetics-help.rs
new file mode 100644
index 00000000000..cf68850cc4d
--- /dev/null
+++ b/tests/ui/typeck/issue-112252-ptr-arithmetics-help.rs
@@ -0,0 +1,10 @@
+// run-rustfix
+
+fn main() {
+    let _ptr1: *const u32 = std::ptr::null();
+    let _ptr2: *const u32 = std::ptr::null();
+    let _a = _ptr1 + 5; //~ ERROR cannot add
+    let _b = _ptr1 - 5; //~ ERROR cannot subtract
+    let _c = _ptr2 - _ptr1; //~ ERROR cannot subtract
+    let _d = _ptr1[5]; //~ ERROR cannot index
+}
diff --git a/tests/ui/typeck/issue-112252-ptr-arithmetics-help.stderr b/tests/ui/typeck/issue-112252-ptr-arithmetics-help.stderr
new file mode 100644
index 00000000000..c55930da225
--- /dev/null
+++ b/tests/ui/typeck/issue-112252-ptr-arithmetics-help.stderr
@@ -0,0 +1,54 @@
+error[E0369]: cannot add `{integer}` to `*const u32`
+  --> $DIR/issue-112252-ptr-arithmetics-help.rs:6:20
+   |
+LL |     let _a = _ptr1 + 5;
+   |              ----- ^ - {integer}
+   |              |
+   |              *const u32
+   |
+help: consider using `wrapping_add` or `add` for pointer + {integer}
+   |
+LL |     let _a = _ptr1.wrapping_add(5);
+   |                   ~~~~~~~~~~~~~~ +
+
+error[E0369]: cannot subtract `{integer}` from `*const u32`
+  --> $DIR/issue-112252-ptr-arithmetics-help.rs:7:20
+   |
+LL |     let _b = _ptr1 - 5;
+   |              ----- ^ - {integer}
+   |              |
+   |              *const u32
+   |
+help: consider using `wrapping_sub` or `sub` for pointer - {integer}
+   |
+LL |     let _b = _ptr1.wrapping_sub(5);
+   |                   ~~~~~~~~~~~~~~ +
+
+error[E0369]: cannot subtract `*const u32` from `*const u32`
+  --> $DIR/issue-112252-ptr-arithmetics-help.rs:8:20
+   |
+LL |     let _c = _ptr2 - _ptr1;
+   |              ----- ^ ----- *const u32
+   |              |
+   |              *const u32
+   |
+help: consider using `offset_from` for pointer - pointer if the pointers point to the same allocation
+   |
+LL |     let _c = unsafe { _ptr2.offset_from(_ptr1) };
+   |              ++++++++      ~~~~~~~~~~~~~     +++
+
+error[E0608]: cannot index into a value of type `*const u32`
+  --> $DIR/issue-112252-ptr-arithmetics-help.rs:9:14
+   |
+LL |     let _d = _ptr1[5];
+   |              ^^^^^^^^
+   |
+help: consider using `wrapping_add` or `add` for indexing into raw pointer
+   |
+LL |     let _d = _ptr1.wrapping_add(5);
+   |                   ~~~~~~~~~~~~~~ ~
+
+error: aborting due to 4 previous errors
+
+Some errors have detailed explanations: E0369, E0608.
+For more information about an error, try `rustc --explain E0369`.