Skip to content

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

Closed
@comex

Description

@comex

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions