bors
fd0a3313f7
Auto merge of #112261 - jieyouxu:c-like-ptr-arithmetics-diagnostics, r=WaffleLapkin
Add help for trying to do C-like pointer arithmetics
This PR adds help messages for these cases:
```rust
fn main() {
let ptr1: *const u32 = std::ptr::null();
let ptr2: *const u32 = std::ptr::null();
let a = ptr1 + 5;
let b = ptr1 - 5;
let c = ptr2 - ptr1;
let d = ptr1[5];
}
```
### Current Output
```
error[E0369]: cannot add `{integer}` to `*const u32`
--> tests/ui/typeck/issue-112252-ptr-arithmetics-help.rs:4:18
|
4 | let a = ptr1 + 5; //~ ERROR cannot add
| ---- ^ - {integer}
| |
| *const u32
error[E0369]: cannot subtract `{integer}` from `*const u32`
--> tests/ui/typeck/issue-112252-ptr-arithmetics-help.rs:5:18
|
5 | let b = ptr1 - 5; //~ ERROR cannot subtract
| ---- ^ - {integer}
| |
| *const u32
error[E0369]: cannot subtract `*const u32` from `*const u32`
--> tests/ui/typeck/issue-112252-ptr-arithmetics-help.rs:6:18
|
6 | let c = ptr2 - ptr1; //~ ERROR cannot subtract
| ---- ^ ---- *const u32
| |
| *const u32
error[E0608]: cannot index into a value of type `*const u32`
--> tests/ui/typeck/issue-112252-ptr-arithmetics-help.rs:7:13
|
7 | let d = ptr1[5]; //~ ERROR cannot index
| ^^^^^^^
error: aborting due to 4 previous errors
```
### Output After This PR
```
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}: `_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 `offset` for pointer - {integer}: `unsafe { _ptr1.offset(-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: `_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
```
Closes #112252.
2023-06-12 07:15:19 +00:00
..
2023-06-04 00:51:49 -07:00
2023-06-01 18:21:42 +00:00
2023-06-04 03:39:24 +00:00
2023-06-09 09:45:40 +00:00
2023-06-09 09:45:40 +00:00
2023-06-10 18:28:14 +00:00
2023-06-10 14:38:08 +02:00
2023-06-10 14:08:26 -07:00
2023-06-10 14:08:26 -07:00
2023-06-12 07:15:19 +00:00
2023-06-01 14:45:18 +00:00