-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Improve diagnostics for pointer arithmetic += and -= (fixes #137391) #140094
Conversation
r? @nnethercote rustbot has assigned @nnethercote. Use |
This comment has been minimized.
This comment has been minimized.
Oh wait, I did something very wrong with brances... |
7f207ae
to
5fcf14a
Compare
5fcf14a
to
023fab8
Compare
compiler/rustc_hir_typeck/src/op.rs
Outdated
// If there any better way to get lhs name variable please tell me :) | ||
// I really wanna know |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
compiler/rustc_hir_typeck/src/op.rs
Outdated
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)`", |
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, fixed
r? compiler-errors |
Some changes occurred in src/tools/cargo cc @ehuss |
This comment has been minimized.
This comment has been minimized.
bc620e4
to
9d69c35
Compare
9d69c35
to
834e476
Compare
@bors r+ rollup |
…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
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)
Description:
This PR improves the diagnostic message for cases where a binary assignment operation like
ptr += offset
orptr -= 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
it will say:
Related issue: #137391
cc @nabijaczleweli for context (issue author)