c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Error message for casting pointer difference is confusing

Open orangebowlerhat opened this issue 6 months ago • 1 comments

Given code like this

char* a = 0x20; // not a sensible value, just for example purposes
char* b = 0x25;
uint diff = b - a;

Produces the error message

Error: 'isz' (long) cannot implicitly be converted to 'uint', but you may use a cast.

This message is confusing. It's unclear what to solve given that the error mentions 'isz' but nothing in the error line is a size value. A message more like the following might explain the problem better.

The difference of two pointers cannot be implicitly converted to 'uint', but you may use a cast

orangebowlerhat avatar Jun 23 '25 15:06 orangebowlerhat

This should be fixed now.

lerno avatar Jun 23 '25 21:06 lerno

Is this working?

lerno avatar Jul 07 '25 10:07 lerno

On both v0.7.2 and v0.7.3 I get an error about the char* a = 0x20; part.

  • On 0.7.2 the error is
Error: Implicitly casting 'char' to 'char*' is not permitted, but you may do an explicit cast...
  • On 0.7.3 the error is
Error: Implicitly casting 'uint' to 'char*' is not permitted...

That changed too apparently, but ok.

When I add the casts for those and look at the errors for uint diff = b - a;, then

  • On 0.7.2:
Error: 'isz' (long) cannot implicitly be converted to 'uint', but you may use a cast.
  • On 0.7.3:
Error: A pointer diff has the type 'isz' (long) which cannot implicitly be converted to 'uint'. You can use an explicit cast if you know the conversion is safe.

BWindey avatar Jul 07 '25 10:07 BWindey

That looks correct. I'll close it. Thank you for checking @BWindey

lerno avatar Jul 07 '25 22:07 lerno