jyn514 / saltwater

A C compiler written in Rust, with a focus on good error messages.
BSD 3-Clause "New" or "Revised" License
294 stars 27 forks source link

Pre-increment on pointer to struct causes error #500

Open ryankeleti opened 4 years ago

ryankeleti commented 4 years ago

When attempting to pre-increment a pointer to a struct, compiler throws error that operation is not between arithmetic types or a pointer operation.

Code

typedef struct {
  int i;
} s;

int main() {
  s *x;
  ++x;
}

Error

struct.c:7:3 error: invalid program: invalid operators for '+' (expected either arithmetic types or pointer operation, got 'struct {
    int i;
} * + struct {
    int i;
} *'
  ++x;
  ^^
1 error generated

Potential cause of error, paraphrasing @jyn514 here: It thinks that it's adding a 1 of type pointer, but it's adding a 1 of type integer here. This is a bug in desugaring complex assignment then here. It needs to be smart and only cast if it would be valid.