Skip to content

Commit 553f0e0

Browse files
committed
fixups and revert a few things
1 parent 7eb938c commit 553f0e0

File tree

9 files changed

+55
-83
lines changed

9 files changed

+55
-83
lines changed

lib/std/array_list.zig

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -189,32 +189,30 @@ pub fn AlignedArrayList(comptime T: type, comptime alignment: ?u29) type {
189189
self.len += items.len;
190190
}
191191

192-
pub usingnamespace if (T == u8)
193-
struct {
194-
/// Same as `append` except it returns the number of bytes written, which is always the same
195-
/// as `m.len`. The purpose of this function existing is to match `std.io.OutStream` API.
196-
fn appendWrite(self: *Self, m: []const u8) !usize {
197-
try self.appendSlice(m);
198-
return m.len;
199-
}
200-
201-
pub fn outStream(self: *Self) std.io.OutStream(*Self, error{OutOfMemory}, appendWrite) {
202-
return .{ .context = self };
203-
}
204-
}
205-
else
206-
struct {};
192+
/// Same as `append` except it returns the number of bytes written, which is always the same
193+
/// as `m.len`. The purpose of this function existing is to match `std.io.OutStream` API.
194+
/// This function may be called only when `T` is `u8`.
195+
fn appendWrite(self: *Self, m: []const u8) !usize {
196+
try self.appendSlice(m);
197+
return m.len;
198+
}
199+
200+
/// Initializes an OutStream which will append to the list.
201+
/// This function may be called only when `T` is `u8`.
202+
pub fn outStream(self: *Self) std.io.OutStream(*Self, error{OutOfMemory}, appendWrite) {
203+
return .{ .context = self };
204+
}
207205

208-
/// Append a value to the list `n` times. Allocates more memory
209-
/// as necessary.
206+
/// Append a value to the list `n` times.
207+
/// Allocates more memory as necessary.
210208
pub fn appendNTimes(self: *Self, value: T, n: usize) !void {
211209
const old_len = self.len;
212210
try self.resize(self.len + n);
213211
mem.set(T, self.items[old_len..self.len], value);
214212
}
215213

