Commit fd0a331
committed
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.File tree
5 files changed
+135
-0
lines changed- compiler/rustc_hir_typeck/src
- tests/ui/typeck
5 files changed
+135
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2871 | 2871 | | |
2872 | 2872 | | |
2873 | 2873 | | |
| 2874 | + | |
| 2875 | + | |
| 2876 | + | |
| 2877 | + | |
| 2878 | + | |
| 2879 | + | |
| 2880 | + | |
| 2881 | + | |
| 2882 | + | |
| 2883 | + | |
| 2884 | + | |
| 2885 | + | |
| 2886 | + | |
| 2887 | + | |
| 2888 | + | |
2874 | 2889 | | |
2875 | 2890 | | |
2876 | 2891 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
521 | 521 | | |
522 | 522 | | |
523 | 523 | | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
524 | 570 | | |
525 | 571 | | |
526 | 572 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
0 commit comments