diff --git a/lib/std/array_list.zig b/lib/std/array_list.zig index 87bf3c51df..523d1c810f 100644 --- a/lib/std/array_list.zig +++ b/lib/std/array_list.zig @@ -189,32 +189,30 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type { self.len += items.len; } - pub usingnamespace if (T == u8) - struct { - /// Same as `append` except it returns the number of bytes written, which is always the same - /// as `m.len`. The purpose of this function existing is to match `std.io.OutStream` API. - fn appendWrite(self: *Self, m: []const u8) !usize { - try self.appendSlice(m); - return m.len; - } + /// Same as `append` except it returns the number of bytes written, which is always the same + /// as `m.len`. The purpose of this function existing is to match `std.io.OutStream` API. + /// This function may be called only when `T` is `u8`. + fn appendWrite(self: *Self, m: []const u8) !usize { + try self.appendSlice(m); + return m.len; + } - pub fn outStream(self: *Self) std.io.OutStream(*Self, error{OutOfMemory}, appendWrite) { - return .{ .context = self }; - } - } - else - struct {}; + /// Initializes an OutStream which will append to the list. + /// This function may be called only when `T` is `u8`. + pub fn outStream(self: *Self) std.io.OutStream(*Self, error{OutOfMemory}, appendWrite) { + return .{ .context = self }; + } - /// Append a value to the list `n` times. Allocates more memory - /// as necessary. + /// Append a value to the list `n` times. + /// Allocates more memory as necessary. pub fn appendNTimes(self: *Self, value: T, n: usize) !void { const old_len = self.len; try self.resize(self.len + n); mem.set(T, self.items[old_len..self.len], value); } - /// Adjust the list's length to `new_len`. Doesn't initialize - /// added items if any. + /// Adjust the list's length to `new_len`. + /// Does not initialize added items if any. pub fn resize(self: *Self, new_len: usize) !void { try self.ensureCapacity(new_len); self.len = new_len; diff --git a/lib/std/build.zig b/lib/std/build.zig index 626638647e..ccd0ebaf8a 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -1961,7 +1961,6 @@ pub const LibExeObjStep = struct { } } else { var mcpu_buffer = std.ArrayList(u8).init(builder.allocator); - errdefer mcpu_buffer.deinit(); try mcpu_buffer.outStream().print("-mcpu={}", .{cross.cpu.model.name}); diff --git a/lib/std/child_process.zig b/lib/std/child_process.zig index 8e341a07b8..0c24af2d85 100644 --- a/lib/std/child_process.zig +++ b/lib/std/child_process.zig @@ -757,7 +757,6 @@ fn windowsCreateProcess(app_name: [*:0]u16, cmd_line: [*:0]u16, envp_ptr: ?[*]u1 } /// Caller must dealloc. -/// Guarantees a null byte at result[result.len]. fn windowsCreateCommandLine(allocator: *mem.Allocator, argv: []const []const u8) ![:0]u8 { var buf = try Buffer.initSize(allocator, 0); defer buf.deinit(); diff --git a/lib/std/zig/cross_target.zig b/lib/std/zig/cross_target.zig index 9099519274..4d19ecee8c 100644 --- a/lib/std/zig/cross_target.zig +++ b/lib/std/zig/cross_target.zig @@ -504,7 +504,7 @@ pub const CrossTarget = struct { const os_name = if (self.os_tag) |os_tag| @tagName(os_tag) else "native"; var result = std.ArrayList(u8).init(allocator); - errdefer result.deinit(); + defer result.deinit(); try result.outStream().print("{}-{}", .{ arch_name, os_name }); diff --git a/src-self-hosted/libc_installation.zig b/src-self-hosted/libc_installation.zig index f802cbd606..11708e2a31 100644 --- a/src-self-hosted/libc_installation.zig +++ b/src-self-hosted/libc_installation.zig @@ -327,20 +327,14 @@ pub const LibCInstallation = struct { var search_buf: [2]Search = undefined; const searches = fillSearch(&search_buf, sdk); - for (searches) |search| { - const dir_path = try fs.path.join( - allocator, - &[_][]const u8{ - search.path, - "Include", - search.version, - "ucrt", - }, - ); - var found = false; - defer if (!found) allocator.free(dir_path); + var result_buf = std.ArrayList([]const u8).init(allocator); + defer result_buf.deinit(); - var dir = fs.cwd().openDir(dir_path, .{}) catch |err| switch (err) { + for (searches) |search| { + result_buf.shrink(0); + try result_buf.outStream().print("{}\\Include\\{}\\ucrt", .{ search.path, search.version }); + + var dir = fs.cwd().openDir(result_buf.span(), .{}) catch |err| switch (err) { error.FileNotFound, error.NotDir, error.NoDevice, @@ -355,8 +349,7 @@ pub const LibCInstallation = struct { else => return error.FileSystem, }; - found = true; - self.include_dir = dir_path; + self.include_dir = result_buf.toOwnedSlice(); return; } @@ -373,6 +366,9 @@ pub const LibCInstallation = struct { var search_buf: [2]Search = undefined; const searches = fillSearch(&search_buf, sdk); + var result_buf = try std.ArrayList([]const u8).init(allocator); + defer result_buf.deinit(); + const arch_sub_dir = switch (builtin.arch) { .i386 => "x86", .x86_64 => "x64", @@ -381,20 +377,10 @@ pub const LibCInstallation = struct { }; for (searches) |search| { - const dir_path = try fs.path.join( - allocator, - &[_][]const u8{ - search.path, - "Lib", - search.version, - "ucrt", - arch_sub_dir, - }, - ); - var found = false; - defer if (!found) allocator.free(dir_path); + result_buf.shrink(0); + try result_buf.outStream().print("{}\\Lib\\{}\\ucrt\\{}", .{ search.path, search.version, arch_sub_dir }); - var dir = fs.cwd().openDir(dir_path, .{}) catch |err| switch (err) { + var dir = fs.cwd().openDir(result_buf.span(), .{}) catch |err| switch (err) { error.FileNotFound, error.NotDir, error.NoDevice, @@ -409,8 +395,7 @@ pub const LibCInstallation = struct { else => return error.FileSystem, }; - found = true; - self.crt_dir = dir_path; + self.crt_dir = result_buf.toOwnedSlice(); return; } return error.LibCRuntimeNotFound; @@ -434,6 +419,10 @@ pub const LibCInstallation = struct { var search_buf: [2]Search = undefined; const searches = fillSearch(&search_buf, sdk); + + var result_buf = try std.ArrayList([]const u8).init(allocator); + defer result_buf.deinit(); + const arch_sub_dir = switch (builtin.arch) { .i386 => "x86", .x86_64 => "x64", @@ -442,20 +431,11 @@ pub const LibCInstallation = struct { }; for (searches) |search| { - const dir_path = try fs.path.join( - allocator, - &[_][]const u8{ - search.path, - "Lib", - search.version, - "um", - arch_sub_dir, - }, - ); - var found = false; - defer if (!found) allocator.free(dir_path); + result_buf.shrink(0); + const stream = result_buf.outStream(); + try stream.print("{}\\Lib\\{}\\um\\{}", .{ search.path, search.version, arch_sub_dir }); - var dir = fs.cwd().openDir(dir_path, .{}) catch |err| switch (err) { + var dir = fs.cwd().openDir(result_buf.span(), .{}) catch |err| switch (err) { error.FileNotFound, error.NotDir, error.NoDevice, @@ -470,8 +450,7 @@ pub const LibCInstallation = struct { else => return error.FileSystem, }; - found = true; - self.kernel32_lib_dir = dir_path; + self.kernel32_lib_dir = result_buf.toOwnedSlice(); return; } return error.LibCKernel32LibNotFound; @@ -489,13 +468,7 @@ pub const LibCInstallation = struct { const up1 = fs.path.dirname(msvc_lib_dir) orelse return error.LibCStdLibHeaderNotFound; const up2 = fs.path.dirname(up1) orelse return error.LibCStdLibHeaderNotFound; - const dir_path = try fs.path.join( - allocator, - &[_][]const u8{ - up2, - "include", - }, - ); + const dir_path = try fs.path.join(allocator, &[_][]const u8{ up2, "include" }); errdefer allocator.free(dir_path); var dir = fs.cwd().openDir(dir_path, .{}) catch |err| switch (err) { diff --git a/src-self-hosted/stage2.zig b/src-self-hosted/stage2.zig index 771765bee6..02213464e6 100644 --- a/src-self-hosted/stage2.zig +++ b/src-self-hosted/stage2.zig @@ -413,11 +413,11 @@ fn printErrMsgToFile( var text_buf = std.ArrayList(u8).init(allocator); defer text_buf.deinit(); - const out_stream = &text_buf.outStream(); + const out_stream = text_buf.outStream(); try parse_error.render(&tree.tokens, out_stream); const text = text_buf.span(); - const stream = &file.outStream(); + const stream = file.outStream(); try stream.print("{}:{}:{}: error: {}\n", .{ path, start_loc.line + 1, start_loc.column + 1, text }); if (!color_on) return; diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index 62318a40aa..854037ec57 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -297,7 +297,6 @@ pub fn translate( }; var source_buffer = std.ArrayList(u8).init(arena); - errdefer source_buffer.deinit(); var context = Context{ .tree = tree, diff --git a/src/codegen.cpp b/src/codegen.cpp index 2c2bf7c36b..24e77e8689 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -9123,17 +9123,20 @@ static void detect_libc(CodeGen *g) { g->libc_include_dir_len = 0; g->libc_include_dir_list = heap::c_allocator.allocate(dir_count); - g->libc_include_dir_list[g->libc_include_dir_len] = buf_ptr(buf_create_from_mem(g->libc->include_dir, g->libc->include_dir_len)); + g->libc_include_dir_list[g->libc_include_dir_len] = buf_ptr(buf_create_from_mem( + g->libc->include_dir, g->libc->include_dir_len)); g->libc_include_dir_len += 1; if (want_sys_dir) { - g->libc_include_dir_list[g->libc_include_dir_len] = buf_ptr(buf_create_from_mem(g->libc->sys_include_dir, g->libc->sys_include_dir_len)); + g->libc_include_dir_list[g->libc_include_dir_len] = buf_ptr(buf_create_from_mem( + g->libc->sys_include_dir, g->libc->sys_include_dir_len)); g->libc_include_dir_len += 1; } if (want_um_and_shared_dirs != 0) { Buf *include_dir_parent = buf_alloc(); - os_path_join(buf_create_from_mem(g->libc->include_dir, g->libc->include_dir_len), buf_create_from_str(".."), include_dir_parent); + os_path_join(buf_create_from_mem(g->libc->include_dir, g->libc->include_dir_len), + buf_create_from_str(".."), include_dir_parent); Buf *buff1 = buf_alloc(); os_path_join(include_dir_parent, buf_create_from_str("um"), buff1); diff --git a/src/link.cpp b/src/link.cpp index 49bbb7c1a5..fba572de98 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -1595,7 +1595,8 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr } else { assert(parent->libc != nullptr); Buf *out_buf = buf_alloc(); - os_path_join(buf_create_from_mem(parent->libc->crt_dir, parent->libc->crt_dir_len), buf_create_from_str(file), out_buf); + os_path_join(buf_create_from_mem(parent->libc->crt_dir, parent->libc->crt_dir_len), + buf_create_from_str(file), out_buf); return buf_ptr(out_buf); } }