aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2025-12-12 19:04:35 -0800
committerAndrew Kelley <andrew@ziglang.org>2025-12-23 22:15:09 -0800
commit1925e0319f1337b4856bd5a181bf4f6d3ac7d428 (patch)
treeb38839cca2604368b01b4312119083a5cdf3d224 /src
parentec56696503e702e063af8740a77376b2e7694c29 (diff)
downloadzig-1925e0319f1337b4856bd5a181bf4f6d3ac7d428.tar.gz
zig-1925e0319f1337b4856bd5a181bf4f6d3ac7d428.zip
update lockStderrWriter sites
use the application's Io implementation where possible. This correctly makes writing to stderr cancelable, fallible, and participate in the application's event loop. It also removes one more hard-coded dependency on a secondary Io implementation.
Diffstat (limited to 'src')
-rw-r--r--src/Air/print.zig28
-rw-r--r--src/Compilation.zig53
-rw-r--r--src/InternPool.zig63
-rw-r--r--src/Sema.zig8
-rw-r--r--src/Zcu/PerThread.zig5
-rw-r--r--src/codegen/aarch64/Select.zig11
-rw-r--r--src/crash_report.zig9
-rw-r--r--src/fmt.zig8
-rw-r--r--src/libs/mingw.zig29
-rw-r--r--src/libs/mingw/def.zig32
-rw-r--r--src/link.zig2
-rw-r--r--src/link/Coff.zig15
-rw-r--r--src/link/Elf2.zig14
-rw-r--r--src/main.zig33
14 files changed, 184 insertions, 126 deletions
diff --git a/src/Air/print.zig b/src/Air/print.zig
index 95c8a1fcda..05614a0ed3 100644
--- a/src/Air/print.zig
+++ b/src/Air/print.zig
@@ -9,7 +9,7 @@ const Type = @import("../Type.zig");
const Air = @import("../Air.zig");
const InternPool = @import("../InternPool.zig");
-pub fn write(air: Air, stream: *std.Io.Writer, pt: Zcu.PerThread, liveness: ?Air.Liveness) void {
+pub fn write(air: Air, stream: *std.Io.Writer, pt: Zcu.PerThread, liveness: ?Air.Liveness) !void {
comptime assert(build_options.enable_debug_extensions);
const instruction_bytes = air.instructions.len *
// Here we don't use @sizeOf(Air.Inst.Data) because it would include
@@ -24,7 +24,7 @@ pub fn write(air: Air, stream: *std.Io.Writer, pt: Zcu.PerThread, liveness: ?Air
liveness_special_bytes + tomb_bytes;
// zig fmt: off
- stream.print(
+ try stream.print(
\\# Total AIR+Liveness bytes: {Bi}
\\# AIR Instructions: {d} ({Bi})
\\# AIR Extra Data: {d} ({Bi})
@@ -39,7 +39,7 @@ pub fn write(air: Air, stream: *std.Io.Writer, pt: Zcu.PerThread, liveness: ?Air
tomb_bytes,
if (liveness) |l| l.extra.len else 0, liveness_extra_bytes,
if (liveness) |l| l.special.count() else 0, liveness_special_bytes,
- }) catch return;
+ });
// zig fmt: on
var writer: Writer = .{
@@ -50,7 +50,7 @@ pub fn write(air: Air, stream: *std.Io.Writer, pt: Zcu.PerThread, liveness: ?Air
.indent = 2,
.skip_body = false,
};
- writer.writeBody(stream, air.getMainBody()) catch return;
+ try writer.writeBody(stream, air.getMainBody());
}
pub fn writeInst(
@@ -73,15 +73,23 @@ pub fn writeInst(
}
pub fn dump(air: Air, pt: Zcu.PerThread, liveness: ?Air.Liveness) void {
- const stderr_bw, _ = std.debug.lockStderrWriter(&.{});
- defer std.debug.unlockStderrWriter();
- air.write(stderr_bw, pt, liveness);
+ const comp = pt.zcu.comp;
+ const io = comp.io;
+ var buffer: [512]u8 = undefined;
+ const stderr = try io.lockStderrWriter(&buffer);
+ defer io.unlockStderrWriter();
+ const w = &stderr.interface;
+ air.write(w, pt, liveness);
}
pub fn dumpInst(air: Air, inst: Air.Inst.Index, pt: Zcu.PerThread, liveness: ?Air.Liveness) void {
- const stderr_bw, _ = std.debug.lockStderrWriter(&.{});
- defer std.debug.unlockStderrWriter();
- air.writeInst(stderr_bw, inst, pt, liveness);
+ const comp = pt.zcu.comp;
+ const io = comp.io;
+ var buffer: [512]u8 = undefined;
+ const stderr = try io.lockStderrWriter(&buffer);
+ defer io.unlockStderrWriter();
+ const w = &stderr.interface;
+ air.writeInst(w, inst, pt, liveness);
}
const Writer = struct {
diff --git a/src/Compilation.zig b/src/Compilation.zig
index 9e76b8feca..47098317b5 100644
--- a/src/Compilation.zig
+++ b/src/Compilation.zig
@@ -2088,12 +2088,13 @@ pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic,
if (options.verbose_llvm_cpu_features) {
if (options.root_mod.resolved_target.llvm_cpu_features) |cf| print: {
- const stderr_w, _ = std.debug.lockStderrWriter(&.{});
- defer std.debug.unlockStderrWriter();
- stderr_w.print("compilation: {s}\n", .{options.root_name}) catch break :print;
- stderr_w.print(" target: {s}\n", .{try target.zigTriple(arena)}) catch break :print;
- stderr_w.print(" cpu: {s}\n", .{target.cpu.model.name}) catch break :print;
- stderr_w.print(" features: {s}\n", .{cf}) catch {};
+ const stderr = try io.lockStderrWriter(&.{});
+ defer io.unlockStderrWriter();
+ const w = &stderr.interface;
+ w.print("compilation: {s}\n", .{options.root_name}) catch break :print;
+ w.print(" target: {s}\n", .{try target.zigTriple(arena)}) catch break :print;
+ w.print(" cpu: {s}\n", .{target.cpu.model.name}) catch break :print;
+ w.print(" features: {s}\n", .{cf}) catch {};
}
}
@@ -4257,12 +4258,13 @@ pub fn getAllErrorsAlloc(comp: *Compilation) error{OutOfMemory}!ErrorBundle {
// However, we haven't reported any such error.
// This is a compiler bug.
print_ctx: {
- var stderr_w, _ = std.debug.lockStderrWriter(&.{});
- defer std.debug.unlockStderrWriter();
- stderr_w.writeAll("referenced transitive analysis errors, but none actually emitted\n") catch break :print_ctx;
- stderr_w.print("{f} [transitive failure]\n", .{zcu.fmtAnalUnit(failed_unit)}) catch break :print_ctx;
+ const stderr = try io.lockStderrWriter(&.{});
+ defer io.unlockStderrWriter();
+ const w = &stderr.interface;
+ w.writeAll("referenced transitive analysis errors, but none actually emitted\n") catch break :print_ctx;
+ w.print("{f} [transitive failure]\n", .{zcu.fmtAnalUnit(failed_unit)}) catch break :print_ctx;
while (ref) |r| {
- stderr_w.print("referenced by: {f}{s}\n", .{
+ w.print("referenced by: {f}{s}\n", .{
zcu.fmtAnalUnit(r.referencer),
if (zcu.transitive_failed_analysis.contains(r.referencer)) " [transitive failure]" else "",
}) catch break :print_ctx;
@@ -5756,7 +5758,7 @@ pub fn translateC(
try argv.appendSlice(comp.global_cc_argv);
try argv.appendSlice(owner_mod.cc_argv);
try argv.appendSlice(&.{ source_path, "-o", translated_path });
- if (comp.verbose_cimport) dump_argv(argv.items);
+ if (comp.verbose_cimport) dumpArgv(io, argv.items);
}
var stdout: []u8 = undefined;
@@ -6264,7 +6266,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr
}
if (comp.verbose_cc) {
- dump_argv(argv.items);
+ dumpArgv(io, argv.items);
}
const err = std.process.execv(arena, argv.items);
@@ -6310,7 +6312,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr
}
if (comp.verbose_cc) {
- dump_argv(argv.items);
+ dumpArgv(io, argv.items);
}
// Just to save disk space, we delete the files that are never needed again.
@@ -7773,17 +7775,22 @@ pub fn lockAndSetMiscFailure(
return setMiscFailure(comp, tag, format, args);
}
-pub fn dump_argv(argv: []const []const u8) void {
+pub fn dumpArgv(io: Io, argv: []const []const u8) Io.Cancelable!void {
var buffer: [64]u8 = undefined;
- const stderr, _ = std.debug.lockStderrWriter(&buffer);
- defer std.debug.unlockStderrWriter();
- nosuspend {
- for (argv, 0..) |arg, i| {
- if (i != 0) stderr.writeByte(' ') catch return;
- stderr.writeAll(arg) catch return;
- }
- stderr.writeByte('\n') catch return;
+ const stderr = try io.lockStderrWriter(&buffer);
+ defer io.unlockStderrWriter();
+ const w = &stderr.interface;
+ return dumpArgvWriter(w, argv) catch |err| switch (err) {
+ error.WriteFailed => return stderr.err.?,
+ };
+}
+
+fn dumpArgvWriter(w: *Io.Writer, argv: []const []const u8) Io.Writer.Error!void {
+ for (argv, 0..) |arg, i| {
+ if (i != 0) try w.writeByte(' ');
+ try w.writeAll(arg);
}
+ try w.writeByte('\n');
}
pub fn getZigBackend(comp: Compilation) std.builtin.CompilerBackend {
diff --git a/src/InternPool.zig b/src/InternPool.zig
index 5568c493d9..539cb441c5 100644
--- a/src/InternPool.zig
+++ b/src/InternPool.zig
@@ -1,8 +1,11 @@
//! All interned objects have both a value and a type.
//! This data structure is self-contained.
+const InternPool = @This();
const builtin = @import("builtin");
+
const std = @import("std");
+const Io = std.Io;
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const BigIntConst = std.math.big.int.Const;
@@ -11,10 +14,9 @@ const Cache = std.Build.Cache;
const Io = std.Io;
const Limb = std.math.big.Limb;
const Hash = std.hash.Wyhash;
+const Zir = std.zig.Zir;
-const InternPool = @This();
const Zcu = @import("Zcu.zig");
-const Zir = std.zig.Zir;
/// One item per thread, indexed by `tid`, which is dense and unique per thread.
locals: []Local,
@@ -11165,12 +11167,16 @@ pub fn mutateVarInit(ip: *InternPool, io: Io, index: Index, init_index: Index) v
@atomicStore(u32, &extra_items[item.data + std.meta.fieldIndex(Tag.Variable, "init").?], @intFromEnum(init_index), .release);
}
-pub fn dump(ip: *const InternPool) void {
- dumpStatsFallible(ip, std.heap.page_allocator) catch return;
- dumpAllFallible(ip) catch return;
+pub fn dump(ip: *const InternPool, io: Io) Io.Cancelable!void {
+ var buffer: [4096]u8 = undefined;
+ const stderr_writer = try io.lockStderrWriter(&buffer);
+ defer io.unlockStderrWriter();
+ const w = &stderr_writer.interface;
+ try dumpStatsFallible(ip, w, std.heap.page_allocator);
+ try dumpAllFallible(ip, w);
}
-fn dumpStatsFallible(ip: *const InternPool, arena: Allocator) anyerror!void {
+fn dumpStatsFallible(ip: *const InternPool, w: *Io.Writer, arena: Allocator) anyerror!void {
var items_len: usize = 0;
var extra_len: usize = 0;
var limbs_len: usize = 0;
@@ -11423,18 +11429,13 @@ fn dumpStatsFallible(ip: *const InternPool, arena: Allocator) anyerror!void {
};
counts.sort(SortContext{ .map = &counts });
const len = @min(50, counts.count());
- std.debug.print(" top 50 tags:\n", .{});
+ w.print(" top 50 tags:\n", .{});
for (counts.keys()[0..len], counts.values()[0..len]) |tag, stats| {
- std.debug.print(" {s}: {d} occurrences, {d} total bytes\n", .{
- @tagName(tag), stats.count, stats.bytes,
- });
+ w.print(" {t}: {d} occurrences, {d} total bytes\n", .{ tag, stats.count, stats.bytes });
}
}
-fn dumpAllFallible(ip: *const InternPool) anyerror!void {
- var buffer: [4096]u8 = undefined;
- const stderr_bw, _ = std.debug.lockStderrWriter(&buffer);
- defer std.debug.unlockStderrWriter();
+fn dumpAllFallible(ip: *const InternPool, w: *Io.Writer) anyerror!void {
for (ip.locals, 0..) |*local, tid| {
const items = local.shared.items.view();
for (
@@ -11443,12 +11444,12 @@ fn dumpAllFallible(ip: *const InternPool) anyerror!void {
0..,
) |tag, data, index| {
const i = Index.Unwrapped.wrap(.{ .tid = @enumFromInt(tid), .index = @intCast(index) }, ip);
- try stderr_bw.print("${d} = {s}(", .{ i, @tagName(tag) });
+ try w.print("${d} = {s}(", .{ i, @tagName(tag) });
switch (tag) {
.removed => {},
- .simple_type => try stderr_bw.print("{s}", .{@tagName(@as(SimpleType, @enumFromInt(@intFromEnum(i))))}),
- .simple_value => try stderr_bw.print("{s}", .{@tagName(@as(SimpleValue, @enumFromInt(@intFromEnum(i))))}),
+ .simple_type => try w.print("{s}", .{@tagName(@as(SimpleType, @enumFromInt(@intFromEnum(i))))}),
+ .simple_value => try w.print("{s}", .{@tagName(@as(SimpleValue, @enumFromInt(@intFromEnum(i))))}),
.type_int_signed,
.type_int_unsigned,
@@ -11521,23 +11522,27 @@ fn dumpAllFallible(ip: *const InternPool) anyerror!void {
.func_coerced,
.union_value,
.memoized_call,
- => try stderr_bw.print("{d}", .{data}),
+ => try w.print("{d}", .{data}),
.opt_null,
.type_slice,
.only_possible_value,
- => try stderr_bw.print("${d}", .{data}),
+ => try w.print("${d}", .{data}),
}
- try stderr_bw.writeAll(")\n");
+ try w.writeAll(")\n");
}
}
}
-pub fn dumpGenericInstances(ip: *const InternPool, allocator: Allocator) void {
- ip.dumpGenericInstancesFallible(allocator) catch return;
+pub fn dumpGenericInstances(ip: *const InternPool, io: Io, allocator: Allocator) Io.Cancelable!void {
+ var buffer: [4096]u8 = undefined;
+ const stderr_writer = try io.lockStderrWriter(&buffer);
+ defer io.unlockStderrWriter();
+ const w = &stderr_writer.interface;
+ try ip.dumpGenericInstancesFallible(allocator, w);
}
-pub fn dumpGenericInstancesFallible(ip: *const InternPool, allocator: Allocator) anyerror!void {
+pub fn dumpGenericInstancesFallible(ip: *const InternPool, allocator: Allocator, w: *Io.Writer) !void {
var arena_allocator = std.heap.ArenaAllocator.init(allocator);
defer arena_allocator.deinit();
const arena = arena_allocator.allocator();
@@ -11564,10 +11569,6 @@ pub fn dumpGenericInstancesFallible(ip: *const InternPool, allocator: Allocator)
}
}
- var buffer: [4096]u8 = undefined;
- const stderr_bw, _ = std.debug.lockStderrWriter(&buffer);
- defer std.debug.unlockStderrWriter();
-
const SortContext = struct {
values: []std.ArrayList(Index),
pub fn lessThan(ctx: @This(), a_index: usize, b_index: usize) bool {
@@ -11579,19 +11580,19 @@ pub fn dumpGenericInstancesFallible(ip: *const InternPool, allocator: Allocator)
var it = instances.iterator();
while (it.next()) |entry| {
const generic_fn_owner_nav = ip.getNav(ip.funcDeclInfo(entry.key_ptr.*).owner_nav);
- try stderr_bw.print("{f} ({d}): \n", .{ generic_fn_owner_nav.name.fmt(ip), entry.value_ptr.items.len });
+ try w.print("{f} ({d}): \n", .{ generic_fn_owner_nav.name.fmt(ip), entry.value_ptr.items.len });
for (entry.value_ptr.items) |index| {
const unwrapped_index = index.unwrap(ip);
const func = ip.extraFuncInstance(unwrapped_index.tid, unwrapped_index.getExtra(ip), unwrapped_index.getData(ip));
const owner_nav = ip.getNav(func.owner_nav);
- try stderr_bw.print(" {f}: (", .{owner_nav.name.fmt(ip)});
+ try w.print(" {f}: (", .{owner_nav.name.fmt(ip)});
for (func.comptime_args.get(ip)) |arg| {
if (arg != .none) {
const key = ip.indexToKey(arg);
- try stderr_bw.print(" {} ", .{key});
+ try w.print(" {} ", .{key});
}
}
- try stderr_bw.writeAll(")\n");
+ try w.writeAll(")\n");
}
}
}
diff --git a/src/Sema.zig b/src/Sema.zig
index fec6850c4c..1f6a577f60 100644
--- a/src/Sema.zig
+++ b/src/Sema.zig
@@ -2668,16 +2668,18 @@ fn failWithTypeMismatch(sema: *Sema, block: *Block, src: LazySrcLoc, expected: T
pub fn failWithOwnedErrorMsg(sema: *Sema, block: ?*Block, err_msg: *Zcu.ErrorMsg) error{ AnalysisFail, OutOfMemory } {
@branchHint(.cold);
- const gpa = sema.gpa;
const zcu = sema.pt.zcu;
+ const comp = zcu.comp;
+ const gpa = comp.gpa;
+ const io = comp.io;
- if (build_options.enable_debug_extensions and zcu.comp.debug_compile_errors) {
+ if (build_options.enable_debug_extensions and comp.debug_compile_errors) {
var wip_errors: std.zig.ErrorBundle.Wip = undefined;
wip_errors.init(gpa) catch @panic("out of memory");
Compilation.addModuleErrorMsg(zcu, &wip_errors, err_msg.*, false) catch @panic("out of memory");
std.debug.print("compile error during Sema:\n", .{});
var error_bundle = wip_errors.toOwnedBundle("") catch @panic("out of memory");
- error_bundle.renderToStdErr(.{}, .auto);
+ error_bundle.renderToStderr(io, .{}, .auto);
std.debug.panicExtra(@returnAddress(), "unexpected compile error occurred", .{});
}
diff --git a/src/Zcu/PerThread.zig b/src/Zcu/PerThread.zig
index 2333d7f286..408f16bd74 100644
--- a/src/Zcu/PerThread.zig
+++ b/src/Zcu/PerThread.zig
@@ -4556,8 +4556,9 @@ fn runCodegenInner(pt: Zcu.PerThread, func_index: InternPool.Index, air: *Air) e
defer if (liveness) |*l| l.deinit(gpa);
if (build_options.enable_debug_extensions and comp.verbose_air) {
- const stderr, _ = std.debug.lockStderrWriter(&.{});
- defer std.debug.unlockStderrWriter();
+ const io = comp.io;
+ const stderr = try io.lockStderrWriter(&.{});
+ defer io.unlockStderrWriter();
stderr.print("# Begin Function AIR: {f}:\n", .{fqn.fmt(ip)}) catch {};
air.write(stderr, pt, liveness);
stderr.print("# End Function AIR: {f}\n\n", .{fqn.fmt(ip)}) catch {};
diff --git a/src/codegen/aarch64/Select.zig b/src/codegen/aarch64/Select.zig
index f390d83f03..138c70fecf 100644
--- a/src/codegen/aarch64/Select.zig
+++ b/src/codegen/aarch64/Select.zig
@@ -11273,15 +11273,18 @@ fn initValueAdvanced(
return @enumFromInt(isel.values.items.len);
}
pub fn dumpValues(isel: *Select, which: enum { only_referenced, all }) void {
- errdefer |err| @panic(@errorName(err));
- const stderr, _ = std.debug.lockStderrWriter(&.{});
- defer std.debug.unlockStderrWriter();
-
const zcu = isel.pt.zcu;
+ const io = zcu.comp.io;
const gpa = zcu.gpa;
const ip = &zcu.intern_pool;
const nav = ip.getNav(isel.nav_index);
+ errdefer |err| @panic(@errorName(err));
+
+ const stderr_writer = io.lockStderrWriter(&.{}) catch return;
+ defer io.unlockStderrWriter();
+ const stderr = &stderr_writer.interface;
+
var reverse_live_values: std.AutoArrayHashMapUnmanaged(Value.Index, std.ArrayList(Air.Inst.Index)) = .empty;
defer {
for (reverse_live_values.values()) |*list| list.deinit(gpa);
diff --git a/src/crash_report.zig b/src/crash_report.zig
index 76503fdc53..f23806b758 100644
--- a/src/crash_report.zig
+++ b/src/crash_report.zig
@@ -97,17 +97,18 @@ fn dumpCrashContext() Io.Writer.Error!void {
// and the actual panic printing, which would be quite confusing.
const stderr = std.debug.lockStderrWriter(&.{});
defer std.debug.unlockStderrWriter();
+ const w = &stderr.interface;
- try stderr.interface.writeAll("Compiler crash context:\n");
+ try w.writeAll("Compiler crash context:\n");
if (CodegenFunc.current) |*cg| {
const func_nav = cg.zcu.funcInfo(cg.func_index).owner_nav;
const func_fqn = cg.zcu.intern_pool.getNav(func_nav).fqn;
- try stderr.interface.print("Generating function '{f}'\n\n", .{func_fqn.fmt(&cg.zcu.intern_pool)});
+ try w.print("Generating function '{f}'\n\n", .{func_fqn.fmt(&cg.zcu.intern_pool)});
} else if (AnalyzeBody.current) |anal| {
- try dumpCrashContextSema(anal, &stderr.interface, &S.crash_heap);
+ try dumpCrashContextSema(anal, w, &S.crash_heap);
} else {
- try stderr.interface.writeAll("(no context)\n\n");
+ try w.writeAll("(no context)\n\n");
}
}
fn dumpCrashContextSema(anal: *AnalyzeBody, stderr: *Io.Writer, crash_heap: []u8) Io.Writer.Error!void {
diff --git a/src/fmt.zig b/src/fmt.zig
index 55a68d1dc8..b9b1cc1363 100644
--- a/src/fmt.zig
+++ b/src/fmt.zig
@@ -124,7 +124,7 @@ pub fn run(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !
try wip_errors.addZirErrorMessages(zir, tree, source_code, "<stdin>");
var error_bundle = try wip_errors.toOwnedBundle("");
defer error_bundle.deinit(gpa);
- error_bundle.renderToStdErr(.{}, color);
+ error_bundle.renderToStderr(io, .{}, color);
process.exit(2);
}
} else {
@@ -138,7 +138,7 @@ pub fn run(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8) !
try wip_errors.addZoirErrorMessages(zoir, tree, source_code, "<stdin>");
var error_bundle = try wip_errors.toOwnedBundle("");
defer error_bundle.deinit(gpa);
- error_bundle.renderToStdErr(.{}, color);
+ error_bundle.renderToStderr(io, .{}, color);
process.exit(2);
}
}
@@ -319,7 +319,7 @@ fn fmtPathFile(
try wip_errors.addZirErrorMessages(zir, tree, source_code, file_path);
var error_bundle = try wip_errors.toOwnedBundle("");
defer error_bundle.deinit(gpa);
- error_bundle.renderToStdErr(.{}, fmt.color);
+ error_bundle.renderToStderr(io, .{}, fmt.color);
fmt.any_error = true;
}
},
@@ -334,7 +334,7 @@ fn fmtPathFile(
try wip_errors.addZoirErrorMessages(zoir, tree, source_code, file_path);
var error_bundle = try wip_errors.toOwnedBundle("");
defer error_bundle.deinit(gpa);
- error_bundle.renderToStdErr(.{}, fmt.color);
+ error_bundle.renderToStderr(io, .{}, fmt.color);
fmt.any_error = true;
}
},
diff --git a/src/libs/mingw.zig b/src/libs/mingw.zig
index 05a9de71e7..a76fec9237 100644
--- a/src/libs/mingw.zig
+++ b/src/libs/mingw.zig
@@ -312,11 +312,17 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
const include_dir = try comp.dirs.zig_lib.join(arena, &.{ "libc", "mingw", "def-include" });
- if (comp.verbose_cc) print: {
- var stderr, _ = std.debug.lockStderrWriter(&.{});
- defer std.debug.unlockStderrWriter();
- nosuspend stderr.print("def file: {s}\n", .{def_file_path}) catch break :print;
- nosuspend stderr.print("include dir: {s}\n", .{include_dir}) catch break :print;
+ if (comp.verbose_cc) {
+ var buffer: [256]u8 = undefined;
+ const stderr = try io.lockStderrWriter(&buffer);
+ defer io.unlockStderrWriter();
+ const w = &stderr.interface;
+ w.print("def file: {s}\n", .{def_file_path}) catch |err| switch (err) {
+ error.WriteFailed => return stderr.err.?,
+ };
+ w.print("include dir: {s}\n", .{include_dir}) catch |err| switch (err) {
+ error.WriteFailed => return stderr.err.?,
+ };
}
try aro_comp.search_path.append(gpa, .{ .path = include_dir, .kind = .normal });
@@ -333,11 +339,13 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
if (aro_comp.diagnostics.output.to_list.messages.items.len != 0) {
var buffer: [64]u8 = undefined;
- const w, const ttyconf = std.debug.lockStderrWriter(&buffer);
- defer std.debug.unlockStderrWriter();
+ const stderr = try io.lockStderrWriter(&buffer);
+ defer io.unlockStderrWriter();
for (aro_comp.diagnostics.output.to_list.messages.items) |msg| {
if (msg.kind == .@"fatal error" or msg.kind == .@"error") {
- msg.write(w, ttyconf, true) catch {};
+ msg.write(&stderr.interface, stderr.mode, true) catch |err| switch (err) {
+ error.WriteFailed => return stderr.err.?,
+ };
return error.AroPreprocessorFailed;
}
}
@@ -357,8 +365,9 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
error.OutOfMemory => |e| return e,
error.ParseError => {
var buffer: [64]u8 = undefined;
- const w, _ = std.debug.lockStderrWriter(&buffer);
- defer std.debug.unlockStderrWriter();
+ const stderr = try io.lockStderrWriter(&buffer);
+ defer io.unlockStderrWriter();
+ const w = &stderr.interface;
try w.writeAll("error: ");
try def_diagnostics.writeMsg(w, input);
try w.writeByte('\n');
diff --git a/src/libs/mingw/def.zig b/src/libs/mingw/def.zig
index 24dc95c13c..9a105b6182 100644
--- a/src/libs/mingw/def.zig
+++ b/src/libs/mingw/def.zig
@@ -1,4 +1,5 @@
const std = @import("std");
+const Io = std.Io;
pub const ModuleDefinitionType = enum {
mingw,
@@ -663,7 +664,9 @@ test parse {
\\
;
- try testParse(.AMD64, source, "foo.dll", &[_]ModuleDefinition.Export{
+ const io = std.testing.io;
+
+ try testParse(io, .AMD64, source, "foo.dll", &[_]ModuleDefinition.Export{
.{
.name = "foo",
.mangled_symbol_name = null,
@@ -743,7 +746,7 @@ test parse {
},
});
- try testParse(.I386, source, "foo.dll", &[_]ModuleDefinition.Export{
+ try testParse(io, .I386, source, "foo.dll", &[_]ModuleDefinition.Export{
.{
.name = "_foo",
.mangled_symbol_name = null,
@@ -823,7 +826,7 @@ test parse {
},
});
- try testParse(.ARMNT, source, "foo.dll", &[_]ModuleDefinition.Export{
+ try testParse(io, .ARMNT, source, "foo.dll", &[_]ModuleDefinition.Export{
.{
.name = "foo",
.mangled_symbol_name = null,
@@ -903,7 +906,7 @@ test parse {
},
});
- try testParse(.ARM64, source, "foo.dll", &[_]ModuleDefinition.Export{
+ try testParse(io, .ARM64, source, "foo.dll", &[_]ModuleDefinition.Export{
.{
.name = "foo",
.mangled_symbol_name = null,
@@ -997,7 +1000,9 @@ test "ntdll" {
\\RtlActivateActivationContextUnsafeFast@0
;
- try testParse(.AMD64, source, "ntdll.dll", &[_]ModuleDefinition.Export{
+ const io = std.testing.io;
+
+ try testParse(io, .AMD64, source, "ntdll.dll", &[_]ModuleDefinition.Export{
.{
.name = "RtlDispatchAPC@12",
.mangled_symbol_name = null,
@@ -1023,15 +1028,22 @@ test "ntdll" {
});
}
-fn testParse(machine_type: std.coff.IMAGE.FILE.MACHINE, source: [:0]const u8, expected_module_name: []const u8, expected_exports: []const ModuleDefinition.Export) !void {
+fn testParse(
+ io: Io,
+ machine_type: std.coff.IMAGE.FILE.MACHINE,
+ source: [:0]const u8,
+ expected_module_name: []const u8,
+ expected_exports: []const ModuleDefinition.Export,
+) !void {
var diagnostics: Diagnostics = undefined;
const module = parse(std.testing.allocator, source, machine_type, .mingw, &diagnostics) catch |err| switch (err) {
error.OutOfMemory => |e| return e,
error.ParseError => {
- const stderr, _ = std.debug.lockStderrWriter(&.{});
- defer std.debug.unlockStderrWriter();
- try diagnostics.writeMsg(stderr, source);
- try stderr.writeByte('\n');
+ const stderr = try io.lockStderrWriter(&.{});
+ defer io.unlockStderrWriter();
+ const w = &stderr.interface;
+ try diagnostics.writeMsg(w, source);
+ try w.writeByte('\n');
return err;
},
};
diff --git a/src/link.zig b/src/link.zig
index e37ba98cd9..a4729f296c 100644
--- a/src/link.zig
+++ b/src/link.zig
@@ -2246,7 +2246,7 @@ fn resolvePathInputLib(
var error_bundle = try wip_errors.toOwnedBundle("");
defer error_bundle.deinit(gpa);
- error_bundle.renderToStdErr(.{}, color);
+ error_bundle.renderToStderr(io, .{}, color);
std.process.exit(1);
}
diff --git a/src/link/Coff.zig b/src/link/Coff.zig
index 8954266e3f..d379669613 100644
--- a/src/link/Coff.zig
+++ b/src/link/Coff.zig
@@ -4,6 +4,7 @@ const builtin = @import("builtin");
const native_endian = builtin.cpu.arch.endian();
const std = @import("std");
+const Io = std.Io;
const assert = std.debug.assert;
const log = std.log.scoped(.link);
@@ -2377,10 +2378,16 @@ pub fn deleteExport(coff: *Coff, exported: Zcu.Exported, name: InternPool.NullTe
_ = name;
}
-pub fn dump(coff: *Coff, tid: Zcu.PerThread.Id) void {
- const w, _ = std.debug.lockStderrWriter(&.{});
- defer std.debug.unlockStderrWriter();
- coff.printNode(tid, w, .root, 0) catch {};
+pub fn dump(coff: *Coff, tid: Zcu.PerThread.Id) Io.Cancelable!void {
+ const comp = coff.base.comp;
+ const io = comp.io;
+ var buffer: [512]u8 = undefined;
+ const stderr = try io.lockStderrWriter(&buffer);
+ defer io.unlockStderrWriter();
+ const w = &stderr.interface;
+ coff.printNode(tid, w, .root, 0) catch |err| switch (err) {
+ error.WriteFailed => return stderr.err.?,
+ };
}
pub fn printNode(
diff --git a/src/link/Elf2.zig b/src/link/Elf2.zig
index fc4794a7b0..3c511f1ee9 100644
--- a/src/link/Elf2.zig
+++ b/src/link/Elf2.zig
@@ -3729,10 +3729,16 @@ pub fn deleteExport(elf: *Elf, exported: Zcu.Exported, name: InternPool.NullTerm
_ = name;
}
-pub fn dump(elf: *Elf, tid: Zcu.PerThread.Id) void {
- const w, _ = std.debug.lockStderrWriter(&.{});
- defer std.debug.unlockStderrWriter();
- elf.printNode(tid, w, .root, 0) catch {};
+pub fn dump(elf: *Elf, tid: Zcu.PerThread.Id) Io.Cancelable!void {
+ const comp = elf.base.comp;
+ const io = comp.io;
+ var buffer: [512]u8 = undefined;
+ const stderr = try io.lockStderrWriter(&buffer);
+ defer io.unlockStderrWriter();
+ const w = &stderr.interface;
+ elf.printNode(tid, w, .root, 0) catch |err| switch (err) {
+ error.WriteFailed => return stderr.err.?,
+ };
}
pub fn printNode(
diff --git a/src/main.zig b/src/main.zig
index 115c1b5212..f735f671c6 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -4429,9 +4429,9 @@ fn runOrTest(
// the error message and invocation below.
if (process.can_execv and arg_mode == .run) {
// execv releases the locks; no need to destroy the Compilation here.
- _ = std.debug.lockStderrWriter(&.{});
+ _ = try io.lockStderrWriter(&.{});
const err = process.execve(gpa, argv.items, &env_map);
- std.debug.unlockStderrWriter();
+ io.unlockStderrWriter();
try warnAboutForeignBinaries(io, arena, arg_mode, target, link_libc);
const cmd = try std.mem.join(arena, " ", argv.items);
fatal("the following command failed to execve with '{t}':\n{s}", .{ err, cmd });
@@ -4448,8 +4448,8 @@ fn runOrTest(
comp_destroyed.* = true;
const term_result = t: {
- _ = std.debug.lockStderrWriter();
- defer std.debug.unlockStderrWriter();
+ _ = try io.lockStderrWriter(&.{});
+ defer io.unlockStderrWriter();
break :t child.spawnAndWait(io);
};
const term = term_result catch |err| {
@@ -4606,7 +4606,8 @@ fn updateModule(comp: *Compilation, color: Color, prog_node: std.Progress.Node)
defer errors.deinit(comp.gpa);
if (errors.errorMessageCount() > 0) {
- errors.renderToStdErr(.{}, color);
+ const io = comp.io;
+ errors.renderToStderr(io, .{}, color);
return error.CompileErrorsReported;
}
}
@@ -4659,7 +4660,7 @@ fn cmdTranslateC(
return;
} else {
const color: Color = .auto;
- result.errors.renderToStdErr(.{}, color);
+ result.errors.renderToStderr(io, .{}, color);
process.exit(1);
}
}
@@ -5280,7 +5281,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)
if (fetch.error_bundle.root_list.items.len > 0) {
var errors = try fetch.error_bundle.toOwnedBundle("");
- errors.renderToStdErr(.{}, color);
+ errors.renderToStderr(io, .{}, color);
process.exit(1);
}
@@ -5412,8 +5413,8 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, io: Io, args: []const []const u8)
child.stderr_behavior = .Inherit;
const term = t: {
- _ = std.debug.lockStderrWriter(&.{});
- defer std.debug.unlockStderrWriter();
+ _ = try io.lockStderrWriter(&.{});
+ defer io.unlockStderrWriter();
break :t child.spawnAndWait(io) catch |err|
fatal("failed to spawn build runner {s}: {t}", .{ child_argv.items[0], err });
};
@@ -6212,7 +6213,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {
try wip_errors.init(arena);
try wip_errors.addZirErrorMessages(zir, tree, source, display_path);
var error_bundle = try wip_errors.toOwnedBundle("");
- error_bundle.renderToStdErr(.{}, color);
+ error_bundle.renderToStderr(io, .{}, color);
if (zir.loweringFailed()) {
process.exit(1);
}
@@ -6283,7 +6284,7 @@ fn cmdAstCheck(arena: Allocator, io: Io, args: []const []const u8) !void {
try wip_errors.init(arena);
try wip_errors.addZoirErrorMessages(zoir, tree, source, display_path);
var error_bundle = try wip_errors.toOwnedBundle("");
- error_bundle.renderToStdErr(.{}, color);
+ error_bundle.renderToStderr(io, .{}, color);
process.exit(1);
}
@@ -6557,7 +6558,7 @@ fn cmdChangelist(arena: Allocator, io: Io, args: []const []const u8) !void {
try wip_errors.init(arena);
try wip_errors.addZirErrorMessages(old_zir, old_tree, old_source, old_source_path);
var error_bundle = try wip_errors.toOwnedBundle("");
- error_bundle.renderToStdErr(.{}, color);
+ error_bundle.renderToStderr(io, .{}, color);
process.exit(1);
}
@@ -6569,7 +6570,7 @@ fn cmdChangelist(arena: Allocator, io: Io, args: []const []const u8) !void {
try wip_errors.init(arena);
try wip_errors.addZirErrorMessages(new_zir, new_tree, new_source, new_source_path);
var error_bundle = try wip_errors.toOwnedBundle("");
- error_bundle.renderToStdErr(.{}, color);
+ error_bundle.renderToStderr(io, .{}, color);
process.exit(1);
}
@@ -7005,7 +7006,7 @@ fn cmdFetch(
if (fetch.error_bundle.root_list.items.len > 0) {
var errors = try fetch.error_bundle.toOwnedBundle("");
- errors.renderToStdErr(.{}, color);
+ errors.renderToStderr(io, .{}, color);
process.exit(1);
}
@@ -7345,7 +7346,7 @@ fn loadManifest(
errdefer ast.deinit(gpa);
if (ast.errors.len > 0) {
- try std.zig.printAstErrorsToStderr(gpa, ast, Package.Manifest.basename, options.color);
+ try std.zig.printAstErrorsToStderr(gpa, io, ast, Package.Manifest.basename, options.color);
process.exit(2);
}
@@ -7362,7 +7363,7 @@ fn loadManifest(
var error_bundle = try wip_errors.toOwnedBundle("");
defer error_bundle.deinit(gpa);
- error_bundle.renderToStdErr(.{}, options.color);
+ error_bundle.renderToStderr(io, .{}, options.color);
process.exit(2);
}