Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2025

Conversation

Kivooeo
Copy link
Contributor

@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
Contributor 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
Comment on lines 700 to 701
// If there any better way to get lhs name variable please tell me :)
// I really wanna know
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't leave comments like this in the codebase. If you have questions for the reviewer, leave them as review comments on the github PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, I see, I thought is a good way to ask something related to code, comments will anyway be removed before merge, but it's a good remark I'll keep that in mind thanks

if lhs_ty.is_raw_ptr() && rhs_ty.is_integral() =>
{
err.multipart_suggestion(
"consider replacing `ptr += offset` with `ptr = ptr.wrapping_add(offset)` or `ptr.add(offset)`",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The suggestion is already responsible for showing what should be replaced. I think this can be made a lot shorter -- something like "consider using add or wrapping_add to do pointer arithmetic"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, fixed

@compiler-errors
Copy link
Member

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
Member

@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