authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-02-28 03:42:21+01:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-02-28 04:30:49+01:00
logf446d8e8f9bbfc52e73649b1754dab88d4acfb07
tree20043828c848344c5e622da282c291f5aa1027de
parent17d0bb5bea1bd6c885759604d564a82e8ab36231

posix: `@as` and other general cleanup


2 files changed, 158 insertions(+), 212 deletions(-)

lib/std/Target.zig+4-1
......@@ -392,7 +392,10 @@ pub const Os = struct {
392392
393393 /// Checks if system is guaranteed to be at least `version` or older than `version`.
394394 /// Returns `null` if a runtime check is required.
395 pub inline fn isAtLeast(self: Os, comptime tag: Tag, version: anytype) ?bool {
395 pub inline fn isAtLeast(self: Os, comptime tag: Tag, version: switch (tag) {
396 else => std.SemanticVersion,
397 .windows => WindowsVersion,
398 }) ?bool {
396399 if (self.tag != tag) return false;
397400
398401 return switch (tag) {
lib/std/os.zig+154-211
......@@ -302,7 +302,7 @@ pub fn close(fd: fd_t) void {
302302 _ = wasi.fd_close(fd);
303303 return;
304304 }
305 if (comptime builtin.target.isDarwin()) {
305 if (builtin.target.isDarwin()) {
306306 // This avoids the EINTR problem.
307307 switch (darwin.getErrno(darwin.@"close$NOCANCEL"(fd))) {
308308 .BADF => unreachable, // Always a race condition.
......@@ -476,7 +476,7 @@ fn fchmodat2(dirfd: fd_t, path: []const u8, mode: mode_t, flags: u32) FChmodAtEr
476476 const rc = system.openat(dirfd, &path_c, .{ .PATH = true, .NOFOLLOW = true, .CLOEXEC = true }, @as(mode_t, 0));
477477 switch (system.getErrno(rc)) {
478478 .SUCCESS => {
479 pathfd = @as(fd_t, @intCast(rc));
479 pathfd = @intCast(rc);
480480 break;
481481 },
482482 .INTR => continue,
......@@ -550,7 +550,7 @@ pub fn fchown(fd: fd_t, owner: ?uid_t, group: ?gid_t) FChownError!void {
550550 }
551551
552552 while (true) {
553 const res = system.fchown(fd, owner orelse @as(u32, 0) -% 1, group orelse @as(u32, 0) -% 1);
553 const res = system.fchown(fd, owner orelse ~@as(uid_t, 0), group orelse ~@as(gid_t, 0));
554554
555555 switch (system.getErrno(res)) {
556556 .SUCCESS => return,
......@@ -596,7 +596,7 @@ pub fn reboot(cmd: RebootCommand) RebootError!void {
596596 switch (system.getErrno(linux.reboot(
597597 .MAGIC1,
598598 .MAGIC2,
599 @as(linux.LINUX_REBOOT.CMD, cmd),
599 cmd,
600600 switch (cmd) {
601601 .RESTART2 => |s| s,
602602 else => null,
......@@ -639,27 +639,27 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {
639639 std.c.versionCheck(std.SemanticVersion{ .major = 2, .minor = 25, .patch = 0 });
640640
641641 while (buf.len != 0) {
642 const res = if (use_c) blk: {
642 const num_read: usize, const err = if (use_c) res: {
643643 const rc = std.c.getrandom(buf.ptr, buf.len, 0);
644 break :blk .{
645 .num_read = @as(usize, @bitCast(rc)),
646 .err = std.c.getErrno(rc),
644 break :res .{
645 @bitCast(rc),
646 std.c.getErrno(rc),
647647 };
648 } else blk: {
648 } else res: {
649649 const rc = linux.getrandom(buf.ptr, buf.len, 0);
650 break :blk .{
651 .num_read = rc,
652 .err = linux.getErrno(rc),
650 break :res .{
651 rc,
652 linux.getErrno(rc),
653653 };
654654 };
655655
656 switch (res.err) {
657 .SUCCESS => buf = buf[res.num_read..],
656 switch (err) {
657 .SUCCESS => buf = buf[num_read..],
658658 .INVAL => unreachable,
659659 .FAULT => unreachable,
660660 .INTR => continue,
661661 .NOSYS => return getRandomBytesDevURandom(buf),
662 else => return unexpectedErrno(res.err),
662 else => return unexpectedErrno(err),
663663 }
664664 }
665665 return;
......@@ -818,10 +818,10 @@ pub fn exit(status: u8) noreturn {
818818 // exit() is only available if exitBootServices() has not been called yet.
819819 // This call to exit should not fail, so we don't care about its return value.
820820 if (uefi.system_table.boot_services) |bs| {
821 _ = bs.exit(uefi.handle, @as(uefi.Status, @enumFromInt(status)), 0, null);
821 _ = bs.exit(uefi.handle, @enumFromInt(status), 0, null);
822822 }
823823 // If we can't exit, reboot the system instead.
824 uefi.system_table.runtime_services.resetSystem(uefi.tables.ResetType.ResetCold, @as(uefi.Status, @enumFromInt(status)), 0, null);
824 uefi.system_table.runtime_services.resetSystem(.ResetCold, @enumFromInt(status), 0, null);
825825 }
826826 system.exit(status);
827827}
......@@ -893,12 +893,10 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
893893 .macos, .ios, .watchos, .tvos => math.maxInt(i32),
894894 else => math.maxInt(isize),
895895 };
896 const adjusted_len = @min(max_count, buf.len);
897
898896 while (true) {
899 const rc = system.read(fd, buf.ptr, adjusted_len);
897 const rc = system.read(fd, buf.ptr, @min(buf.len, max_count));
900898 switch (errno(rc)) {
901 .SUCCESS => return @as(usize, @intCast(rc)),
899 .SUCCESS => return @intCast(rc),
902900 .INTR => continue,
903901 .INVAL => unreachable,
904902 .FAULT => unreachable,
......@@ -932,7 +930,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
932930pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
933931 if (builtin.os.tag == .windows) {
934932 // TODO improve this to use ReadFileScatter
935 if (iov.len == 0) return @as(usize, 0);
933 if (iov.len == 0) return 0;
936934 const first = iov[0];
937935 return read(fd, first.iov_base[0..first.iov_len]);
938936 }
......@@ -956,12 +954,11 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
956954 else => |err| return unexpectedErrno(err),
957955 }
958956 }
959 const iov_count = math.cast(u31, iov.len) orelse math.maxInt(u31);
957
960958 while (true) {
961 // TODO handle the case when iov_len is too large and get rid of this @intCast
962 const rc = system.readv(fd, iov.ptr, iov_count);
959 const rc = system.readv(fd, iov.ptr, @min(iov.len, IOV_MAX));
963960 switch (errno(rc)) {
964 .SUCCESS => return @as(usize, @intCast(rc)),
961 .SUCCESS => return @intCast(rc),
965962 .INTR => continue,
966963 .INVAL => unreachable,
967964 .FAULT => unreachable,
......@@ -1035,15 +1032,12 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
10351032 .macos, .ios, .watchos, .tvos => math.maxInt(i32),
10361033 else => math.maxInt(isize),
10371034 };
1038 const adjusted_len = @min(max_count, buf.len);
10391035
10401036 const pread_sym = if (lfs64_abi) system.pread64 else system.pread;
1041
1042 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
10431037 while (true) {
1044 const rc = pread_sym(fd, buf.ptr, adjusted_len, ioffset);
1038 const rc = pread_sym(fd, buf.ptr, @min(buf.len, max_count), @bitCast(offset));
10451039 switch (errno(rc)) {
1046 .SUCCESS => return @as(usize, @intCast(rc)),
1040 .SUCCESS => return @intCast(rc),
10471041 .INTR => continue,
10481042 .INVAL => unreachable,
10491043 .FAULT => unreachable,
......@@ -1078,7 +1072,7 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void {
10781072 if (builtin.os.tag == .windows) {
10791073 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
10801074 var eof_info = windows.FILE_END_OF_FILE_INFORMATION{
1081 .EndOfFile = @as(windows.LARGE_INTEGER, @bitCast(length)),
1075 .EndOfFile = @bitCast(length),
10821076 };
10831077
10841078 const rc = windows.ntdll.NtSetInformationFile(
......@@ -1111,11 +1105,9 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void {
11111105 }
11121106 }
11131107
1108 const ftruncate_sym = if (lfs64_abi) system.ftruncate64 else system.ftruncate;
11141109 while (true) {
1115 const ftruncate_sym = if (lfs64_abi) system.ftruncate64 else system.ftruncate;
1116
1117 const ilen = @as(i64, @bitCast(length)); // the OS treats this as unsigned
1118 switch (errno(ftruncate_sym(fd, ilen))) {
1110 switch (errno(ftruncate_sym(fd, @bitCast(length)))) {
11191111 .SUCCESS => return,
11201112 .INTR => continue,
11211113 .FBIG => return error.FileTooBig,
......@@ -1178,15 +1170,11 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
11781170 }
11791171 }
11801172
1181 const iov_count = math.cast(u31, iov.len) orelse math.maxInt(u31);
1182
11831173 const preadv_sym = if (lfs64_abi) system.preadv64 else system.preadv;
1184
1185 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
11861174 while (true) {
1187 const rc = preadv_sym(fd, iov.ptr, iov_count, ioffset);
1175 const rc = preadv_sym(fd, iov.ptr, @min(iov.len, IOV_MAX), @bitCast(offset));
11881176 switch (errno(rc)) {
1189 .SUCCESS => return @as(usize, @bitCast(rc)),
1177 .SUCCESS => return @bitCast(rc),
11901178 .INTR => continue,
11911179 .INVAL => unreachable,
11921180 .FAULT => unreachable,
......@@ -1293,12 +1281,10 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
12931281 .macos, .ios, .watchos, .tvos => math.maxInt(i32),
12941282 else => math.maxInt(isize),
12951283 };
1296 const adjusted_len = @min(max_count, bytes.len);
1297
12981284 while (true) {
1299 const rc = system.write(fd, bytes.ptr, adjusted_len);
1285 const rc = system.write(fd, bytes.ptr, @min(bytes.len, max_count));
13001286 switch (errno(rc)) {
1301 .SUCCESS => return @as(usize, @intCast(rc)),
1287 .SUCCESS => return @intCast(rc),
13021288 .INTR => continue,
13031289 .INVAL => return error.InvalidArgument,
13041290 .FAULT => unreachable,
......@@ -1342,7 +1328,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
13421328pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
13431329 if (builtin.os.tag == .windows) {
13441330 // TODO improve this to use WriteFileScatter
1345 if (iov.len == 0) return @as(usize, 0);
1331 if (iov.len == 0) return 0;
13461332 const first = iov[0];
13471333 return write(fd, first.iov_base[0..first.iov_len]);
13481334 }
......@@ -1367,11 +1353,10 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
13671353 }
13681354 }
13691355
1370 const iov_count = if (iov.len > IOV_MAX) IOV_MAX else @as(u31, @intCast(iov.len));
13711356 while (true) {
1372 const rc = system.writev(fd, iov.ptr, iov_count);
1357 const rc = system.writev(fd, iov.ptr, @min(iov.len, IOV_MAX));
13731358 switch (errno(rc)) {
1374 .SUCCESS => return @as(usize, @intCast(rc)),
1359 .SUCCESS => return @intCast(rc),
13751360 .INTR => continue,
13761361 .INVAL => return error.InvalidArgument,
13771362 .FAULT => unreachable,
......@@ -1455,15 +1440,12 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
14551440 .macos, .ios, .watchos, .tvos => math.maxInt(i32),
14561441 else => math.maxInt(isize),
14571442 };
1458 const adjusted_len = @min(max_count, bytes.len);
14591443
14601444 const pwrite_sym = if (lfs64_abi) system.pwrite64 else system.pwrite;
1461
1462 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
14631445 while (true) {
1464 const rc = pwrite_sym(fd, bytes.ptr, adjusted_len, ioffset);
1446 const rc = pwrite_sym(fd, bytes.ptr, @min(bytes.len, max_count), @bitCast(offset));
14651447 switch (errno(rc)) {
1466 .SUCCESS => return @as(usize, @intCast(rc)),
1448 .SUCCESS => return @intCast(rc),
14671449 .INTR => continue,
14681450 .INVAL => return error.InvalidArgument,
14691451 .FAULT => unreachable,
......@@ -1515,7 +1497,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
15151497 if (have_pwrite_but_not_pwritev) {
15161498 // We could loop here; but proper usage of `pwritev` must handle partial writes anyway.
15171499 // So we simply write the first vector only.
1518 if (iov.len == 0) return @as(usize, 0);
1500 if (iov.len == 0) return 0;
15191501 const first = iov[0];
15201502 return pwrite(fd, first.iov_base[0..first.iov_len], offset);
15211503 }
......@@ -1544,13 +1526,10 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
15441526 }
15451527
15461528 const pwritev_sym = if (lfs64_abi) system.pwritev64 else system.pwritev;
1547
1548 const iov_count = if (iov.len > IOV_MAX) IOV_MAX else @as(u31, @intCast(iov.len));
1549 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
15501529 while (true) {
1551 const rc = pwritev_sym(fd, iov.ptr, iov_count, ioffset);
1530 const rc = pwritev_sym(fd, iov.ptr, @min(iov.len, IOV_MAX), @bitCast(offset));
15521531 switch (errno(rc)) {
1553 .SUCCESS => return @as(usize, @intCast(rc)),
1532 .SUCCESS => return @intCast(rc),
15541533 .INTR => continue,
15551534 .INVAL => return error.InvalidArgument,
15561535 .FAULT => unreachable,
......@@ -1666,11 +1645,10 @@ pub fn openZ(file_path: [*:0]const u8, flags: O, perm: mode_t) OpenError!fd_t {
16661645 }
16671646
16681647 const open_sym = if (lfs64_abi) system.open64 else system.open;
1669
16701648 while (true) {
16711649 const rc = open_sym(file_path, flags, perm);
16721650 switch (errno(rc)) {
1673 .SUCCESS => return @as(fd_t, @intCast(rc)),
1651 .SUCCESS => return @intCast(rc),
16741652 .INTR => continue,
16751653
16761654 .FAULT => unreachable,
......@@ -1863,11 +1841,10 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: O, mode: mode_t) O
18631841 }
18641842
18651843 const openat_sym = if (lfs64_abi) system.openat64 else system.openat;
1866
18671844 while (true) {
18681845 const rc = openat_sym(dir_fd, file_path, flags, mode);
18691846 switch (errno(rc)) {
1870 .SUCCESS => return @as(fd_t, @intCast(rc)),
1847 .SUCCESS => return @intCast(rc),
18711848 .INTR => continue,
18721849
18731850 .FAULT => unreachable,
......@@ -1900,7 +1877,7 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: O, mode: mode_t) O
19001877pub fn dup(old_fd: fd_t) !fd_t {
19011878 const rc = system.dup(old_fd);
19021879 return switch (errno(rc)) {
1903 .SUCCESS => return @as(fd_t, @intCast(rc)),
1880 .SUCCESS => return @intCast(rc),
19041881 .MFILE => error.ProcessFdQuotaExceeded,
19051882 .BADF => unreachable, // invalid file descriptor
19061883 else => |err| return unexpectedErrno(err),
......@@ -2144,11 +2121,11 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
21442121 return result;
21452122 }
21462123
2147 const err = if (builtin.link_libc) blk: {
2124 const err: E = if (builtin.link_libc) err: {
21482125 const c_err = if (std.c.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else std.c._errno().*;
2149 break :blk @as(E, @enumFromInt(c_err));
2150 } else blk: {
2151 break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len));
2126 break :err @enumFromInt(c_err);
2127 } else err: {
2128 break :err errno(system.getcwd(out_buffer.ptr, out_buffer.len));
21522129 };
21532130 switch (err) {
21542131 .SUCCESS => return mem.sliceTo(out_buffer, 0),
......@@ -2880,13 +2857,13 @@ pub fn renameatW(
28802857 // supported in order to avoid either (1) using a redundant call that we can know in advance will return
28812858 // STATUS_NOT_SUPPORTED or (2) only setting IGNORE_READONLY_ATTRIBUTE when >= rs5
28822859 // and therefore having different behavior when the Windows version is >= rs1 but < rs5.
2883 if (comptime builtin.target.os.version_range.windows.min.isAtLeast(.win10_rs5)) {
2860 if (builtin.target.os.isAtLeast(.windows, .win10_rs5) orelse false) {
28842861 const struct_buf_len = @sizeOf(windows.FILE_RENAME_INFORMATION_EX) + (MAX_PATH_BYTES - 1);
28852862 var rename_info_buf: [struct_buf_len]u8 align(@alignOf(windows.FILE_RENAME_INFORMATION_EX)) = undefined;
28862863 const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION_EX) - 1 + new_path_w.len * 2;
28872864 if (struct_len > struct_buf_len) return error.NameTooLong;
28882865
2889 const rename_info = @as(*windows.FILE_RENAME_INFORMATION_EX, @ptrCast(&rename_info_buf));
2866 const rename_info: *windows.FILE_RENAME_INFORMATION_EX = @ptrCast(&rename_info_buf);
28902867 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
28912868
28922869 var flags: windows.ULONG = windows.FILE_RENAME_POSIX_SEMANTICS | windows.FILE_RENAME_IGNORE_READONLY_ATTRIBUTE;
......@@ -2897,7 +2874,7 @@ pub fn renameatW(
28972874 .FileNameLength = @intCast(new_path_w.len * 2), // already checked error.NameTooLong
28982875 .FileName = undefined,
28992876 };
2900 @memcpy(@as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w);
2877 @memcpy((&rename_info.FileName).ptr, new_path_w);
29012878 rc = windows.ntdll.NtSetInformationFile(
29022879 src_fd,
29032880 &io_status_block,
......@@ -2923,7 +2900,7 @@ pub fn renameatW(
29232900 const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION) - 1 + new_path_w.len * 2;
29242901 if (struct_len > struct_buf_len) return error.NameTooLong;
29252902
2926 const rename_info = @as(*windows.FILE_RENAME_INFORMATION, @ptrCast(&rename_info_buf));
2903 const rename_info: *windows.FILE_RENAME_INFORMATION = @ptrCast(&rename_info_buf);
29272904 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
29282905
29292906 rename_info.* = .{
......@@ -2932,7 +2909,7 @@ pub fn renameatW(
29322909 .FileNameLength = @intCast(new_path_w.len * 2), // already checked error.NameTooLong
29332910 .FileName = undefined,
29342911 };
2935 @memcpy(@as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w);
2912 @memcpy((&rename_info.FileName).ptr, new_path_w);
29362913
29372914 rc =
29382915 windows.ntdll.NtSetInformationFile(
......@@ -3380,7 +3357,7 @@ pub fn readlinkZ(file_path: [*:0]const u8, out_buffer: []u8) ReadLinkError![]u8
33803357 }
33813358 const rc = system.readlink(file_path, out_buffer.ptr, out_buffer.len);
33823359 switch (errno(rc)) {
3383 .SUCCESS => return out_buffer[0..@as(usize, @bitCast(rc))],
3360 .SUCCESS => return out_buffer[0..@bitCast(rc)],
33843361 .ACCES => return error.AccessDenied,
33853362 .FAULT => unreachable,
33863363 .INVAL => return error.NotLink,
......@@ -3458,7 +3435,7 @@ pub fn readlinkatZ(dirfd: fd_t, file_path: [*:0]const u8, out_buffer: []u8) Read
34583435 }
34593436 const rc = system.readlinkat(dirfd, file_path, out_buffer.ptr, out_buffer.len);
34603437 switch (errno(rc)) {
3461 .SUCCESS => return out_buffer[0..@as(usize, @bitCast(rc))],
3438 .SUCCESS => return out_buffer[0..@bitCast(rc)],
34623439 .ACCES => return error.AccessDenied,
34633440 .FAULT => unreachable,
34643441 .INVAL => return error.NotLink,
......@@ -3570,7 +3547,7 @@ pub fn isatty(handle: fd_t) bool {
35703547 if (builtin.os.tag == .linux) {
35713548 while (true) {
35723549 var wsz: linux.winsize = undefined;
3573 const fd = @as(usize, @bitCast(@as(isize, handle)));
3550 const fd: usize = @bitCast(@as(isize, handle));
35743551 const rc = linux.syscall3(.ioctl, fd, linux.T.IOCGWINSZ, @intFromPtr(&wsz));
35753552 switch (linux.getErrno(rc)) {
35763553 .SUCCESS => return true,
......@@ -3614,15 +3591,15 @@ pub fn isCygwinPty(handle: fd_t) bool {
36143591 var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = [_]u8{0} ** (name_bytes_offset + num_name_bytes);
36153592
36163593 var io_status_block: windows.IO_STATUS_BLOCK = undefined;
3617 const rc = windows.ntdll.NtQueryInformationFile(handle, &io_status_block, &name_info_bytes, @as(u32, @intCast(name_info_bytes.len)), .FileNameInformation);
3594 const rc = windows.ntdll.NtQueryInformationFile(handle, &io_status_block, &name_info_bytes, @intCast(name_info_bytes.len), .FileNameInformation);
36183595 switch (rc) {
36193596 .SUCCESS => {},
36203597 .INVALID_PARAMETER => unreachable,
36213598 else => return false,
36223599 }
36233600
3624 const name_info = @as(*const windows.FILE_NAME_INFO, @ptrCast(&name_info_bytes[0]));
3625 const name_bytes = name_info_bytes[name_bytes_offset .. name_bytes_offset + @as(usize, name_info.FileNameLength)];
3601 const name_info: *const windows.FILE_NAME_INFO = @ptrCast(&name_info_bytes);
3602 const name_bytes = name_info_bytes[name_bytes_offset .. name_bytes_offset + name_info.FileNameLength];
36263603 const name_wide = mem.bytesAsSlice(u16, name_bytes);
36273604 // Note: The name we get from NtQueryInformationFile will be prefixed with a '\', e.g. \msys-1888ae32e00d56aa-pty0-to-master
36283605 return (mem.startsWith(u16, name_wide, &[_]u16{ '\\', 'm', 's', 'y', 's', '-' }) or
......@@ -3668,9 +3645,9 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t
36683645 else
36693646 0;
36703647 const rc = try windows.WSASocketW(
3671 @as(i32, @bitCast(domain)),
3672 @as(i32, @bitCast(filtered_sock_type)),
3673 @as(i32, @bitCast(protocol)),
3648 @bitCast(domain),
3649 @bitCast(filtered_sock_type),
3650 @bitCast(protocol),
36743651 null,
36753652 0,
36763653 flags,
......@@ -3688,7 +3665,7 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t
36883665 return rc;
36893666 }
36903667
3691 const have_sock_flags = comptime !builtin.target.isDarwin();
3668 const have_sock_flags = !builtin.target.isDarwin();
36923669 const filtered_sock_type = if (!have_sock_flags)
36933670 socket_type & ~@as(u32, SOCK.NONBLOCK | SOCK.CLOEXEC)
36943671 else
......@@ -3696,7 +3673,7 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t
36963673 const rc = system.socket(domain, filtered_sock_type, protocol);
36973674 switch (errno(rc)) {
36983675 .SUCCESS => {
3699 const fd = @as(fd_t, @intCast(rc));
3676 const fd: fd_t = @intCast(rc);
37003677 errdefer close(fd);
37013678 if (!have_sock_flags) {
37023679 try setSockFlags(fd, socket_type);
......@@ -3984,10 +3961,10 @@ pub fn accept(
39843961 /// description of the `CLOEXEC` flag in `open` for reasons why this may be useful.
39853962 flags: u32,
39863963) AcceptError!socket_t {
3987 const have_accept4 = comptime !(builtin.target.isDarwin() or builtin.os.tag == .windows);
3964 const have_accept4 = !(builtin.target.isDarwin() or builtin.os.tag == .windows);
39883965 assert(0 == (flags & ~@as(u32, SOCK.NONBLOCK | SOCK.CLOEXEC))); // Unsupported flag(s)
39893966
3990 const accepted_sock = while (true) {
3967 const accepted_sock: socket_t = while (true) {
39913968 const rc = if (have_accept4)
39923969 system.accept4(sock, addr, addr_size, flags)
39933970 else if (builtin.os.tag == .windows)
......@@ -4014,9 +3991,7 @@ pub fn accept(
40143991 }
40153992 } else {
40163993 switch (errno(rc)) {
4017 .SUCCESS => {
4018 break @as(socket_t, @intCast(rc));
4019 },
3994 .SUCCESS => break @intCast(rc),
40203995 .INTR => continue,
40213996 .AGAIN => return error.WouldBlock,
40223997 .BADF => unreachable, // always a race condition
......@@ -4063,7 +4038,7 @@ pub const EpollCreateError = error{
40634038pub fn epoll_create1(flags: u32) EpollCreateError!i32 {
40644039 const rc = system.epoll_create1(flags);
40654040 switch (errno(rc)) {
4066 .SUCCESS => return @as(i32, @intCast(rc)),
4041 .SUCCESS => return @intCast(rc),
40674042 else => |err| return unexpectedErrno(err),
40684043
40694044 .INVAL => unreachable,
......@@ -4122,9 +4097,9 @@ pub fn epoll_ctl(epfd: i32, op: u32, fd: i32, event: ?*linux.epoll_event) EpollC
41224097pub fn epoll_wait(epfd: i32, events: []linux.epoll_event, timeout: i32) usize {
41234098 while (true) {
41244099 // TODO get rid of the @intCast
4125 const rc = system.epoll_wait(epfd, events.ptr, @as(u32, @intCast(events.len)), timeout);
4100 const rc = system.epoll_wait(epfd, events.ptr, @intCast(events.len), timeout);
41264101 switch (errno(rc)) {
4127 .SUCCESS => return @as(usize, @intCast(rc)),
4102 .SUCCESS => return @intCast(rc),
41284103 .INTR => continue,
41294104 .BADF => unreachable,
41304105 .FAULT => unreachable,
......@@ -4143,7 +4118,7 @@ pub const EventFdError = error{
41434118pub fn eventfd(initval: u32, flags: u32) EventFdError!i32 {
41444119 const rc = system.eventfd(initval, flags);
41454120 switch (errno(rc)) {
4146 .SUCCESS => return @as(i32, @intCast(rc)),
4121 .SUCCESS => return @intCast(rc),
41474122 else => |err| return unexpectedErrno(err),
41484123
41494124 .INVAL => unreachable, // invalid parameters
......@@ -4277,7 +4252,7 @@ pub const ConnectError = error{
42774252/// return error.WouldBlock when EAGAIN or EINPROGRESS is received.
42784253pub fn connect(sock: socket_t, sock_addr: *const sockaddr, len: socklen_t) ConnectError!void {
42794254 if (builtin.os.tag == .windows) {
4280 const rc = windows.ws2_32.connect(sock, sock_addr, @as(i32, @intCast(len)));
4255 const rc = windows.ws2_32.connect(sock, sock_addr, @intCast(len));
42814256 if (rc == 0) return;
42824257 switch (windows.ws2_32.WSAGetLastError()) {
42834258 .WSAEADDRINUSE => return error.AddressInUse,
......@@ -4332,7 +4307,7 @@ pub fn connect(sock: socket_t, sock_addr: *const sockaddr, len: socklen_t) Conne
43324307pub fn getsockoptError(sockfd: fd_t) ConnectError!void {
43334308 var err_code: i32 = undefined;
43344309 var size: u32 = @sizeOf(u32);
4335 const rc = system.getsockopt(sockfd, SOL.SOCKET, SO.ERROR, @as([*]u8, @ptrCast(&err_code)), &size);
4310 const rc = system.getsockopt(sockfd, SOL.SOCKET, SO.ERROR, @ptrCast(&err_code), &size);
43364311 assert(size == 4);
43374312 switch (errno(rc)) {
43384313 .SUCCESS => switch (@as(E, @enumFromInt(err_code))) {
......@@ -4373,15 +4348,13 @@ pub const WaitPidResult = struct {
43734348/// Use this version of the `waitpid` wrapper if you spawned your child process using explicit
43744349/// `fork` and `execve` method.
43754350pub fn waitpid(pid: pid_t, flags: u32) WaitPidResult {
4376 const Status = if (builtin.link_libc) c_int else u32;
4377 var status: Status = undefined;
4378 const coerced_flags = if (builtin.link_libc) @as(c_int, @intCast(flags)) else flags;
4351 var status: if (builtin.link_libc) c_int else u32 = undefined;
43794352 while (true) {
4380 const rc = system.waitpid(pid, &status, coerced_flags);
4353 const rc = system.waitpid(pid, &status, @intCast(flags));
43814354 switch (errno(rc)) {
43824355 .SUCCESS => return .{
4383 .pid = @as(pid_t, @intCast(rc)),
4384 .status = @as(u32, @bitCast(status)),
4356 .pid = @intCast(rc),
4357 .status = @bitCast(status),
43854358 },
43864359 .INTR => continue,
43874360 .CHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error.
......@@ -4392,15 +4365,13 @@ pub fn waitpid(pid: pid_t, flags: u32) WaitPidResult {
43924365}
43934366
43944367pub fn wait4(pid: pid_t, flags: u32, ru: ?*rusage) WaitPidResult {
4395 const Status = if (builtin.link_libc) c_int else u32;
4396 var status: Status = undefined;
4397 const coerced_flags = if (builtin.link_libc) @as(c_int, @intCast(flags)) else flags;
4368 var status: if (builtin.link_libc) c_int else u32 = undefined;
43984369 while (true) {
4399 const rc = system.wait4(pid, &status, coerced_flags, ru);
4370 const rc = system.wait4(pid, &status, @intCast(flags), ru);
44004371 switch (errno(rc)) {
44014372 .SUCCESS => return .{
4402 .pid = @as(pid_t, @intCast(rc)),
4403 .status = @as(u32, @bitCast(status)),
4373 .pid = @intCast(rc),
4374 .status = @bitCast(status),
44044375 },
44054376 .INTR => continue,
44064377 .CHILD => unreachable, // The process specified does not exist. It would be a race condition to handle this error.
......@@ -4428,7 +4399,6 @@ pub fn fstat(fd: fd_t) FStatError!Stat {
44284399 }
44294400
44304401 const fstat_sym = if (lfs64_abi) system.fstat64 else system.fstat;
4431
44324402 var stat = mem.zeroes(Stat);
44334403 switch (errno(fstat_sym(fd, &stat))) {
44344404 .SUCCESS => return stat,
......@@ -4512,7 +4482,6 @@ pub fn fstatatZ(dirfd: fd_t, pathname: [*:0]const u8, flags: u32) FStatAtError!S
45124482 }
45134483
45144484 const fstatat_sym = if (lfs64_abi) system.fstatat64 else system.fstatat;
4515
45164485 var stat = mem.zeroes(Stat);
45174486 switch (errno(fstatat_sym(dirfd, pathname, &stat, flags))) {
45184487 .SUCCESS => return stat,
......@@ -4545,7 +4514,7 @@ pub const KQueueError = error{
45454514pub fn kqueue() KQueueError!i32 {
45464515 const rc = system.kqueue();
45474516 switch (errno(rc)) {
4548 .SUCCESS => return @as(i32, @intCast(rc)),
4517 .SUCCESS => return @intCast(rc),
45494518 .MFILE => return error.ProcessFdQuotaExceeded,
45504519 .NFILE => return error.SystemFdQuotaExceeded,
45514520 else => |err| return unexpectedErrno(err),
......@@ -4586,7 +4555,7 @@ pub fn kevent(
45864555 timeout,
45874556 );
45884557 switch (errno(rc)) {
4589 .SUCCESS => return @as(usize, @intCast(rc)),
4558 .SUCCESS => return @intCast(rc),
45904559 .ACCES => return error.AccessDenied,
45914560 .FAULT => unreachable,
45924561 .BADF => unreachable, // Always a race condition.
......@@ -4610,7 +4579,7 @@ pub const INotifyInitError = error{
46104579pub fn inotify_init1(flags: u32) INotifyInitError!i32 {
46114580 const rc = system.inotify_init1(flags);
46124581 switch (errno(rc)) {
4613 .SUCCESS => return @as(i32, @intCast(rc)),
4582 .SUCCESS => return @intCast(rc),
46144583 .INVAL => unreachable,
46154584 .MFILE => return error.ProcessFdQuotaExceeded,
46164585 .NFILE => return error.SystemFdQuotaExceeded,
......@@ -4639,7 +4608,7 @@ pub fn inotify_add_watch(inotify_fd: i32, pathname: []const u8, mask: u32) INoti
46394608pub fn inotify_add_watchZ(inotify_fd: i32, pathname: [*:0]const u8, mask: u32) INotifyAddWatchError!i32 {
46404609 const rc = system.inotify_add_watch(inotify_fd, pathname, mask);
46414610 switch (errno(rc)) {
4642 .SUCCESS => return @as(i32, @intCast(rc)),
4611 .SUCCESS => return @intCast(rc),
46434612 .ACCES => return error.AccessDenied,
46444613 .BADF => unreachable,
46454614 .FAULT => unreachable,
......@@ -4675,7 +4644,7 @@ pub const FanotifyInitError = error{
46754644pub fn fanotify_init(flags: u32, event_f_flags: u32) FanotifyInitError!i32 {
46764645 const rc = system.fanotify_init(flags, event_f_flags);
46774646 switch (errno(rc)) {
4678 .SUCCESS => return @as(i32, @intCast(rc)),
4647 .SUCCESS => return @intCast(rc),
46794648 .INVAL => unreachable,
46804649 .MFILE => return error.ProcessFdQuotaExceeded,
46814650 .NFILE => return error.SystemFdQuotaExceeded,
......@@ -4780,7 +4749,7 @@ pub const ForkError = error{SystemResources} || UnexpectedError;
47804749pub fn fork() ForkError!pid_t {
47814750 const rc = system.fork();
47824751 switch (errno(rc)) {
4783 .SUCCESS => return @as(pid_t, @intCast(rc)),
4752 .SUCCESS => return @intCast(rc),
47844753 .AGAIN => return error.SystemResources,
47854754 .NOMEM => return error.SystemResources,
47864755 else => |err| return unexpectedErrno(err),
......@@ -4820,12 +4789,10 @@ pub fn mmap(
48204789 offset: u64,
48214790) MMapError![]align(mem.page_size) u8 {
48224791 const mmap_sym = if (lfs64_abi) system.mmap64 else system.mmap;
4823
4824 const ioffset: i64 = @bitCast(offset); // the OS treats this as unsigned
4825 const rc = mmap_sym(ptr, length, prot, @bitCast(flags), fd, ioffset);
4826 const err = if (builtin.link_libc) blk: {
4792 const rc = mmap_sym(ptr, length, prot, @bitCast(flags), fd, @bitCast(offset));
4793 const err: E = if (builtin.link_libc) blk: {
48274794 if (rc != std.c.MAP_FAILED) return @as([*]align(mem.page_size) u8, @ptrCast(@alignCast(rc)))[0..length];
4828 break :blk @as(E, @enumFromInt(system._errno().*));
4795 break :blk @enumFromInt(system._errno().*);
48294796 } else blk: {
48304797 const err = errno(rc);
48314798 if (err == .SUCCESS) return @as([*]align(mem.page_size) u8, @ptrFromInt(rc))[0..length];
......@@ -5250,7 +5217,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
52505217 }
52515218 if (builtin.os.tag == .wasi and !builtin.link_libc) {
52525219 var new_offset: wasi.filesize_t = undefined;
5253 switch (wasi.fd_seek(fd, @as(wasi.filedelta_t, @bitCast(offset)), .SET, &new_offset)) {
5220 switch (wasi.fd_seek(fd, @bitCast(offset), .SET, &new_offset)) {
52545221 .SUCCESS => return,
52555222 .BADF => unreachable, // always a race condition
52565223 .INVAL => return error.Unseekable,
......@@ -5263,9 +5230,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
52635230 }
52645231
52655232 const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek;
5266
5267 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
5268 switch (errno(lseek_sym(fd, ioffset, SEEK.SET))) {
5233 switch (errno(lseek_sym(fd, @bitCast(offset), SEEK.SET))) {
52695234 .SUCCESS => return,
52705235 .BADF => unreachable, // always a race condition
52715236 .INVAL => return error.Unseekable,
......@@ -5280,7 +5245,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
52805245pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
52815246 if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
52825247 var result: u64 = undefined;
5283 switch (errno(system.llseek(fd, @as(u64, @bitCast(offset)), &result, SEEK.CUR))) {
5248 switch (errno(system.llseek(fd, @bitCast(offset), &result, SEEK.CUR))) {
52845249 .SUCCESS => return,
52855250 .BADF => unreachable, // always a race condition
52865251 .INVAL => return error.Unseekable,
......@@ -5307,9 +5272,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
53075272 }
53085273 }
53095274 const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek;
5310
5311 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
5312 switch (errno(lseek_sym(fd, ioffset, SEEK.CUR))) {
5275 switch (errno(lseek_sym(fd, @bitCast(offset), SEEK.CUR))) {
53135276 .SUCCESS => return,
53145277 .BADF => unreachable, // always a race condition
53155278 .INVAL => return error.Unseekable,
......@@ -5324,7 +5287,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
53245287pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
53255288 if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
53265289 var result: u64 = undefined;
5327 switch (errno(system.llseek(fd, @as(u64, @bitCast(offset)), &result, SEEK.END))) {
5290 switch (errno(system.llseek(fd, @bitCast(offset), &result, SEEK.END))) {
53285291 .SUCCESS => return,
53295292 .BADF => unreachable, // always a race condition
53305293 .INVAL => return error.Unseekable,
......@@ -5351,9 +5314,7 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
53515314 }
53525315 }
53535316 const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek;
5354
5355 const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
5356 switch (errno(lseek_sym(fd, ioffset, SEEK.END))) {
5317 switch (errno(lseek_sym(fd, @bitCast(offset), SEEK.END))) {
53575318 .SUCCESS => return,
53585319 .BADF => unreachable, // always a race condition
53595320 .INVAL => return error.Unseekable,
......@@ -5395,10 +5356,9 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
53955356 }
53965357 }
53975358 const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek;
5398
53995359 const rc = lseek_sym(fd, 0, SEEK.CUR);
54005360 switch (errno(rc)) {
5401 .SUCCESS => return @as(u64, @bitCast(rc)),
5361 .SUCCESS => return @bitCast(rc),
54025362 .BADF => unreachable, // always a race condition
54035363 .INVAL => return error.Unseekable,
54045364 .OVERFLOW => return error.Unseekable,
......@@ -5421,7 +5381,7 @@ pub fn fcntl(fd: fd_t, cmd: i32, arg: usize) FcntlError!usize {
54215381 while (true) {
54225382 const rc = system.fcntl(fd, cmd, arg);
54235383 switch (errno(rc)) {
5424 .SUCCESS => return @as(usize, @intCast(rc)),
5384 .SUCCESS => return @intCast(rc),
54255385 .INTR => continue,
54265386 .AGAIN, .ACCES => return error.Locked,
54275387 .BADF => unreachable,
......@@ -5714,7 +5674,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
57145674 // errno values to expect when command is F.GETPATH...
57155675 else => |err| return unexpectedErrno(err),
57165676 }
5717 const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES;
5677 const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse MAX_PATH_BYTES;
57185678 return out_buffer[0..len];
57195679 },
57205680 .linux => {
......@@ -5746,7 +5706,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
57465706 return target;
57475707 },
57485708 .freebsd => {
5749 if (comptime builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0, .patch = 0 }) == .gt) {
5709 if (comptime builtin.os.isAtLeast(.freebsd, .{ .major = 13, .minor = 0, .patch = 0 }) orelse false) {
57505710 var kfile: system.kinfo_file = undefined;
57515711 kfile.structsize = system.KINFO_FILE_SIZE;
57525712 switch (errno(system.fcntl(fd, system.F.KINFO, @intFromPtr(&kfile)))) {
......@@ -5785,7 +5745,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
57855745 };
57865746 var i: usize = 0;
57875747 while (i < len) {
5788 const kf: *align(1) system.kinfo_file = @as(*align(1) system.kinfo_file, @ptrCast(&buf[i]));
5748 const kf: *align(1) system.kinfo_file = @ptrCast(&buf[i]);
57895749 if (kf.fd == fd) {
57905750 len = mem.indexOfScalar(u8, &kf.path, 0) orelse MAX_PATH_BYTES;
57915751 if (len == 0) return error.NameTooLong;
......@@ -5793,7 +5753,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
57935753 @memcpy(result, kf.path[0..len]);
57945754 return result;
57955755 }
5796 i += @as(usize, @intCast(kf.structsize));
5756 i += @intCast(kf.structsize);
57975757 }
57985758 return error.FileNotFound;
57995759 }
......@@ -5806,7 +5766,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
58065766 .RANGE => return error.NameTooLong,
58075767 else => |err| return unexpectedErrno(err),
58085768 }
5809 const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES;
5769 const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse MAX_PATH_BYTES;
58105770 return out_buffer[0..len];
58115771 },
58125772 .netbsd => {
......@@ -5820,7 +5780,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
58205780 .RANGE => return error.NameTooLong,
58215781 else => |err| return unexpectedErrno(err),
58225782 }
5823 const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES;
5783 const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse MAX_PATH_BYTES;
58245784 return out_buffer[0..len];
58255785 },
58265786 else => unreachable, // made unreachable by isGetFdPathSupportedOnTarget above
......@@ -5871,14 +5831,14 @@ pub fn dl_iterate_phdr(
58715831 callback(info, size, context_ptr.*) catch |err| return @intFromError(err);
58725832 return 0;
58735833 }
5874 }.callbackC, @as(?*anyopaque, @ptrFromInt(@intFromPtr(&context))))) {
5834 }.callbackC, @ptrCast(@constCast(&context)))) {
58755835 0 => return,
58765836 else => |err| return @as(Error, @errorCast(@errorFromInt(@as(std.meta.Int(.unsigned, @bitSizeOf(anyerror)), @intCast(err))))),
58775837 }
58785838 }
58795839
58805840 const elf_base = std.process.getBaseAddress();
5881 const ehdr = @as(*elf.Ehdr, @ptrFromInt(elf_base));
5841 const ehdr: *elf.Ehdr = @ptrFromInt(elf_base);
58825842 // Make sure the base address points to an ELF image.
58835843 assert(mem.eql(u8, ehdr.e_ident[0..4], elf.MAGIC));
58845844 const n_phdr = ehdr.e_phnum;
......@@ -5916,12 +5876,12 @@ pub fn dl_iterate_phdr(
59165876 var dlpi_phnum: u16 = undefined;
59175877
59185878 if (entry.l_addr != 0) {
5919 const elf_header = @as(*elf.Ehdr, @ptrFromInt(entry.l_addr));
5920 dlpi_phdr = @as([*]elf.Phdr, @ptrFromInt(entry.l_addr + elf_header.e_phoff));
5879 const elf_header: *elf.Ehdr = @ptrFromInt(entry.l_addr);
5880 dlpi_phdr = @ptrFromInt(entry.l_addr + elf_header.e_phoff);
59215881 dlpi_phnum = elf_header.e_phnum;
59225882 } else {
59235883 // This is the running ELF image
5924 dlpi_phdr = @as([*]elf.Phdr, @ptrFromInt(elf_base + ehdr.e_phoff));
5884 dlpi_phdr = @ptrFromInt(elf_base + ehdr.e_phoff);
59255885 dlpi_phnum = ehdr.e_phnum;
59265886 }
59275887
......@@ -5943,11 +5903,11 @@ pub const ClockGetTimeError = error{UnsupportedClock} || UnexpectedError;
59435903pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
59445904 if (builtin.os.tag == .wasi and !builtin.link_libc) {
59455905 var ts: timestamp_t = undefined;
5946 switch (system.clock_time_get(@as(u32, @bitCast(clk_id)), 1, &ts)) {
5906 switch (system.clock_time_get(@bitCast(clk_id), 1, &ts)) {
59475907 .SUCCESS => {
59485908 tp.* = .{
5949 .tv_sec = @as(i64, @intCast(ts / std.time.ns_per_s)),
5950 .tv_nsec = @as(isize, @intCast(ts % std.time.ns_per_s)),
5909 .tv_sec = @intCast(ts / std.time.ns_per_s),
5910 .tv_nsec = @intCast(ts % std.time.ns_per_s),
59515911 };
59525912 },
59535913 .INVAL => return error.UnsupportedClock,
......@@ -5984,10 +5944,10 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
59845944pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void {
59855945 if (builtin.os.tag == .wasi and !builtin.link_libc) {
59865946 var ts: timestamp_t = undefined;
5987 switch (system.clock_res_get(@as(u32, @bitCast(clk_id)), &ts)) {
5947 switch (system.clock_res_get(@bitCast(clk_id), &ts)) {
59885948 .SUCCESS => res.* = .{
5989 .tv_sec = @as(i64, @intCast(ts / std.time.ns_per_s)),
5990 .tv_nsec = @as(isize, @intCast(ts % std.time.ns_per_s)),
5949 .tv_sec = @intCast(ts / std.time.ns_per_s),
5950 .tv_nsec = @intCast(ts % std.time.ns_per_s),
59915951 },
59925952 .INVAL => return error.UnsupportedClock,
59935953 else => |err| return unexpectedErrno(err),
......@@ -6212,7 +6172,7 @@ pub fn res_mkquery(
62126172 // TODO determine the circumstances for this and whether or
62136173 // not this should be an error.
62146174 if (j - i - 1 > 62) unreachable;
6215 q[i - 1] = @as(u8, @intCast(j - i));
6175 q[i - 1] = @intCast(j - i);
62166176 }
62176177 q[i + 1] = ty;
62186178 q[i + 3] = class;
......@@ -6221,10 +6181,10 @@ pub fn res_mkquery(
62216181 var ts: timespec = undefined;
62226182 clock_gettime(CLOCK.REALTIME, &ts) catch {};
62236183 const UInt = std.meta.Int(.unsigned, @bitSizeOf(@TypeOf(ts.tv_nsec)));
6224 const unsec = @as(UInt, @bitCast(ts.tv_nsec));
6225 const id = @as(u32, @truncate(unsec + unsec / 65536));
6226 q[0] = @as(u8, @truncate(id / 256));
6227 q[1] = @as(u8, @truncate(id));
6184 const unsec: UInt = @bitCast(ts.tv_nsec);
6185 const id: u32 = @truncate(unsec + unsec / 65536);
6186 q[0] = @truncate(id / 256);
6187 q[1] = @truncate(id);
62286188
62296189 @memcpy(buf[0..n], q[0..n]);
62306190 return n;
......@@ -6330,11 +6290,11 @@ pub fn sendmsg(
63306290 else => |err| return windows.unexpectedWSAError(err),
63316291 }
63326292 } else {
6333 return @as(usize, @intCast(rc));
6293 return @intCast(rc);
63346294 }
63356295 } else {
63366296 switch (errno(rc)) {
6337 .SUCCESS => return @as(usize, @intCast(rc)),
6297 .SUCCESS => return @intCast(rc),
63386298
63396299 .ACCES => return error.AccessDenied,
63406300 .AGAIN => return error.WouldBlock,
......@@ -6430,13 +6390,13 @@ pub fn sendto(
64306390 .WSANOTINITIALISED => unreachable, // A successful WSAStartup call must occur before using this function.
64316391 else => |err| return windows.unexpectedWSAError(err),
64326392 },
6433 else => |rc| return @as(usize, @intCast(rc)),
6393 else => |rc| return @intCast(rc),
64346394 }
64356395 }
64366396 while (true) {
64376397 const rc = system.sendto(sockfd, buf.ptr, buf.len, flags, dest_addr, addrlen);
64386398 switch (errno(rc)) {
6439 .SUCCESS => return @as(usize, @intCast(rc)),
6399 .SUCCESS => return @intCast(rc),
64406400
64416401 .ACCES => return error.AccessDenied,
64426402 .AGAIN => return error.WouldBlock,
......@@ -6588,18 +6548,15 @@ pub fn sendfile(
65886548 }
65896549
65906550 // Here we match BSD behavior, making a zero count value send as many bytes as possible.
6591 const adjusted_count_tmp = if (in_len == 0) max_count else @min(in_len, @as(size_t, max_count));
6592 // TODO we should not need this cast; improve return type of @min
6593 const adjusted_count = @as(usize, @intCast(adjusted_count_tmp));
6551 const adjusted_count = if (in_len == 0) max_count else @min(in_len, max_count);
65946552
65956553 const sendfile_sym = if (lfs64_abi) system.sendfile64 else system.sendfile;
6596
65976554 while (true) {
6598 var offset: off_t = @as(off_t, @bitCast(in_offset));
6555 var offset: off_t = @bitCast(in_offset);
65996556 const rc = sendfile_sym(out_fd, in_fd, &offset, adjusted_count);
66006557 switch (errno(rc)) {
66016558 .SUCCESS => {
6602 const amt = @as(usize, @bitCast(rc));
6559 const amt: usize = @bitCast(rc);
66036560 total_written += amt;
66046561 if (in_len == 0 and amt == 0) {
66056562 // We have detected EOF from `in_fd`.
......@@ -6665,13 +6622,10 @@ pub fn sendfile(
66656622 hdtr = &hdtr_data;
66666623 }
66676624
6668 const adjusted_count = @min(in_len, max_count);
6669
66706625 while (true) {
66716626 var sbytes: off_t = undefined;
6672 const offset = @as(off_t, @bitCast(in_offset));
6673 const err = errno(system.sendfile(in_fd, out_fd, offset, adjusted_count, hdtr, &sbytes, flags));
6674 const amt = @as(usize, @bitCast(sbytes));
6627 const err = errno(system.sendfile(in_fd, out_fd, @bitCast(in_offset), @min(in_len, max_count), hdtr, &sbytes, flags));
6628 const amt: usize = @bitCast(sbytes);
66756629 switch (err) {
66766630 .SUCCESS => return amt,
66776631
......@@ -6738,15 +6692,10 @@ pub fn sendfile(
67386692 hdtr = &hdtr_data;
67396693 }
67406694
6741 const adjusted_count_temporary = @min(in_len, @as(u63, max_count));
6742 // TODO we should not need this int cast; improve the return type of `@min`
6743 const adjusted_count = @as(u63, @intCast(adjusted_count_temporary));
6744
67456695 while (true) {
6746 var sbytes: off_t = adjusted_count;
6747 const signed_offset = @as(i64, @bitCast(in_offset));
6748 const err = errno(system.sendfile(in_fd, out_fd, signed_offset, &sbytes, hdtr, flags));
6749 const amt = @as(usize, @bitCast(sbytes));
6696 var sbytes: off_t = @min(in_len, max_count);
6697 const err = errno(system.sendfile(in_fd, out_fd, @bitCast(in_offset), &sbytes, hdtr, flags));
6698 const amt: usize = @bitCast(sbytes);
67506699 switch (err) {
67516700 .SUCCESS => return amt,
67526701
......@@ -6791,9 +6740,7 @@ pub fn sendfile(
67916740 rw: {
67926741 var buf: [8 * 4096]u8 = undefined;
67936742 // Here we match BSD behavior, making a zero count value send as many bytes as possible.
6794 const adjusted_count_tmp = if (in_len == 0) buf.len else @min(buf.len, in_len);
6795 // TODO we should not need this cast; improve return type of @min
6796 const adjusted_count = @as(usize, @intCast(adjusted_count_tmp));
6743 const adjusted_count = if (in_len == 0) buf.len else @min(buf.len, in_len);
67976744 const amt_read = try pread(in_fd, buf[0..adjusted_count], in_offset);
67986745 if (amt_read == 0) {
67996746 if (in_len == 0) {
......@@ -6864,14 +6811,14 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
68646811 std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 })) and
68656812 has_copy_file_range_syscall.load(.Monotonic)))
68666813 {
6867 var off_in_copy = @as(i64, @bitCast(off_in));
6868 var off_out_copy = @as(i64, @bitCast(off_out));
6814 var off_in_copy: i64 = @bitCast(off_in);
6815 var off_out_copy: i64 = @bitCast(off_out);
68696816
68706817 while (true) {
68716818 const rc = system.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags);
68726819 if (builtin.os.tag == .freebsd) {
68736820 switch (system.getErrno(rc)) {
6874 .SUCCESS => return @as(usize, @intCast(rc)),
6821 .SUCCESS => return @intCast(rc),
68756822 .BADF => return error.FilesOpenedWithWrongFlags,
68766823 .FBIG => return error.FileTooBig,
68776824 .IO => return error.InputOutput,
......@@ -6884,7 +6831,7 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
68846831 }
68856832 } else { // assume linux
68866833 switch (system.getErrno(rc)) {
6887 .SUCCESS => return @as(usize, @intCast(rc)),
6834 .SUCCESS => return @intCast(rc),
68886835 .BADF => return error.FilesOpenedWithWrongFlags,
68896836 .FBIG => return error.FileTooBig,
68906837 .IO => return error.InputOutput,
......@@ -6907,11 +6854,8 @@ pub fn copy_file_range(fd_in: fd_t, off_in: u64, fd_out: fd_t, off_out: u64, len
69076854 }
69086855
69096856 var buf: [8 * 4096]u8 = undefined;
6910 const adjusted_count = @min(buf.len, len);
6911 const amt_read = try pread(fd_in, buf[0..adjusted_count], off_in);
6912 // TODO without @as the line below fails to compile for wasm32-wasi:
6913 // error: integer value 0 cannot be coerced to type 'os.PWriteError!usize'
6914 if (amt_read == 0) return @as(usize, 0);
6857 const amt_read = try pread(fd_in, buf[0..@min(buf.len, len)], off_in);
6858 if (amt_read == 0) return 0;
69156859 return pwrite(fd_out, buf[0..amt_read], off_out);
69166860}
69176861
......@@ -6937,11 +6881,11 @@ pub fn poll(fds: []pollfd, timeout: i32) PollError!usize {
69376881 else => |err| return windows.unexpectedWSAError(err),
69386882 }
69396883 } else {
6940 return @as(usize, @intCast(rc));
6884 return @intCast(rc);
69416885 }
69426886 } else {
69436887 switch (errno(rc)) {
6944 .SUCCESS => return @as(usize, @intCast(rc)),
6888 .SUCCESS => return @intCast(rc),
69456889 .FAULT => unreachable,
69466890 .INTR => continue,
69476891 .INVAL => unreachable,
......@@ -6971,7 +6915,7 @@ pub fn ppoll(fds: []pollfd, timeout: ?*const timespec, mask: ?*const sigset_t) P
69716915 const fds_count = math.cast(nfds_t, fds.len) orelse return error.SystemResources;
69726916 const rc = system.ppoll(fds.ptr, fds_count, ts_ptr, mask);
69736917 switch (errno(rc)) {
6974 .SUCCESS => return @as(usize, @intCast(rc)),
6918 .SUCCESS => return @intCast(rc),
69756919 .FAULT => unreachable,
69766920 .INTR => return error.SignalInterrupt,
69776921 .INVAL => unreachable,
......@@ -7038,11 +6982,11 @@ pub fn recvfrom(
70386982 else => |err| return windows.unexpectedWSAError(err),
70396983 }
70406984 } else {
7041 return @as(usize, @intCast(rc));
6985 return @intCast(rc);
70426986 }
70436987 } else {
70446988 switch (errno(rc)) {
7045 .SUCCESS => return @as(usize, @intCast(rc)),
6989 .SUCCESS => return @intCast(rc),
70466990 .BADF => unreachable, // always a race condition
70476991 .FAULT => unreachable,
70486992 .INVAL => unreachable,
......@@ -7081,7 +7025,7 @@ pub fn dn_expand(
70817025 // loop invariants: p<end, dest<dend
70827026 if ((p[0] & 0xc0) != 0) {
70837027 if (p + 1 == end) return error.InvalidDnsPacket;
7084 const j = ((p[0] & @as(usize, 0x3f)) << 8) | p[1];
7028 const j = @as(usize, p[0] & 0x3f) << 8 | p[1];
70857029 if (len == std.math.maxInt(usize)) len = @intFromPtr(p) + 2 - @intFromPtr(comp_dn.ptr);
70867030 if (j >= msg.len) return error.InvalidDnsPacket;
70877031 p = msg.ptr + j;
......@@ -7135,7 +7079,7 @@ pub const SetSockOptError = error{
71357079/// Set a socket's options.
71367080pub fn setsockopt(fd: socket_t, level: u32, optname: u32, opt: []const u8) SetSockOptError!void {
71377081 if (builtin.os.tag == .windows) {
7138 const rc = windows.ws2_32.setsockopt(fd, @as(i32, @intCast(level)), @as(i32, @intCast(optname)), opt.ptr, @as(i32, @intCast(opt.len)));
7082 const rc = windows.ws2_32.setsockopt(fd, @intCast(level), @intCast(optname), opt.ptr, @intCast(opt.len));
71397083 if (rc == windows.ws2_32.SOCKET_ERROR) {
71407084 switch (windows.ws2_32.WSAGetLastError()) {
71417085 .WSANOTINITIALISED => unreachable,
......@@ -7148,7 +7092,7 @@ pub fn setsockopt(fd: socket_t, level: u32, optname: u32, opt: []const u8) SetSo
71487092 }
71497093 return;
71507094 } else {
7151 switch (errno(system.setsockopt(fd, level, optname, opt.ptr, @as(socklen_t, @intCast(opt.len))))) {
7095 switch (errno(system.setsockopt(fd, level, optname, opt.ptr, @intCast(opt.len)))) {
71527096 .SUCCESS => {},
71537097 .BADF => unreachable, // always a race condition
71547098 .NOTSOCK => unreachable, // always a race condition
......@@ -7185,7 +7129,7 @@ pub fn memfd_createZ(name: [*:0]const u8, flags: u32) MemFdCreateError!fd_t {
71857129 const getErrno = if (use_c) std.c.getErrno else linux.getErrno;
71867130 const rc = sys.memfd_create(name, flags);
71877131 switch (getErrno(rc)) {
7188 .SUCCESS => return @as(fd_t, @intCast(rc)),
7132 .SUCCESS => return @intCast(rc),
71897133 .FAULT => unreachable, // name has invalid memory
71907134 .INVAL => unreachable, // name/flags are faulty
71917135 .NFILE => return error.SystemFdQuotaExceeded,
......@@ -7335,7 +7279,7 @@ pub fn ioctl_SIOCGIFINDEX(fd: fd_t, ifr: *ifreq) IoCtl_SIOCGIFINDEX_Error!void {
73357279pub fn signalfd(fd: fd_t, mask: *const sigset_t, flags: u32) !fd_t {
73367280 const rc = system.signalfd(fd, mask, flags);
73377281 switch (errno(rc)) {
7338 .SUCCESS => return @as(fd_t, @intCast(rc)),
7282 .SUCCESS => return @intCast(rc),
73397283 .BADF, .INVAL => unreachable,
73407284 .NFILE => return error.SystemFdQuotaExceeded,
73417285 .NOMEM => return error.SystemResources,
......@@ -7443,7 +7387,7 @@ pub fn prctl(option: PR, args: anytype) PrctlError!u31 {
74437387
74447388 const rc = system.prctl(@intFromEnum(option), buf[0], buf[1], buf[2], buf[3]);
74457389 switch (errno(rc)) {
7446 .SUCCESS => return @as(u31, @intCast(rc)),
7390 .SUCCESS => return @intCast(rc),
74477391 .ACCES => return error.AccessDenied,
74487392 .BADF => return error.InvalidFileDescriptor,
74497393 .FAULT => return error.InvalidAddress,
......@@ -7624,7 +7568,7 @@ pub fn perf_event_open(
76247568) PerfEventOpenError!fd_t {
76257569 const rc = linux.perf_event_open(attr, pid, cpu, group_fd, flags);
76267570 switch (errno(rc)) {
7627 .SUCCESS => return @as(fd_t, @intCast(rc)),
7571 .SUCCESS => return @intCast(rc),
76287572 .@"2BIG" => return error.TooBig,
76297573 .ACCES => return error.PermissionDenied,
76307574 .BADF => unreachable, // group_fd file descriptor is not valid.
......@@ -7659,7 +7603,7 @@ pub const TimerFdSetError = TimerFdGetError || error{Canceled};
76597603pub fn timerfd_create(clokid: i32, flags: linux.TFD) TimerFdCreateError!fd_t {
76607604 const rc = linux.timerfd_create(clokid, flags);
76617605 return switch (errno(rc)) {
7662 .SUCCESS => @as(fd_t, @intCast(rc)),
7606 .SUCCESS => @intCast(rc),
76637607 .INVAL => unreachable,
76647608 .MFILE => return error.ProcessFdQuotaExceeded,
76657609 .NFILE => return error.SystemFdQuotaExceeded,
......@@ -7702,7 +7646,6 @@ pub fn timerfd_gettime(fd: i32) TimerFdGetError!linux.itimerspec {
77027646pub const PtraceError = error{
77037647 DeviceBusy,
77047648 InputOutput,
7705 Overflow,
77067649 ProcessNotFound,
77077650 PermissionDenied,
77087651} || UnexpectedError;
......@@ -7724,10 +7667,10 @@ pub fn ptrace(request: u32, pid: pid_t, addr: usize, signal: usize) PtraceError!
77247667 },
77257668
77267669 .macos, .ios, .tvos, .watchos => switch (errno(darwin.ptrace(
7727 math.cast(i32, request) orelse return error.Overflow,
7670 @intCast(request),
77287671 pid,
7729 @as(?[*]u8, @ptrFromInt(addr)),
7730 math.cast(i32, signal) orelse return error.Overflow,
7672 @ptrFromInt(addr),
7673 @intCast(signal),
77317674 ))) {
77327675 .SUCCESS => {},
77337676 .SRCH => error.ProcessNotFound,