Skip to content

Diagnostics when trying to do pointer arithmetic like in C #112252

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

Closed
comex opened this issue Jun 3, 2023 · 0 comments · Fixed by #112261
Closed

Diagnostics when trying to do pointer arithmetic like in C #112252

comex opened this issue Jun 3, 2023 · 0 comments · Fixed by #112261
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@comex
Copy link
Contributor

comex commented Jun 3, 2023

Code

fn foo(ptr1: *const u32, ptr2: *const u32) {
    // In C these would be valid expressions:
    let x = ptr1 + 5;
    let y = ptr1[5];
    let z = ptr2 - ptr1;
}

Current output

error[E0369]: cannot add `{integer}` to `*const u32`
 --> src/lib.rs:3:18
  |
3 |     let x = ptr1 + 5;
  |             ---- ^ - {integer}
  |             |
  |             *const u32

error[E0608]: cannot index into a value of type `*const u32`
 --> src/lib.rs:4:13
  |
4 |     let y = ptr1[5];
  |             ^^^^^^^

error[E0369]: cannot subtract `*const u32` from `*const u32`
 --> src/lib.rs:5:18
  |
5 |     let z = ptr2 - ptr1;
  |             ---- ^ ---- *const u32
  |             |
  |             *const u32

Desired output

Suggest using .offset (or .add) for addition, and .offset_from for subtraction.

@comex comex added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 3, 2023
@bors bors closed this as completed in fd0a331 Jun 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant