Commit f446d8e8f9

Jacob Young <jacobly0@users.noreply.github.com>
2024-02-28 03:42:21
posix: `@as` and other general cleanup
1 parent 17d0bb5
Changed files (2)
lib/std/os.zig
@@ -302,7 +302,7 @@ pub fn close(fd: fd_t) void {
         _ = wasi.fd_close(fd);
         return;
     }
-    if (comptime builtin.target.isDarwin()) {
+    if (builtin.target.isDarwin()) {
         // This avoids the EINTR problem.
         switch (darwin.getErrno(darwin.@"close$NOCANCEL"(fd))) {
             .BADF => unreachable, // Always a race condition.
@@ -476,7 +476,7 @@ fn fchmodat2(dirfd: fd_t, path: []const u8, mode: mode_t, flags: u32) FChmodAtEr
         const rc = system.openat(dirfd, &path_c, .{ .PATH = true, .NOFOLLOW = true, .CLOEXEC = true }, @as(mode_t, 0));
         switch (system.getErrno(rc)) {
             .SUCCESS => {
-                pathfd = @as(fd_t, @intCast(rc));
+                pathfd = @intCast(rc);
                 break;
             },
             .INTR => continue,
@@ -550,7 +550,7 @@ pub fn fchown(fd: fd_t, owner: ?uid_t, group: ?gid_t) FChownError!void {
     }
 
     while (true) {
-        const res = system.fchown(fd, owner orelse @as(u32, 0) -% 1, group orelse @as(u32, 0) -% 1);
+        const res = system.fchown(fd, owner orelse ~@as(uid_t, 0), group orelse ~@as(gid_t, 0));
 
         switch (system.getErrno(res)) {
             .SUCCESS => return,
@@ -596,7 +596,7 @@ pub fn reboot(cmd: RebootCommand) RebootError!void {
             switch (system.getErrno(linux.reboot(
                 .MAGIC1,
                 .MAGIC2,
-                @as(linux.LINUX_REBOOT.CMD, cmd),
+                cmd,
                 switch (cmd) {
                     .RESTART2 => |s| s,
                     else => null,
@@ -639,27 +639,27 @@ pub fn getrandom(buffer: []u8) GetRandomError!void {
             std.c.versionCheck(std.SemanticVersion{ .major = 2, .minor = 25, .patch = 0 });
 
         while (buf.len != 0) {
-            const res = if (use_c) blk: {
+            const num_read: usize, const err = if (use_c) res: {
                 const rc = std.c.getrandom(buf.ptr, buf.len, 0);
-                break :blk .{
-                    .num_read = @as(usize, @bitCast(rc)),
-                    .err = std.c.getErrno(rc),
+                break :res .{
+                    @bitCast(rc),
+                    std.c.getErrno(rc),
                 };
-            } else blk: {
+            } else res: {
                 const rc = linux.getrandom(buf.ptr, buf.len, 0);
-                break :blk .{
-                    .num_read = rc,
-                    .err = linux.getErrno(rc),
+                break :res .{
+                    rc,
+                    linux.getErrno(rc),
                 };
             };
 
-            switch (res.err) {
-                .SUCCESS => buf = buf[res.num_read..],
+            switch (err) {
+                .SUCCESS => buf = buf[num_read..],
                 .INVAL => unreachable,
                 .FAULT => unreachable,
                 .INTR => continue,
                 .NOSYS => return getRandomBytesDevURandom(buf),
-                else => return unexpectedErrno(res.err),
+                else => return unexpectedErrno(err),
             }
         }
         return;
@@ -818,10 +818,10 @@ pub fn exit(status: u8) noreturn {
         // exit() is only available if exitBootServices() has not been called yet.
         // This call to exit should not fail, so we don't care about its return value.
         if (uefi.system_table.boot_services) |bs| {
-            _ = bs.exit(uefi.handle, @as(uefi.Status, @enumFromInt(status)), 0, null);
+            _ = bs.exit(uefi.handle, @enumFromInt(status), 0, null);
         }
         // If we can't exit, reboot the system instead.
-        uefi.system_table.runtime_services.resetSystem(uefi.tables.ResetType.ResetCold, @as(uefi.Status, @enumFromInt(status)), 0, null);
+        uefi.system_table.runtime_services.resetSystem(.ResetCold, @enumFromInt(status), 0, null);
     }
     system.exit(status);
 }
@@ -893,12 +893,10 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
         .macos, .ios, .watchos, .tvos => math.maxInt(i32),
         else => math.maxInt(isize),
     };
-    const adjusted_len = @min(max_count, buf.len);
-
     while (true) {
-        const rc = system.read(fd, buf.ptr, adjusted_len);
+        const rc = system.read(fd, buf.ptr, @min(buf.len, max_count));
         switch (errno(rc)) {
-            .SUCCESS => return @as(usize, @intCast(rc)),
+            .SUCCESS => return @intCast(rc),
             .INTR => continue,
             .INVAL => unreachable,
             .FAULT => unreachable,
@@ -932,7 +930,7 @@ pub fn read(fd: fd_t, buf: []u8) ReadError!usize {
 pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
     if (builtin.os.tag == .windows) {
         // TODO improve this to use ReadFileScatter
-        if (iov.len == 0) return @as(usize, 0);
+        if (iov.len == 0) return 0;
         const first = iov[0];
         return read(fd, first.iov_base[0..first.iov_len]);
     }
@@ -956,12 +954,11 @@ pub fn readv(fd: fd_t, iov: []const iovec) ReadError!usize {
             else => |err| return unexpectedErrno(err),
         }
     }
-    const iov_count = math.cast(u31, iov.len) orelse math.maxInt(u31);
+
     while (true) {
-        // TODO handle the case when iov_len is too large and get rid of this @intCast
-        const rc = system.readv(fd, iov.ptr, iov_count);
+        const rc = system.readv(fd, iov.ptr, @min(iov.len, IOV_MAX));
         switch (errno(rc)) {
-            .SUCCESS => return @as(usize, @intCast(rc)),
+            .SUCCESS => return @intCast(rc),
             .INTR => continue,
             .INVAL => unreachable,
             .FAULT => unreachable,
@@ -1035,15 +1032,12 @@ pub fn pread(fd: fd_t, buf: []u8, offset: u64) PReadError!usize {
         .macos, .ios, .watchos, .tvos => math.maxInt(i32),
         else => math.maxInt(isize),
     };
-    const adjusted_len = @min(max_count, buf.len);
 
     const pread_sym = if (lfs64_abi) system.pread64 else system.pread;
-
-    const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
     while (true) {
-        const rc = pread_sym(fd, buf.ptr, adjusted_len, ioffset);
+        const rc = pread_sym(fd, buf.ptr, @min(buf.len, max_count), @bitCast(offset));
         switch (errno(rc)) {
-            .SUCCESS => return @as(usize, @intCast(rc)),
+            .SUCCESS => return @intCast(rc),
             .INTR => continue,
             .INVAL => unreachable,
             .FAULT => unreachable,
@@ -1078,7 +1072,7 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void {
     if (builtin.os.tag == .windows) {
         var io_status_block: windows.IO_STATUS_BLOCK = undefined;
         var eof_info = windows.FILE_END_OF_FILE_INFORMATION{
-            .EndOfFile = @as(windows.LARGE_INTEGER, @bitCast(length)),
+            .EndOfFile = @bitCast(length),
         };
 
         const rc = windows.ntdll.NtSetInformationFile(
@@ -1111,11 +1105,9 @@ pub fn ftruncate(fd: fd_t, length: u64) TruncateError!void {
         }
     }
 
+    const ftruncate_sym = if (lfs64_abi) system.ftruncate64 else system.ftruncate;
     while (true) {
-        const ftruncate_sym = if (lfs64_abi) system.ftruncate64 else system.ftruncate;
-
-        const ilen = @as(i64, @bitCast(length)); // the OS treats this as unsigned
-        switch (errno(ftruncate_sym(fd, ilen))) {
+        switch (errno(ftruncate_sym(fd, @bitCast(length)))) {
             .SUCCESS => return,
             .INTR => continue,
             .FBIG => return error.FileTooBig,
@@ -1178,15 +1170,11 @@ pub fn preadv(fd: fd_t, iov: []const iovec, offset: u64) PReadError!usize {
         }
     }
 
-    const iov_count = math.cast(u31, iov.len) orelse math.maxInt(u31);
-
     const preadv_sym = if (lfs64_abi) system.preadv64 else system.preadv;
-
-    const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
     while (true) {
-        const rc = preadv_sym(fd, iov.ptr, iov_count, ioffset);
+        const rc = preadv_sym(fd, iov.ptr, @min(iov.len, IOV_MAX), @bitCast(offset));
         switch (errno(rc)) {
-            .SUCCESS => return @as(usize, @bitCast(rc)),
+            .SUCCESS => return @bitCast(rc),
             .INTR => continue,
             .INVAL => unreachable,
             .FAULT => unreachable,
@@ -1293,12 +1281,10 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
         .macos, .ios, .watchos, .tvos => math.maxInt(i32),
         else => math.maxInt(isize),
     };
-    const adjusted_len = @min(max_count, bytes.len);
-
     while (true) {
-        const rc = system.write(fd, bytes.ptr, adjusted_len);
+        const rc = system.write(fd, bytes.ptr, @min(bytes.len, max_count));
         switch (errno(rc)) {
-            .SUCCESS => return @as(usize, @intCast(rc)),
+            .SUCCESS => return @intCast(rc),
             .INTR => continue,
             .INVAL => return error.InvalidArgument,
             .FAULT => unreachable,
@@ -1342,7 +1328,7 @@ pub fn write(fd: fd_t, bytes: []const u8) WriteError!usize {
 pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
     if (builtin.os.tag == .windows) {
         // TODO improve this to use WriteFileScatter
-        if (iov.len == 0) return @as(usize, 0);
+        if (iov.len == 0) return 0;
         const first = iov[0];
         return write(fd, first.iov_base[0..first.iov_len]);
     }
@@ -1367,11 +1353,10 @@ pub fn writev(fd: fd_t, iov: []const iovec_const) WriteError!usize {
         }
     }
 
-    const iov_count = if (iov.len > IOV_MAX) IOV_MAX else @as(u31, @intCast(iov.len));
     while (true) {
-        const rc = system.writev(fd, iov.ptr, iov_count);
+        const rc = system.writev(fd, iov.ptr, @min(iov.len, IOV_MAX));
         switch (errno(rc)) {
-            .SUCCESS => return @as(usize, @intCast(rc)),
+            .SUCCESS => return @intCast(rc),
             .INTR => continue,
             .INVAL => return error.InvalidArgument,
             .FAULT => unreachable,
@@ -1455,15 +1440,12 @@ pub fn pwrite(fd: fd_t, bytes: []const u8, offset: u64) PWriteError!usize {
         .macos, .ios, .watchos, .tvos => math.maxInt(i32),
         else => math.maxInt(isize),
     };
-    const adjusted_len = @min(max_count, bytes.len);
 
     const pwrite_sym = if (lfs64_abi) system.pwrite64 else system.pwrite;
-
-    const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
     while (true) {
-        const rc = pwrite_sym(fd, bytes.ptr, adjusted_len, ioffset);
+        const rc = pwrite_sym(fd, bytes.ptr, @min(bytes.len, max_count), @bitCast(offset));
         switch (errno(rc)) {
-            .SUCCESS => return @as(usize, @intCast(rc)),
+            .SUCCESS => return @intCast(rc),
             .INTR => continue,
             .INVAL => return error.InvalidArgument,
             .FAULT => unreachable,
@@ -1515,7 +1497,7 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
     if (have_pwrite_but_not_pwritev) {
         // We could loop here; but proper usage of `pwritev` must handle partial writes anyway.
         // So we simply write the first vector only.
-        if (iov.len == 0) return @as(usize, 0);
+        if (iov.len == 0) return 0;
         const first = iov[0];
         return pwrite(fd, first.iov_base[0..first.iov_len], offset);
     }
@@ -1544,13 +1526,10 @@ pub fn pwritev(fd: fd_t, iov: []const iovec_const, offset: u64) PWriteError!usiz
     }
 
     const pwritev_sym = if (lfs64_abi) system.pwritev64 else system.pwritev;
-
-    const iov_count = if (iov.len > IOV_MAX) IOV_MAX else @as(u31, @intCast(iov.len));
-    const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
     while (true) {
-        const rc = pwritev_sym(fd, iov.ptr, iov_count, ioffset);
+        const rc = pwritev_sym(fd, iov.ptr, @min(iov.len, IOV_MAX), @bitCast(offset));
         switch (errno(rc)) {
-            .SUCCESS => return @as(usize, @intCast(rc)),
+            .SUCCESS => return @intCast(rc),
             .INTR => continue,
             .INVAL => return error.InvalidArgument,
             .FAULT => unreachable,
@@ -1666,11 +1645,10 @@ pub fn openZ(file_path: [*:0]const u8, flags: O, perm: mode_t) OpenError!fd_t {
     }
 
     const open_sym = if (lfs64_abi) system.open64 else system.open;
-
     while (true) {
         const rc = open_sym(file_path, flags, perm);
         switch (errno(rc)) {
-            .SUCCESS => return @as(fd_t, @intCast(rc)),
+            .SUCCESS => return @intCast(rc),
             .INTR => continue,
 
             .FAULT => unreachable,
@@ -1863,11 +1841,10 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: O, mode: mode_t) O
     }
 
     const openat_sym = if (lfs64_abi) system.openat64 else system.openat;
-
     while (true) {
         const rc = openat_sym(dir_fd, file_path, flags, mode);
         switch (errno(rc)) {
-            .SUCCESS => return @as(fd_t, @intCast(rc)),
+            .SUCCESS => return @intCast(rc),
             .INTR => continue,
 
             .FAULT => unreachable,
@@ -1900,7 +1877,7 @@ pub fn openatZ(dir_fd: fd_t, file_path: [*:0]const u8, flags: O, mode: mode_t) O
 pub fn dup(old_fd: fd_t) !fd_t {
     const rc = system.dup(old_fd);
     return switch (errno(rc)) {
-        .SUCCESS => return @as(fd_t, @intCast(rc)),
+        .SUCCESS => return @intCast(rc),
         .MFILE => error.ProcessFdQuotaExceeded,
         .BADF => unreachable, // invalid file descriptor
         else => |err| return unexpectedErrno(err),
@@ -2144,11 +2121,11 @@ pub fn getcwd(out_buffer: []u8) GetCwdError![]u8 {
         return result;
     }
 
-    const err = if (builtin.link_libc) blk: {
+    const err: E = if (builtin.link_libc) err: {
         const c_err = if (std.c.getcwd(out_buffer.ptr, out_buffer.len)) |_| 0 else std.c._errno().*;
-        break :blk @as(E, @enumFromInt(c_err));
-    } else blk: {
-        break :blk errno(system.getcwd(out_buffer.ptr, out_buffer.len));
+        break :err @enumFromInt(c_err);
+    } else err: {
+        break :err errno(system.getcwd(out_buffer.ptr, out_buffer.len));
     };
     switch (err) {
         .SUCCESS => return mem.sliceTo(out_buffer, 0),
@@ -2880,13 +2857,13 @@ pub fn renameatW(
     // supported in order to avoid either (1) using a redundant call that we can know in advance will return
     // STATUS_NOT_SUPPORTED or (2) only setting IGNORE_READONLY_ATTRIBUTE when >= rs5
     // and therefore having different behavior when the Windows version is >= rs1 but < rs5.
-    if (comptime builtin.target.os.version_range.windows.min.isAtLeast(.win10_rs5)) {
+    if (builtin.target.os.isAtLeast(.windows, .win10_rs5) orelse false) {
         const struct_buf_len = @sizeOf(windows.FILE_RENAME_INFORMATION_EX) + (MAX_PATH_BYTES - 1);
         var rename_info_buf: [struct_buf_len]u8 align(@alignOf(windows.FILE_RENAME_INFORMATION_EX)) = undefined;
         const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION_EX) - 1 + new_path_w.len * 2;
         if (struct_len > struct_buf_len) return error.NameTooLong;
 
-        const rename_info = @as(*windows.FILE_RENAME_INFORMATION_EX, @ptrCast(&rename_info_buf));
+        const rename_info: *windows.FILE_RENAME_INFORMATION_EX = @ptrCast(&rename_info_buf);
         var io_status_block: windows.IO_STATUS_BLOCK = undefined;
 
         var flags: windows.ULONG = windows.FILE_RENAME_POSIX_SEMANTICS | windows.FILE_RENAME_IGNORE_READONLY_ATTRIBUTE;
@@ -2897,7 +2874,7 @@ pub fn renameatW(
             .FileNameLength = @intCast(new_path_w.len * 2), // already checked error.NameTooLong
             .FileName = undefined,
         };
-        @memcpy(@as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w);
+        @memcpy((&rename_info.FileName).ptr, new_path_w);
         rc = windows.ntdll.NtSetInformationFile(
             src_fd,
             &io_status_block,
@@ -2923,7 +2900,7 @@ pub fn renameatW(
         const struct_len = @sizeOf(windows.FILE_RENAME_INFORMATION) - 1 + new_path_w.len * 2;
         if (struct_len > struct_buf_len) return error.NameTooLong;
 
-        const rename_info = @as(*windows.FILE_RENAME_INFORMATION, @ptrCast(&rename_info_buf));
+        const rename_info: *windows.FILE_RENAME_INFORMATION = @ptrCast(&rename_info_buf);
         var io_status_block: windows.IO_STATUS_BLOCK = undefined;
 
         rename_info.* = .{
@@ -2932,7 +2909,7 @@ pub fn renameatW(
             .FileNameLength = @intCast(new_path_w.len * 2), // already checked error.NameTooLong
             .FileName = undefined,
         };
-        @memcpy(@as([*]u16, &rename_info.FileName)[0..new_path_w.len], new_path_w);
+        @memcpy((&rename_info.FileName).ptr, new_path_w);
 
         rc =
             windows.ntdll.NtSetInformationFile(
@@ -3380,7 +3357,7 @@ pub fn readlinkZ(file_path: [*:0]const u8, out_buffer: []u8) ReadLinkError![]u8
     }
     const rc = system.readlink(file_path, out_buffer.ptr, out_buffer.len);
     switch (errno(rc)) {
-        .SUCCESS => return out_buffer[0..@as(usize, @bitCast(rc))],
+        .SUCCESS => return out_buffer[0..@bitCast(rc)],
         .ACCES => return error.AccessDenied,
         .FAULT => unreachable,
         .INVAL => return error.NotLink,
@@ -3458,7 +3435,7 @@ pub fn readlinkatZ(dirfd: fd_t, file_path: [*:0]const u8, out_buffer: []u8) Read
     }
     const rc = system.readlinkat(dirfd, file_path, out_buffer.ptr, out_buffer.len);
     switch (errno(rc)) {
-        .SUCCESS => return out_buffer[0..@as(usize, @bitCast(rc))],
+        .SUCCESS => return out_buffer[0..@bitCast(rc)],
         .ACCES => return error.AccessDenied,
         .FAULT => unreachable,
         .INVAL => return error.NotLink,
@@ -3570,7 +3547,7 @@ pub fn isatty(handle: fd_t) bool {
     if (builtin.os.tag == .linux) {
         while (true) {
             var wsz: linux.winsize = undefined;
-            const fd = @as(usize, @bitCast(@as(isize, handle)));
+            const fd: usize = @bitCast(@as(isize, handle));
             const rc = linux.syscall3(.ioctl, fd, linux.T.IOCGWINSZ, @intFromPtr(&wsz));
             switch (linux.getErrno(rc)) {
                 .SUCCESS => return true,
@@ -3614,15 +3591,15 @@ pub fn isCygwinPty(handle: fd_t) bool {
     var name_info_bytes align(@alignOf(windows.FILE_NAME_INFO)) = [_]u8{0} ** (name_bytes_offset + num_name_bytes);
 
     var io_status_block: windows.IO_STATUS_BLOCK = undefined;
-    const rc = windows.ntdll.NtQueryInformationFile(handle, &io_status_block, &name_info_bytes, @as(u32, @intCast(name_info_bytes.len)), .FileNameInformation);
+    const rc = windows.ntdll.NtQueryInformationFile(handle, &io_status_block, &name_info_bytes, @intCast(name_info_bytes.len), .FileNameInformation);
     switch (rc) {
         .SUCCESS => {},
         .INVALID_PARAMETER => unreachable,
         else => return false,
     }
 
-    const name_info = @as(*const windows.FILE_NAME_INFO, @ptrCast(&name_info_bytes[0]));
-    const name_bytes = name_info_bytes[name_bytes_offset .. name_bytes_offset + @as(usize, name_info.FileNameLength)];
+    const name_info: *const windows.FILE_NAME_INFO = @ptrCast(&name_info_bytes);
+    const name_bytes = name_info_bytes[name_bytes_offset .. name_bytes_offset + name_info.FileNameLength];
     const name_wide = mem.bytesAsSlice(u16, name_bytes);
     // Note: The name we get from NtQueryInformationFile will be prefixed with a '\', e.g. \msys-1888ae32e00d56aa-pty0-to-master
     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
         else
             0;
         const rc = try windows.WSASocketW(
-            @as(i32, @bitCast(domain)),
-            @as(i32, @bitCast(filtered_sock_type)),
-            @as(i32, @bitCast(protocol)),
+            @bitCast(domain),
+            @bitCast(filtered_sock_type),
+            @bitCast(protocol),
             null,
             0,
             flags,
@@ -3688,7 +3665,7 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t
         return rc;
     }
 
-    const have_sock_flags = comptime !builtin.target.isDarwin();
+    const have_sock_flags = !builtin.target.isDarwin();
     const filtered_sock_type = if (!have_sock_flags)
         socket_type & ~@as(u32, SOCK.NONBLOCK | SOCK.CLOEXEC)
     else
@@ -3696,7 +3673,7 @@ pub fn socket(domain: u32, socket_type: u32, protocol: u32) SocketError!socket_t
     const rc = system.socket(domain, filtered_sock_type, protocol);
     switch (errno(rc)) {
         .SUCCESS => {
-            const fd = @as(fd_t, @intCast(rc));
+            const fd: fd_t = @intCast(rc);
             errdefer close(fd);
             if (!have_sock_flags) {
                 try setSockFlags(fd, socket_type);
@@ -3984,10 +3961,10 @@ pub fn accept(
     ///   description  of the `CLOEXEC` flag in `open` for reasons why this may be useful.
     flags: u32,
 ) AcceptError!socket_t {
-    const have_accept4 = comptime !(builtin.target.isDarwin() or builtin.os.tag == .windows);
+    const have_accept4 = !(builtin.target.isDarwin() or builtin.os.tag == .windows);
     assert(0 == (flags & ~@as(u32, SOCK.NONBLOCK | SOCK.CLOEXEC))); // Unsupported flag(s)
 
-    const accepted_sock = while (true) {
+    const accepted_sock: socket_t = while (true) {
         const rc = if (have_accept4)
             system.accept4(sock, addr, addr_size, flags)
         else if (builtin.os.tag == .windows)
@@ -4014,9 +3991,7 @@ pub fn accept(
             }
         } else {
             switch (errno(rc)) {
-                .SUCCESS => {
-                    break @as(socket_t, @intCast(rc));
-                },
+                .SUCCESS => break @intCast(rc),
                 .INTR => continue,
                 .AGAIN => return error.WouldBlock,
                 .BADF => unreachable, // always a race condition
@@ -4063,7 +4038,7 @@ pub const EpollCreateError = error{
 pub fn epoll_create1(flags: u32) EpollCreateError!i32 {
     const rc = system.epoll_create1(flags);
     switch (errno(rc)) {
-        .SUCCESS => return @as(i32, @intCast(rc)),
+        .SUCCESS => return @intCast(rc),
         else => |err| return unexpectedErrno(err),
 
         .INVAL => unreachable,
@@ -4122,9 +4097,9 @@ pub fn epoll_ctl(epfd: i32, op: u32, fd: i32, event: ?*linux.epoll_event) EpollC
 pub fn epoll_wait(epfd: i32, events: []linux.epoll_event, timeout: i32) usize {
     while (true) {
         // TODO get rid of the @intCast
-        const rc = system.epoll_wait(epfd, events.ptr, @as(u32, @intCast(events.len)), timeout);
+        const rc = system.epoll_wait(epfd, events.ptr, @intCast(events.len), timeout);
         switch (errno(rc)) {
-            .SUCCESS => return @as(usize, @intCast(rc)),
+            .SUCCESS => return @intCast(rc),
             .INTR => continue,
             .BADF => unreachable,
             .FAULT => unreachable,
@@ -4143,7 +4118,7 @@ pub const EventFdError = error{
 pub fn eventfd(initval: u32, flags: u32) EventFdError!i32 {
     const rc = system.eventfd(initval, flags);
     switch (errno(rc)) {
-        .SUCCESS => return @as(i32, @intCast(rc)),
+        .SUCCESS => return @intCast(rc),
         else => |err| return unexpectedErrno(err),
 
         .INVAL => unreachable, // invalid parameters
@@ -4277,7 +4252,7 @@ pub const ConnectError = error{
 /// return error.WouldBlock when EAGAIN or EINPROGRESS is received.
 pub fn connect(sock: socket_t, sock_addr: *const sockaddr, len: socklen_t) ConnectError!void {
     if (builtin.os.tag == .windows) {
-        const rc = windows.ws2_32.connect(sock, sock_addr, @as(i32, @intCast(len)));
+        const rc = windows.ws2_32.connect(sock, sock_addr, @intCast(len));
         if (rc == 0) return;
         switch (windows.ws2_32.WSAGetLastError()) {
             .WSAEADDRINUSE => return error.AddressInUse,
@@ -4332,7 +4307,7 @@ pub fn connect(sock: socket_t, sock_addr: *const sockaddr, len: socklen_t) Conne
 pub fn getsockoptError(sockfd: fd_t) ConnectError!void {
     var err_code: i32 = undefined;
     var size: u32 = @sizeOf(u32);
-    const rc = system.getsockopt(sockfd, SOL.SOCKET, SO.ERROR, @as([*]u8, @ptrCast(&err_code)), &size);
+    const rc = system.getsockopt(sockfd, SOL.SOCKET, SO.ERROR, @ptrCast(&err_code), &size);
     assert(size == 4);
     switch (errno(rc)) {
         .SUCCESS => switch (@as(E, @enumFromInt(err_code))) {
@@ -4373,15 +4348,13 @@ pub const WaitPidResult = struct {
 /// Use this version of the `waitpid` wrapper if you spawned your child process using explicit
 /// `fork` and `execve` method.
 pub fn waitpid(pid: pid_t, flags: u32) WaitPidResult {
-    const Status = if (builtin.link_libc) c_int else u32;
-    var status: Status = undefined;
-    const coerced_flags = if (builtin.link_libc) @as(c_int, @intCast(flags)) else flags;
+    var status: if (builtin.link_libc) c_int else u32 = undefined;
     while (true) {
-        const rc = system.waitpid(pid, &status, coerced_flags);
+        const rc = system.waitpid(pid, &status, @intCast(flags));
         switch (errno(rc)) {
             .SUCCESS => return .{
-                .pid = @as(pid_t, @intCast(rc)),
-                .status = @as(u32, @bitCast(status)),
+                .pid = @intCast(rc),
+                .status = @bitCast(status),
             },
             .INTR => continue,
             .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 {
 }
 
 pub fn wait4(pid: pid_t, flags: u32, ru: ?*rusage) WaitPidResult {
-    const Status = if (builtin.link_libc) c_int else u32;
-    var status: Status = undefined;
-    const coerced_flags = if (builtin.link_libc) @as(c_int, @intCast(flags)) else flags;
+    var status: if (builtin.link_libc) c_int else u32 = undefined;
     while (true) {
-        const rc = system.wait4(pid, &status, coerced_flags, ru);
+        const rc = system.wait4(pid, &status, @intCast(flags), ru);
         switch (errno(rc)) {
             .SUCCESS => return .{
-                .pid = @as(pid_t, @intCast(rc)),
-                .status = @as(u32, @bitCast(status)),
+                .pid = @intCast(rc),
+                .status = @bitCast(status),
             },
             .INTR => continue,
             .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 {
     }
 
     const fstat_sym = if (lfs64_abi) system.fstat64 else system.fstat;
-
     var stat = mem.zeroes(Stat);
     switch (errno(fstat_sym(fd, &stat))) {
         .SUCCESS => return stat,
@@ -4512,7 +4482,6 @@ pub fn fstatatZ(dirfd: fd_t, pathname: [*:0]const u8, flags: u32) FStatAtError!S
     }
 
     const fstatat_sym = if (lfs64_abi) system.fstatat64 else system.fstatat;
-
     var stat = mem.zeroes(Stat);
     switch (errno(fstatat_sym(dirfd, pathname, &stat, flags))) {
         .SUCCESS => return stat,
@@ -4545,7 +4514,7 @@ pub const KQueueError = error{
 pub fn kqueue() KQueueError!i32 {
     const rc = system.kqueue();
     switch (errno(rc)) {
-        .SUCCESS => return @as(i32, @intCast(rc)),
+        .SUCCESS => return @intCast(rc),
         .MFILE => return error.ProcessFdQuotaExceeded,
         .NFILE => return error.SystemFdQuotaExceeded,
         else => |err| return unexpectedErrno(err),
@@ -4586,7 +4555,7 @@ pub fn kevent(
             timeout,
         );
         switch (errno(rc)) {
-            .SUCCESS => return @as(usize, @intCast(rc)),
+            .SUCCESS => return @intCast(rc),
             .ACCES => return error.AccessDenied,
             .FAULT => unreachable,
             .BADF => unreachable, // Always a race condition.
@@ -4610,7 +4579,7 @@ pub const INotifyInitError = error{
 pub fn inotify_init1(flags: u32) INotifyInitError!i32 {
     const rc = system.inotify_init1(flags);
     switch (errno(rc)) {
-        .SUCCESS => return @as(i32, @intCast(rc)),
+        .SUCCESS => return @intCast(rc),
         .INVAL => unreachable,
         .MFILE => return error.ProcessFdQuotaExceeded,
         .NFILE => return error.SystemFdQuotaExceeded,
@@ -4639,7 +4608,7 @@ pub fn inotify_add_watch(inotify_fd: i32, pathname: []const u8, mask: u32) INoti
 pub fn inotify_add_watchZ(inotify_fd: i32, pathname: [*:0]const u8, mask: u32) INotifyAddWatchError!i32 {
     const rc = system.inotify_add_watch(inotify_fd, pathname, mask);
     switch (errno(rc)) {
-        .SUCCESS => return @as(i32, @intCast(rc)),
+        .SUCCESS => return @intCast(rc),
         .ACCES => return error.AccessDenied,
         .BADF => unreachable,
         .FAULT => unreachable,
@@ -4675,7 +4644,7 @@ pub const FanotifyInitError = error{
 pub fn fanotify_init(flags: u32, event_f_flags: u32) FanotifyInitError!i32 {
     const rc = system.fanotify_init(flags, event_f_flags);
     switch (errno(rc)) {
-        .SUCCESS => return @as(i32, @intCast(rc)),
+        .SUCCESS => return @intCast(rc),
         .INVAL => unreachable,
         .MFILE => return error.ProcessFdQuotaExceeded,
         .NFILE => return error.SystemFdQuotaExceeded,
@@ -4780,7 +4749,7 @@ pub const ForkError = error{SystemResources} || UnexpectedError;
 pub fn fork() ForkError!pid_t {
     const rc = system.fork();
     switch (errno(rc)) {
-        .SUCCESS => return @as(pid_t, @intCast(rc)),
+        .SUCCESS => return @intCast(rc),
         .AGAIN => return error.SystemResources,
         .NOMEM => return error.SystemResources,
         else => |err| return unexpectedErrno(err),
@@ -4820,12 +4789,10 @@ pub fn mmap(
     offset: u64,
 ) MMapError![]align(mem.page_size) u8 {
     const mmap_sym = if (lfs64_abi) system.mmap64 else system.mmap;
-
-    const ioffset: i64 = @bitCast(offset); // the OS treats this as unsigned
-    const rc = mmap_sym(ptr, length, prot, @bitCast(flags), fd, ioffset);
-    const err = if (builtin.link_libc) blk: {
+    const rc = mmap_sym(ptr, length, prot, @bitCast(flags), fd, @bitCast(offset));
+    const err: E = if (builtin.link_libc) blk: {
         if (rc != std.c.MAP_FAILED) return @as([*]align(mem.page_size) u8, @ptrCast(@alignCast(rc)))[0..length];
-        break :blk @as(E, @enumFromInt(system._errno().*));
+        break :blk @enumFromInt(system._errno().*);
     } else blk: {
         const err = errno(rc);
         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 {
     }
     if (builtin.os.tag == .wasi and !builtin.link_libc) {
         var new_offset: wasi.filesize_t = undefined;
-        switch (wasi.fd_seek(fd, @as(wasi.filedelta_t, @bitCast(offset)), .SET, &new_offset)) {
+        switch (wasi.fd_seek(fd, @bitCast(offset), .SET, &new_offset)) {
             .SUCCESS => return,
             .BADF => unreachable, // always a race condition
             .INVAL => return error.Unseekable,
@@ -5263,9 +5230,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
     }
 
     const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek;
-
-    const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
-    switch (errno(lseek_sym(fd, ioffset, SEEK.SET))) {
+    switch (errno(lseek_sym(fd, @bitCast(offset), SEEK.SET))) {
         .SUCCESS => return,
         .BADF => unreachable, // always a race condition
         .INVAL => return error.Unseekable,
@@ -5280,7 +5245,7 @@ pub fn lseek_SET(fd: fd_t, offset: u64) SeekError!void {
 pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
     if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
         var result: u64 = undefined;
-        switch (errno(system.llseek(fd, @as(u64, @bitCast(offset)), &result, SEEK.CUR))) {
+        switch (errno(system.llseek(fd, @bitCast(offset), &result, SEEK.CUR))) {
             .SUCCESS => return,
             .BADF => unreachable, // always a race condition
             .INVAL => return error.Unseekable,
@@ -5307,9 +5272,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
         }
     }
     const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek;
-
-    const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
-    switch (errno(lseek_sym(fd, ioffset, SEEK.CUR))) {
+    switch (errno(lseek_sym(fd, @bitCast(offset), SEEK.CUR))) {
         .SUCCESS => return,
         .BADF => unreachable, // always a race condition
         .INVAL => return error.Unseekable,
@@ -5324,7 +5287,7 @@ pub fn lseek_CUR(fd: fd_t, offset: i64) SeekError!void {
 pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
     if (builtin.os.tag == .linux and !builtin.link_libc and @sizeOf(usize) == 4) {
         var result: u64 = undefined;
-        switch (errno(system.llseek(fd, @as(u64, @bitCast(offset)), &result, SEEK.END))) {
+        switch (errno(system.llseek(fd, @bitCast(offset), &result, SEEK.END))) {
             .SUCCESS => return,
             .BADF => unreachable, // always a race condition
             .INVAL => return error.Unseekable,
@@ -5351,9 +5314,7 @@ pub fn lseek_END(fd: fd_t, offset: i64) SeekError!void {
         }
     }
     const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek;
-
-    const ioffset = @as(i64, @bitCast(offset)); // the OS treats this as unsigned
-    switch (errno(lseek_sym(fd, ioffset, SEEK.END))) {
+    switch (errno(lseek_sym(fd, @bitCast(offset), SEEK.END))) {
         .SUCCESS => return,
         .BADF => unreachable, // always a race condition
         .INVAL => return error.Unseekable,
@@ -5395,10 +5356,9 @@ pub fn lseek_CUR_get(fd: fd_t) SeekError!u64 {
         }
     }
     const lseek_sym = if (lfs64_abi) system.lseek64 else system.lseek;
-
     const rc = lseek_sym(fd, 0, SEEK.CUR);
     switch (errno(rc)) {
-        .SUCCESS => return @as(u64, @bitCast(rc)),
+        .SUCCESS => return @bitCast(rc),
         .BADF => unreachable, // always a race condition
         .INVAL => return error.Unseekable,
         .OVERFLOW => return error.Unseekable,
@@ -5421,7 +5381,7 @@ pub fn fcntl(fd: fd_t, cmd: i32, arg: usize) FcntlError!usize {
     while (true) {
         const rc = system.fcntl(fd, cmd, arg);
         switch (errno(rc)) {
-            .SUCCESS => return @as(usize, @intCast(rc)),
+            .SUCCESS => return @intCast(rc),
             .INTR => continue,
             .AGAIN, .ACCES => return error.Locked,
             .BADF => unreachable,
@@ -5714,7 +5674,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
                 // errno values to expect when command is F.GETPATH...
                 else => |err| return unexpectedErrno(err),
             }
-            const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES;
+            const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse MAX_PATH_BYTES;
             return out_buffer[0..len];
         },
         .linux => {
@@ -5746,7 +5706,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
             return target;
         },
         .freebsd => {
-            if (comptime builtin.os.version_range.semver.max.order(.{ .major = 13, .minor = 0, .patch = 0 }) == .gt) {
+            if (comptime builtin.os.isAtLeast(.freebsd, .{ .major = 13, .minor = 0, .patch = 0 }) orelse false) {
                 var kfile: system.kinfo_file = undefined;
                 kfile.structsize = system.KINFO_FILE_SIZE;
                 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 {
                 };
                 var i: usize = 0;
                 while (i < len) {
-                    const kf: *align(1) system.kinfo_file = @as(*align(1) system.kinfo_file, @ptrCast(&buf[i]));
+                    const kf: *align(1) system.kinfo_file = @ptrCast(&buf[i]);
                     if (kf.fd == fd) {
                         len = mem.indexOfScalar(u8, &kf.path, 0) orelse MAX_PATH_BYTES;
                         if (len == 0) return error.NameTooLong;
@@ -5793,7 +5753,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
                         @memcpy(result, kf.path[0..len]);
                         return result;
                     }
-                    i += @as(usize, @intCast(kf.structsize));
+                    i += @intCast(kf.structsize);
                 }
                 return error.FileNotFound;
             }
@@ -5806,7 +5766,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
                 .RANGE => return error.NameTooLong,
                 else => |err| return unexpectedErrno(err),
             }
-            const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES;
+            const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse MAX_PATH_BYTES;
             return out_buffer[0..len];
         },
         .netbsd => {
@@ -5820,7 +5780,7 @@ pub fn getFdPath(fd: fd_t, out_buffer: *[MAX_PATH_BYTES]u8) RealPathError![]u8 {
                 .RANGE => return error.NameTooLong,
                 else => |err| return unexpectedErrno(err),
             }
-            const len = mem.indexOfScalar(u8, out_buffer[0..], @as(u8, 0)) orelse MAX_PATH_BYTES;
+            const len = mem.indexOfScalar(u8, out_buffer[0..], 0) orelse MAX_PATH_BYTES;
             return out_buffer[0..len];
         },
         else => unreachable, // made unreachable by isGetFdPathSupportedOnTarget above
@@ -5871,14 +5831,14 @@ pub fn dl_iterate_phdr(
                 callback(info, size, context_ptr.*) catch |err| return @intFromError(err);
                 return 0;
             }
-        }.callbackC, @as(?*anyopaque, @ptrFromInt(@intFromPtr(&context))))) {
+        }.callbackC, @ptrCast(@constCast(&context)))) {
             0 => return,
             else => |err| return @as(Error, @errorCast(@errorFromInt(@as(std.meta.Int(.unsigned, @bitSizeOf(anyerror)), @intCast(err))))),
         }
     }
 
     const elf_base = std.process.getBaseAddress();
-    const ehdr = @as(*elf.Ehdr, @ptrFromInt(elf_base));
+    const ehdr: *elf.Ehdr = @ptrFromInt(elf_base);
     // Make sure the base address points to an ELF image.
     assert(mem.eql(u8, ehdr.e_ident[0..4], elf.MAGIC));
     const n_phdr = ehdr.e_phnum;
@@ -5916,12 +5876,12 @@ pub fn dl_iterate_phdr(
         var dlpi_phnum: u16 = undefined;
 
         if (entry.l_addr != 0) {
-            const elf_header = @as(*elf.Ehdr, @ptrFromInt(entry.l_addr));
-            dlpi_phdr = @as([*]elf.Phdr, @ptrFromInt(entry.l_addr + elf_header.e_phoff));
+            const elf_header: *elf.Ehdr = @ptrFromInt(entry.l_addr);
+            dlpi_phdr = @ptrFromInt(entry.l_addr + elf_header.e_phoff);
             dlpi_phnum = elf_header.e_phnum;
         } else {
             // This is the running ELF image
-            dlpi_phdr = @as([*]elf.Phdr, @ptrFromInt(elf_base + ehdr.e_phoff));
+            dlpi_phdr = @ptrFromInt(elf_base + ehdr.e_phoff);
             dlpi_phnum = ehdr.e_phnum;
         }
 
@@ -5943,11 +5903,11 @@ pub const ClockGetTimeError = error{UnsupportedClock} || UnexpectedError;
 pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
     if (builtin.os.tag == .wasi and !builtin.link_libc) {
         var ts: timestamp_t = undefined;
-        switch (system.clock_time_get(@as(u32, @bitCast(clk_id)), 1, &ts)) {
+        switch (system.clock_time_get(@bitCast(clk_id), 1, &ts)) {
             .SUCCESS => {
                 tp.* = .{
-                    .tv_sec = @as(i64, @intCast(ts / std.time.ns_per_s)),
-                    .tv_nsec = @as(isize, @intCast(ts % std.time.ns_per_s)),
+                    .tv_sec = @intCast(ts / std.time.ns_per_s),
+                    .tv_nsec = @intCast(ts % std.time.ns_per_s),
                 };
             },
             .INVAL => return error.UnsupportedClock,
@@ -5984,10 +5944,10 @@ pub fn clock_gettime(clk_id: i32, tp: *timespec) ClockGetTimeError!void {
 pub fn clock_getres(clk_id: i32, res: *timespec) ClockGetTimeError!void {
     if (builtin.os.tag == .wasi and !builtin.link_libc) {
         var ts: timestamp_t = undefined;
-        switch (system.clock_res_get(@as(u32, @bitCast(clk_id)), &ts)) {
+        switch (system.clock_res_get(@bitCast(clk_id), &ts)) {
             .SUCCESS => res.* = .{
-                .tv_sec = @as(i64, @intCast(ts / std.time.ns_per_s)),
-                .tv_nsec = @as(isize, @intCast(ts % std.time.ns_per_s)),
+                .tv_sec = @intCast(ts / std.time.ns_per_s),
+                .tv_nsec = @intCast(ts % std.time.ns_per_s),
             },
             .INVAL => return error.UnsupportedClock,
             else => |err| return unexpectedErrno(err),
@@ -6212,7 +6172,7 @@ pub fn res_mkquery(
         // TODO determine the circumstances for this and whether or
         // not this should be an error.
         if (j - i - 1 > 62) unreachable;
-        q[i - 1] = @as(u8, @intCast(j - i));
+        q[i - 1] = @intCast(j - i);
     }
     q[i + 1] = ty;
     q[i + 3] = class;
@@ -6221,10 +6181,10 @@ pub fn res_mkquery(
     var ts: timespec = undefined;
     clock_gettime(CLOCK.REALTIME, &ts) catch {};
     const UInt = std.meta.Int(.unsigned, @bitSizeOf(@TypeOf(ts.tv_nsec)));
-    const unsec = @as(UInt, @bitCast(ts.tv_nsec));
-    const id = @as(u32, @truncate(unsec + unsec / 65536));
-    q[0] = @as(u8, @truncate(id / 256));
-    q[1] = @as(u8, @truncate(id));
+    const unsec: UInt = @bitCast(ts.tv_nsec);
+    const id: u32 = @truncate(unsec + unsec / 65536);
+    q[0] = @truncate(id / 256);
+    q[1] = @truncate(id);
 
     @memcpy(buf[0..n], q[0..n]);
     return n;
@@ -6330,11 +6290,11 @@ pub fn sendmsg(
                     else => |err| return windows.unexpectedWSAError(err),
                 }
             } else {
-                return @as(usize, @intCast(rc));
+                return @intCast(rc);
             }
         } else {
             switch (errno(rc)) {
-                .SUCCESS => return @as(usize, @intCast(rc)),
+                .SUCCESS => return @intCast(rc),
 
                 .ACCES => return error.AccessDenied,
                 .AGAIN => return error.WouldBlock,
@@ -6430,13 +6390,13 @@ pub fn sendto(
                 .WSANOTINITIALISED => unreachable, // A successful WSAStartup call must occur before using this function.
                 else => |err| return windows.unexpectedWSAError(err),
             },
-            else => |rc| return @as(usize, @intCast(rc)),
+            else => |rc| return @intCast(rc),
         }
     }
     while (true) {
         const rc = system.sendto(sockfd, buf.ptr, buf.len, flags, dest_addr, addrlen);
         switch (errno(rc)) {
-            .SUCCESS => return @as(usize, @intCast(rc)),
+            .SUCCESS => return @intCast(rc),
 
             .ACCES => return error.AccessDenied,
             .AGAIN => return error.WouldBlock,
@@ -6588,18 +6548,15 @@ pub fn sendfile(
             }
 
             // Here we match BSD behavior, making a zero count value send as many bytes as possible.
-            const adjusted_count_tmp = if (in_len == 0) max_count else @min(in_len, @as(size_t, max_count));
-            // TODO we should not need this cast; improve return type of @min
-            const adjusted_count = @as(usize, @intCast(adjusted_count_tmp));
+            const adjusted_count = if (in_len == 0) max_count else @min(in_len, max_count);
 
             const sendfile_sym = if (lfs64_abi) system.sendfile64 else system.sendfile;
-
             while (true) {
-                var offset: off_t = @as(off_t, @bitCast(in_offset));
+                var offset: off_t = @bitCast(in_offset);
                 const rc = sendfile_sym(out_fd, in_fd, &offset, adjusted_count);
                 switch (errno(rc)) {
                     .SUCCESS => {
-                        const amt = @as(usize, @bitCast(rc));
+                        const amt: usize = @bitCast(rc);
                         total_written += amt;
                         if (in_len == 0 and amt == 0) {
                             // We have detected EOF from `in_fd`.
@@ -6665,13 +6622,10 @@ pub fn sendfile(
                 hdtr = &hdtr_data;
             }
 
-            const adjusted_count = @min(in_len, max_count);
-
             while (true) {
                 var sbytes: off_t = undefined;
-                const offset = @as(off_t, @bitCast(in_offset));
-                const err = errno(system.sendfile(in_fd, out_fd, offset, adjusted_count, hdtr, &sbytes, flags));
-                const amt = @as(usize, @bitCast(sbytes));
+                const err = errno(system.sendfile(in_fd, out_fd, @bitCast(in_offset), @min(in_len, max_count), hdtr, &sbytes, flags));
+                const amt: usize = @bitCast(sbytes);
                 switch (err) {
                     .SUCCESS => return amt,
 
@@ -6738,15 +6692,10 @@ pub fn sendfile(
                 hdtr = &hdtr_data;
             }
 
-            const adjusted_count_temporary = @min(in_len, @as(u63, max_count));
-            // TODO we should not need this int cast; improve the return type of `@min`
-            const adjusted_count = @as(u63, @intCast(adjusted_count_temporary));
-
             while (true) {
-                var sbytes: off_t = adjusted_count;
-                const signed_offset = @as(i64, @bitCast(in_offset));
-                const err = errno(system.sendfile(in_fd, out_fd, signed_offset, &sbytes, hdtr, flags));
-                const amt = @as(usize, @bitCast(sbytes));
+                var sbytes: off_t = @min(in_len, max_count);
+                const err = errno(system.sendfile(in_fd, out_fd, @bitCast(in_offset), &sbytes, hdtr, flags));
+                const amt: usize = @bitCast(sbytes);
                 switch (err) {
                     .SUCCESS => return amt,
 
@@ -6791,9 +6740,7 @@ pub fn sendfile(
     rw: {
         var buf: [8 * 4096]u8 = undefined;
         // Here we match BSD behavior, making a zero count value send as many bytes as possible.
-        const adjusted_count_tmp = if (in_len == 0) buf.len else @min(buf.len, in_len);
-        // TODO we should not need this cast; improve return type of @min
-        const adjusted_count = @as(usize, @intCast(adjusted_count_tmp));
+        const adjusted_count = if (in_len == 0) buf.len else @min(buf.len, in_len);
         const amt_read = try pread(in_fd, buf[0..adjusted_count], in_offset);
         if (amt_read == 0) {
             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
         std.c.versionCheck(.{ .major = 2, .minor = 27, .patch = 0 })) and
         has_copy_file_range_syscall.load(.Monotonic)))
     {
-        var off_in_copy = @as(i64, @bitCast(off_in));
-        var off_out_copy = @as(i64, @bitCast(off_out));
+        var off_in_copy: i64 = @bitCast(off_in);
+        var off_out_copy: i64 = @bitCast(off_out);
 
         while (true) {
             const rc = system.copy_file_range(fd_in, &off_in_copy, fd_out, &off_out_copy, len, flags);
             if (builtin.os.tag == .freebsd) {
                 switch (system.getErrno(rc)) {
-                    .SUCCESS => return @as(usize, @intCast(rc)),
+                    .SUCCESS => return @intCast(rc),
                     .BADF => return error.FilesOpenedWithWrongFlags,
                     .FBIG => return error.FileTooBig,
                     .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
                 }
             } else { // assume linux
                 switch (system.getErrno(rc)) {
-                    .SUCCESS => return @as(usize, @intCast(rc)),
+                    .SUCCESS => return @intCast(rc),
                     .BADF => return error.FilesOpenedWithWrongFlags,
                     .FBIG => return error.FileTooBig,
                     .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
     }
 
     var buf: [8 * 4096]u8 = undefined;
-    const adjusted_count = @min(buf.len, len);
-    const amt_read = try pread(fd_in, buf[0..adjusted_count], off_in);
-    // TODO without @as the line below fails to compile for wasm32-wasi:
-    // error: integer value 0 cannot be coerced to type 'os.PWriteError!usize'
-    if (amt_read == 0) return @as(usize, 0);
+    const amt_read = try pread(fd_in, buf[0..@min(buf.len, len)], off_in);
+    if (amt_read == 0) return 0;
     return pwrite(fd_out, buf[0..amt_read], off_out);
 }
 
@@ -6937,11 +6881,11 @@ pub fn poll(fds: []pollfd, timeout: i32) PollError!usize {
                     else => |err| return windows.unexpectedWSAError(err),
                 }
             } else {
-                return @as(usize, @intCast(rc));
+                return @intCast(rc);
             }
         } else {
             switch (errno(rc)) {
-                .SUCCESS => return @as(usize, @intCast(rc)),
+                .SUCCESS => return @intCast(rc),
                 .FAULT => unreachable,
                 .INTR => continue,
                 .INVAL => unreachable,
@@ -6971,7 +6915,7 @@ pub fn ppoll(fds: []pollfd, timeout: ?*const timespec, mask: ?*const sigset_t) P
     const fds_count = math.cast(nfds_t, fds.len) orelse return error.SystemResources;
     const rc = system.ppoll(fds.ptr, fds_count, ts_ptr, mask);
     switch (errno(rc)) {
-        .SUCCESS => return @as(usize, @intCast(rc)),
+        .SUCCESS => return @intCast(rc),
         .FAULT => unreachable,
         .INTR => return error.SignalInterrupt,
         .INVAL => unreachable,
@@ -7038,11 +6982,11 @@ pub fn recvfrom(
                     else => |err| return windows.unexpectedWSAError(err),
                 }
             } else {
-                return @as(usize, @intCast(rc));
+                return @intCast(rc);
             }
         } else {
             switch (errno(rc)) {
-                .SUCCESS => return @as(usize, @intCast(rc)),
+                .SUCCESS => return @intCast(rc),
                 .BADF => unreachable, // always a race condition
                 .FAULT => unreachable,
                 .INVAL => unreachable,
@@ -7081,7 +7025,7 @@ pub fn dn_expand(
         // loop invariants: p<end, dest<dend
         if ((p[0] & 0xc0) != 0) {
             if (p + 1 == end) return error.InvalidDnsPacket;
-            const j = ((p[0] & @as(usize, 0x3f)) << 8) | p[1];
+            const j = @as(usize, p[0] & 0x3f) << 8 | p[1];
             if (len == std.math.maxInt(usize)) len = @intFromPtr(p) + 2 - @intFromPtr(comp_dn.ptr);
             if (j >= msg.len) return error.InvalidDnsPacket;
             p = msg.ptr + j;
@@ -7135,7 +7079,7 @@ pub const SetSockOptError = error{
 /// Set a socket's options.
 pub fn setsockopt(fd: socket_t, level: u32, optname: u32, opt: []const u8) SetSockOptError!void {
     if (builtin.os.tag == .windows) {
-        const rc = windows.ws2_32.setsockopt(fd, @as(i32, @intCast(level)), @as(i32, @intCast(optname)), opt.ptr, @as(i32, @intCast(opt.len)));
+        const rc = windows.ws2_32.setsockopt(fd, @intCast(level), @intCast(optname), opt.ptr, @intCast(opt.len));
         if (rc == windows.ws2_32.SOCKET_ERROR) {
             switch (windows.ws2_32.WSAGetLastError()) {
                 .WSANOTINITIALISED => unreachable,
@@ -7148,7 +7092,7 @@ pub fn setsockopt(fd: socket_t, level: u32, optname: u32, opt: []const u8) SetSo
         }
         return;
     } else {
-        switch (errno(system.setsockopt(fd, level, optname, opt.ptr, @as(socklen_t, @intCast(opt.len))))) {
+        switch (errno(system.setsockopt(fd, level, optname, opt.ptr, @intCast(opt.len)))) {
             .SUCCESS => {},
             .BADF => unreachable, // always a race condition
             .NOTSOCK => unreachable, // always a race condition
@@ -7185,7 +7129,7 @@ pub fn memfd_createZ(name: [*:0]const u8, flags: u32) MemFdCreateError!fd_t {
             const getErrno = if (use_c) std.c.getErrno else linux.getErrno;
             const rc = sys.memfd_create(name, flags);
             switch (getErrno(rc)) {
-                .SUCCESS => return @as(fd_t, @intCast(rc)),
+                .SUCCESS => return @intCast(rc),
                 .FAULT => unreachable, // name has invalid memory
                 .INVAL => unreachable, // name/flags are faulty
                 .NFILE => return error.SystemFdQuotaExceeded,
@@ -7335,7 +7279,7 @@ pub fn ioctl_SIOCGIFINDEX(fd: fd_t, ifr: *ifreq) IoCtl_SIOCGIFINDEX_Error!void {
 pub fn signalfd(fd: fd_t, mask: *const sigset_t, flags: u32) !fd_t {
     const rc = system.signalfd(fd, mask, flags);
     switch (errno(rc)) {
-        .SUCCESS => return @as(fd_t, @intCast(rc)),
+        .SUCCESS => return @intCast(rc),
         .BADF, .INVAL => unreachable,
         .NFILE => return error.SystemFdQuotaExceeded,
         .NOMEM => return error.SystemResources,
@@ -7443,7 +7387,7 @@ pub fn prctl(option: PR, args: anytype) PrctlError!u31 {
 
     const rc = system.prctl(@intFromEnum(option), buf[0], buf[1], buf[2], buf[3]);
     switch (errno(rc)) {
-        .SUCCESS => return @as(u31, @intCast(rc)),
+        .SUCCESS => return @intCast(rc),
         .ACCES => return error.AccessDenied,
         .BADF => return error.InvalidFileDescriptor,
         .FAULT => return error.InvalidAddress,
@@ -7624,7 +7568,7 @@ pub fn perf_event_open(
 ) PerfEventOpenError!fd_t {
     const rc = linux.perf_event_open(attr, pid, cpu, group_fd, flags);
     switch (errno(rc)) {
-        .SUCCESS => return @as(fd_t, @intCast(rc)),
+        .SUCCESS => return @intCast(rc),
         .@"2BIG" => return error.TooBig,
         .ACCES => return error.PermissionDenied,
         .BADF => unreachable, // group_fd file descriptor is not valid.
@@ -7659,7 +7603,7 @@ pub const TimerFdSetError = TimerFdGetError || error{Canceled};
 pub fn timerfd_create(clokid: i32, flags: linux.TFD) TimerFdCreateError!fd_t {
     const rc = linux.timerfd_create(clokid, flags);
     return switch (errno(rc)) {
-        .SUCCESS => @as(fd_t, @intCast(rc)),
+        .SUCCESS => @intCast(rc),
         .INVAL => unreachable,
         .MFILE => return error.ProcessFdQuotaExceeded,
         .NFILE => return error.SystemFdQuotaExceeded,
@@ -7702,7 +7646,6 @@ pub fn timerfd_gettime(fd: i32) TimerFdGetError!linux.itimerspec {
 pub const PtraceError = error{
     DeviceBusy,
     InputOutput,
-    Overflow,
     ProcessNotFound,
     PermissionDenied,
 } || UnexpectedError;
@@ -7724,10 +7667,10 @@ pub fn ptrace(request: u32, pid: pid_t, addr: usize, signal: usize) PtraceError!
         },
 
         .macos, .ios, .tvos, .watchos => switch (errno(darwin.ptrace(
-            math.cast(i32, request) orelse return error.Overflow,
+            @intCast(request),
             pid,
-            @as(?[*]u8, @ptrFromInt(addr)),
-            math.cast(i32, signal) orelse return error.Overflow,
+            @ptrFromInt(addr),
+            @intCast(signal),
         ))) {
             .SUCCESS => {},
             .SRCH => error.ProcessNotFound,
lib/std/Target.zig
@@ -392,7 +392,10 @@ pub const Os = struct {
 
     /// Checks if system is guaranteed to be at least `version` or older than `version`.
     /// Returns `null` if a runtime check is required.
-    pub inline fn isAtLeast(self: Os, comptime tag: Tag, version: anytype) ?bool {
+    pub inline fn isAtLeast(self: Os, comptime tag: Tag, version: switch (tag) {
+        else => std.SemanticVersion,
+        .windows => WindowsVersion,
+    }) ?bool {
         if (self.tag != tag) return false;
 
         return switch (tag) {