zig icon indicating copy to clipboard operation
zig copied to clipboard

Return a `usize` from `@abs` if given an `isize`

Open FnControlOption opened this issue 8 months ago • 3 comments

Also:

  • c_ushort for c_short
  • c_uint for c_int
  • c_ulong for c_long
  • c_ulonglong for c_longlong

Given an isize argument, @abs now returns a usize instead of a u* integer that has the same size as a pointer.

For example, @TypeOf(@abs(@as(isize, -1))) should produce usize instead of u64.

FnControlOption avatar Apr 16 '25 19:04 FnControlOption

dont have stats on how common this is but felt it was worth mentioning this would break the following code

var x: isize = -24;
x = @abs(x);

nektro avatar Apr 27 '25 19:04 nektro

That's already a compile error in current Zig:

prog.zig:6:6: error: expected type 'isize', found 'u64'
 x = @abs(x);
     ^~~~~~~
prog.zig:6:6: note: signed 64-bit int cannot represent all possible unsigned 64-bit values

FnControlOption avatar Apr 27 '25 20:04 FnControlOption

Pushed my src/Type.zig suggestion, and compacted the tests a bit. Thanks!

mlugg avatar Jul 30 '25 21:07 mlugg