216-
/// Adjust the list's length to `new_len`. Doesn't initialize
217-
/// added items if any.
214+
/// Adjust the list's length to `new_len`.
215+
/// Does not initialize added items if any.
218216
pub fn resize(self: *Self, new_len: usize) !void {
219217
try self.ensureCapacity(new_len);
220218
self.len = new_len;

lib/std/build.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1961,7 +1961,6 @@ pub const LibExeObjStep = struct {
19611961
}
19621962
} else {
19631963
var mcpu_buffer = std.ArrayList(u8).init(builder.allocator);
1964-
errdefer mcpu_buffer.deinit();
19651964

19661965
try mcpu_buffer.outStream().print("-mcpu={}", .{cross.cpu.model.name});
19671966

lib/std/child_process.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,6 @@ fn windowsCreateProcess(app_name: [*:0]u16, cmd_line: [*:0]u16, envp_ptr: ?[*]u1
757757
}
758758

759759
/// Caller must dealloc.
760-
/// Guarantees a null byte at result[result.len].
761760
fn windowsCreateCommandLine(allocator: *mem.Allocator, argv: []const []const u8) ![:0]u8 {
762761
var buf = try Buffer.initSize(allocator, 0);
763762
defer buf.deinit();

lib/std/zig/cross_target.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ pub const CrossTarget = struct {
504504
const os_name = if (self.os_tag) |os_tag| @tagName(os_tag) else "native";
505505

506506
var result = std.ArrayList(u8).init(allocator);
507-
errdefer result.deinit();
507+
defer result.deinit();
508508

509509
try result.outStream().print("{}-{}", .{ arch_name, os_name });
510510

src-self-hosted/libc_installation.zig

Lines changed: 27 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -327,20 +327,14 @@ pub const LibCInstallation = struct {
327327
var search_buf: [2]Search = undefined;
328328
const searches = fillSearch(&search_buf, sdk);
329329

330+
var result_buf = std.ArrayList([]const u8).init(allocator);
331+
defer result_buf.deinit();
332+
330333
for (searches) |search| {
331-
const dir_path = try fs.path.join(
332-
allocator,
333-
&[_][]const u8{
334-
search.path,
335-
"Include",
336-
search.version,
337-
"ucrt",
338-
},
339-
);
340-
var found = false;
341-
defer if (!found) allocator.free(dir_path);
342-
343-
var dir = fs.cwd().openDir(dir_path, .{}) catch |err| switch (err) {
334+
result_buf.shrink(0);
335+
try result_buf.outStream().print("{}\\Include\\{}\\ucrt", .{ search.path, search.version });
336+
337+
var dir = fs.cwd().openDir(result_buf.span(), .{}) catch |err| switch (err) {
344338
error.FileNotFound,
345339
error.NotDir,
346340
error.NoDevice,
@@ -355,8 +349,7 @@ pub const LibCInstallation = struct {
355349
else => return error.FileSystem,
356350
};
357351

358-
found = true;
359-
self.include_dir = dir_path;
352+
self.include_dir = result_buf.toOwnedSlice();
360353
return;
361354
}
362355

@@ -373,6 +366,9 @@ pub const LibCInstallation = struct {
373366
var search_buf: [2]Search = undefined;
374367
const searches = fillSearch(&search_buf, sdk);
375368

369+
var result_buf = try std.ArrayList([]const u8).init(allocator);
370+
defer result_buf.deinit();
371+
376372
const arch_sub_dir = switch (builtin.arch) {
377373
.i386 => "x86",
378374
.x86_64 => "x64",
@@ -381,20 +377,10 @@ pub const LibCInstallation = struct {
381377
};
382378

383379
for (searches) |search| {
384-
const dir_path = try fs.path.join(
385-
allocator,
386-
&[_][]const u8{
387-
search.path,
388-
"Lib",
389-
search.version,
390-
"ucrt",
391-
arch_sub_dir,
392-
},
393-
);
394-
var found = false;
395-
defer if (!found) allocator.free(dir_path);
396-
397-
var dir = fs.cwd().openDir(dir_path, .{}) catch |err| switch (err) {
380+
result_buf.shrink(0);
381+
try result_buf.outStream().print("{}\\Lib\\{}\\ucrt\\{}", .{ search.path, search.version, arch_sub_dir });
382+
383+
var dir = fs.cwd().openDir(result_buf.span(), .{}) catch |err| switch (err) {
398384
error.FileNotFound,
399385
error.NotDir,
400386
error.NoDevice,
@@ -409,8 +395,7 @@ pub const LibCInstallation = struct {
409395
else => return error.FileSystem,
410396
};
411397

412-
found = true;
413-
self.crt_dir = dir_path;
398+
self.crt_dir = result_buf.toOwnedSlice();
414399
return;
415400
}
416401
return error.LibCRuntimeNotFound;
@@ -434,6 +419,10 @@ pub const LibCInstallation = struct {
434419

435420
var search_buf: [2]Search = undefined;
436421
const searches = fillSearch(&search_buf, sdk);
422+
423+
var result_buf = try std.ArrayList([]const u8).init(allocator);
424+
defer result_buf.deinit();
425+
437426
const arch_sub_dir = switch (builtin.arch) {
438427
.i386 => "x86",
439428
.x86_64 => "x64",
@@ -442,20 +431,11 @@ pub const LibCInstallation = struct {
442431
};
443432

444433
for (searches) |search| {
445-
const dir_path = try fs.path.join(
446-
allocator,
447-
&[_][]const u8{
448-
search.path,
449-
"Lib",
450-
search.version,
451-
"um",
452-
arch_sub_dir,
453-
},
454-
);
455-
var found = false;
456-
defer if (!found) allocator.free(dir_path);
457-
458-
var dir = fs.cwd().openDir(dir_path, .{}) catch |err| switch (err) {
434+
result_buf.shrink(0);
435+
const stream = result_buf.outStream();
436+
try stream.print("{}\\Lib\\{}\\um\\{}", .{ search.path, search.version, arch_sub_dir });
437+
438+
var dir = fs.cwd().openDir(result_buf.span(), .{}) catch |err| switch (err) {
459439
error.FileNotFound,
460440
error.NotDir,
461441
error.NoDevice,
@@ -470,8 +450,7 @@ pub const LibCInstallation = struct {
470450
else => return error.FileSystem,
471451
};
472452

473-
found = true;
474-
self.kernel32_lib_dir = dir_path;
453+
self.kernel32_lib_dir = result_buf.toOwnedSlice();
475454
return;
476455
}
477456
return error.LibCKernel32LibNotFound;
@@ -489,13 +468,7 @@ pub const LibCInstallation = struct {
489468
const up1 = fs.path.dirname(msvc_lib_dir) orelse return error.LibCStdLibHeaderNotFound;
490469
const up2 = fs.path.dirname(up1) orelse return error.LibCStdLibHeaderNotFound;
491470

492-
const dir_path = try fs.path.join(
493-
allocator,
494-
&[_][]const u8{
495-
up2,
496-
"include",
497-
},
498-
);
471+
const dir_path = try fs.path.join(allocator, &[_][]const u8{ up2, "include" });
499472
errdefer allocator.free(dir_path);
500473

501474
var dir = fs.cwd().openDir(dir_path, .{}) catch |err| switch (err) {

src-self-hosted/stage2.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,11 @@ fn printErrMsgToFile(
413413

414414
var text_buf = std.ArrayList(u8).init(allocator);
415415
defer text_buf.deinit();
416-
const out_stream = &text_buf.outStream();
416+
const out_stream = text_buf.outStream();
417417
try parse_error.render(&tree.tokens, out_stream);
418418
const text = text_buf.span();
419419

420-
const stream = &file.outStream();
420+
const stream = file.outStream();
421421
try stream.print("{}:{}:{}: error: {}\n", .{ path, start_loc.line + 1, start_loc.column + 1, text });
422422

423423
if (!color_on) return;

src-self-hosted/translate_c.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,6 @@ pub fn translate(
297297
};
298298

299299
var source_buffer = std.ArrayList(u8).init(arena);
300-
errdefer source_buffer.deinit();
301300

302301
var context = Context{
303302
.tree = tree,

src/codegen.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9123,17 +9123,20 @@ static void detect_libc(CodeGen *g) {
91239123
g->libc_include_dir_len = 0;
91249124
g->libc_include_dir_list = heap::c_allocator.allocate<const char *>(dir_count);
91259125

9126-
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));
9126+
g->libc_include_dir_list[g->libc_include_dir_len] = buf_ptr(buf_create_from_mem(
9127+
g->libc->include_dir, g->libc->include_dir_len));
91279128
g->libc_include_dir_len += 1;
91289129

91299130
if (want_sys_dir) {
9130-
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));
9131+
g->libc_include_dir_list[g->libc_include_dir_len] = buf_ptr(buf_create_from_mem(
9132+
g->libc->sys_include_dir, g->libc->sys_include_dir_len));
91319133
g->libc_include_dir_len += 1;
91329134
}
91339135

91349136
if (want_um_and_shared_dirs != 0) {
91359137
Buf *include_dir_parent = buf_alloc();
9136-
os_path_join(buf_create_from_mem(g->libc->include_dir, g->libc->include_dir_len), buf_create_from_str(".."), include_dir_parent);
9138+
os_path_join(buf_create_from_mem(g->libc->include_dir, g->libc->include_dir_len),
9139+
buf_create_from_str(".."), include_dir_parent);
91379140

91389141
Buf *buff1 = buf_alloc();
91399142
os_path_join(include_dir_parent, buf_create_from_str("um"), buff1);

src/link.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,8 @@ static const char *get_libc_crt_file(CodeGen *parent, const char *file, Stage2Pr
15951595
} else {
15961596
assert(parent->libc != nullptr);
15971597
Buf *out_buf = buf_alloc();
1598-
os_path_join(buf_create_from_mem(parent->libc->crt_dir, parent->libc->crt_dir_len), buf_create_from_str(file), out_buf);
1598+
os_path_join(buf_create_from_mem(parent->libc->crt_dir, parent->libc->crt_dir_len),
1599+
buf_create_from_str(file), out_buf);
15991600
return buf_ptr(out_buf);
16001601
}
16011602
}

0 commit comments

Comments
 (0)