Skip to content

Conversation

@Kivooeo
Copy link
Member

@Kivooeo Kivooeo commented Apr 20, 2025

Description:

This PR improves the diagnostic message for cases where a binary assignment operation like ptr += offset or ptr -= offset is attempted on *mut T. These operations are not allowed, and the compiler previously suggested calling .add() or .wrapping_add(), which is misleading if not assigned.

This PR updates the diagnostics to suggest assigning the result of .wrapping_add() or .wrapping_sub() back to the pointer, e.g.:

Examples

For this code

let mut arr = [0u8; 10];
let mut ptr = arr.as_mut_ptr();

ptr += 2;

it will say:

10 |     ptr += 2;
   |     ---^^^^^
   |     |
   |     cannot use `+=` on type `*mut u8`
   |
help: consider replacing `ptr += offset` with `ptr = ptr.wrapping_add(offset)` or `ptr.add(offset)`
   |
10 -     ptr += 2;
10 +     ptr = ptr.wrapping_add(2);

Related issue: #137391
cc @nabijaczleweli for context (issue author)

@rustbot
Copy link
Collaborator

rustbot commented Apr 20, 2025

r? @nnethercote

rustbot has assigned @nnethercote.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 20, 2025
@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 20, 2025
@Kivooeo
Copy link
Member Author

Kivooeo commented Apr 20, 2025

Oh wait, I did something very wrong with brances...
(fixed now ^^)

@Kivooeo Kivooeo force-pushed the raw-pointer-assignment-suggestion branch from 7f207ae to 5fcf14a Compare April 20, 2025 20:51
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 20, 2025
@Kivooeo Kivooeo force-pushed the raw-pointer-assignment-suggestion branch from 5fcf14a to 023fab8 Compare April 20, 2025 20:56
@compiler-errors
Copy link
Contributor

r? compiler-errors

@rustbot
Copy link
Collaborator

rustbot commented Apr 21, 2025

Some changes occurred in src/tools/cargo

cc @ehuss

@rustbot

This comment has been minimized.

@Kivooeo Kivooeo force-pushed the raw-pointer-assignment-suggestion branch from bc620e4 to 9d69c35 Compare April 21, 2025 17:11
@Kivooeo Kivooeo force-pushed the raw-pointer-assignment-suggestion branch from 9d69c35 to 834e476 Compare April 21, 2025 17:14
@compiler-errors
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Collaborator

bors commented Apr 22, 2025

📌 Commit 834e476 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Apr 22, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 22, 2025
…enton

Rollup of 5 pull requests

Successful merges:

 - rust-lang#139981 (Don't compute name of associated item if it's an RPITIT)
 - rust-lang#140077 (Construct OutputType using macro and print [=FILENAME] help info)
 - rust-lang#140081 (Update `libc` to 0.2.172)
 - rust-lang#140094 (Improve diagnostics for pointer arithmetic += and -= (fixes rust-lang#137391))
 - rust-lang#140128 (Use correct annotation for CSS pseudo elements)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 8f42ac0 into rust-lang:master Apr 22, 2025
6 checks passed
@rustbot rustbot added this to the 1.88.0 milestone Apr 22, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Apr 22, 2025
Rollup merge of rust-lang#140094 - Kivooeo:raw-pointer-assignment-suggestion, r=compiler-errors

Improve diagnostics for pointer arithmetic += and -= (fixes rust-lang#137391)

**Description**:

This PR improves the diagnostic message for cases where a binary assignment operation like `ptr += offset` or `ptr -= offset` is attempted on `*mut T`. These operations are not allowed, and the compiler previously suggested calling `.add()` or `.wrapping_add()`, which is misleading if not assigned.

This PR updates the diagnostics to suggest assigning the result of `.wrapping_add()` or `.wrapping_sub()` back to the pointer, e.g.:

**Examples**

For this code
```rust
let mut arr = [0u8; 10];
let mut ptr = arr.as_mut_ptr();

ptr += 2;
```
it will say:
```rust
10 |     ptr += 2;
   |     ---^^^^^
   |     |
   |     cannot use `+=` on type `*mut u8`
   |
help: consider replacing `ptr += offset` with `ptr = ptr.wrapping_add(offset)` or `ptr.add(offset)`
   |
10 -     ptr += 2;
10 +     ptr = ptr.wrapping_add(2);
```

**Related issue**: rust-lang#137391
cc `@nabijaczleweli` for context (issue author)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants