authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-08-06 10:13:44-04:00
committergravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2026-09-03 11:00:53-04:00
log2375b0063da554eeb1ed4b0b8dbe8dad485c5f6a
treecd402500c22638f6ac5a4c1a64c3f60f21deb939
parent79bb28aa57b3712116dc75288d60f2d29ba31cfb

link: improve explicit error sets

"I think we made the error sets good now"

16 files changed, 126 insertions(+), 114 deletions(-)

src/codegen.zig+7-8
......@@ -26,7 +26,6 @@ pub const aarch64 = @import("codegen/aarch64.zig");
2626pub const loongarch = @import("codegen/loongarch.zig");
2727
2828pub const Error = link.Error;
29pub const EmitError = Error || std.Io.Writer.Error || error{MappedFileIo};
3029
3130fn devFeatureForBackend(backend: std.lang.CompilerBackend) dev.Feature {
3231 return switch (backend) {
......@@ -197,7 +196,7 @@ pub fn emitFunction(
197196 any_mir: *const AnyMir,
198197 w: *std.Io.Writer,
199198 debug_output: link.File.DebugInfoOutput,
200) EmitError!void {
199) link.EmitError!void {
201200 const zcu = pt.zcu;
202201 const func = zcu.funcInfo(func_index);
203202 const target = &zcu.navFileScope(func.owner_nav).mod.?.resolved_target.result;
......@@ -229,7 +228,7 @@ pub fn generateLazyFunction(
229228 atom_id: link.File.AtomId,
230229 w: *std.Io.Writer,
231230 debug_output: link.File.DebugInfoOutput,
232) EmitError!void {
231) link.EmitError!void {
233232 const zcu = pt.zcu;
234233 const target = if (Type.fromInterned(lazy_sym.ty).typeDeclInstAllowGeneratedTag(zcu)) |inst_index|
235234 &zcu.fileByIndex(inst_index.resolveFile(&zcu.intern_pool)).mod.?.resolved_target.result
......@@ -253,7 +252,7 @@ pub fn generateLazySymbol(
253252 w: *std.Io.Writer,
254253 debug_output: link.File.DebugInfoOutput,
255254 reloc_parent: link.File.RelocInfo.Parent,
256) EmitError!void {
255) link.EmitError!void {
257256 const tracy = trace(@src());
258257 defer tracy.end();
259258 tracy.addTextFmt("{t}, {f}", .{ lazy_sym.kind, Type.fromInterned(lazy_sym.ty).fmt(pt) });
......@@ -315,7 +314,7 @@ pub fn generateSymbol(
315314 val: Value,
316315 w: *std.Io.Writer,
317316 reloc_parent: link.File.RelocInfo.Parent,
318) (Error || std.Io.Writer.Error)!void {
317) link.EmitError!void {
319318 const tracy = trace(@src());
320319 defer tracy.end();
321320
......@@ -666,7 +665,7 @@ fn lowerPtr(
666665 w: *std.Io.Writer,
667666 reloc_parent: link.File.RelocInfo.Parent,
668667 prev_offset: u64,
669) (Error || std.Io.Writer.Error)!void {
668) link.EmitError!void {
670669 const zcu = pt.zcu;
671670 const ptr = zcu.intern_pool.indexToKey(ptr_val).ptr;
672671 const offset: u64 = prev_offset + ptr.byte_offset;
......@@ -724,7 +723,7 @@ fn lowerUavRef(
724723 w: *std.Io.Writer,
725724 reloc_parent: link.File.RelocInfo.Parent,
726725 offset: u64,
727) (Error || std.Io.Writer.Error)!void {
726) link.EmitError!void {
728727 const zcu = pt.zcu;
729728 const ip = &zcu.intern_pool;
730729 const comp = lf.comp;
......@@ -782,7 +781,7 @@ fn lowerNavRef(
782781 w: *std.Io.Writer,
783782 reloc_parent: link.File.RelocInfo.Parent,
784783 offset: u64,
785) (Error || std.Io.Writer.Error)!void {
784) link.EmitError!void {
786785 const zcu = pt.zcu;
787786 const ip = &zcu.intern_pool;
788787 const target = &zcu.navFileScope(nav_index).mod.?.resolved_target.result;
src/codegen/loongarch/Select.zig+8-8
......@@ -1020,7 +1020,7 @@ pub const Value = struct {
10201020 /// Defines a value with a location.
10211021 /// Returned location must be free-ed by caller.
10221022 /// Extension unchanged.
1023 fn def(vi: Value.Index, isel: *Select) error{ AlreadyReported, OutOfMemory }!?Location {
1023 fn def(vi: Value.Index, isel: *Select) codegen.Error!?Location {
10241024 try vi.collectDefs(isel);
10251025 return vi.takeLocationMarkWritten(isel);
10261026 }
......@@ -2046,7 +2046,7 @@ pub const Value = struct {
20462046 if (!std.debug.runtime_safety) assert(@sizeOf(Mat) <= 32);
20472047 }
20482048
2049 const Error = error{ OutOfMemory, AlreadyReported };
2049 const Error = codegen.Error;
20502050
20512051 pub fn ra(mat: Value.Mat) Register.Alias {
20522052 return mat.location.register;
......@@ -2296,13 +2296,13 @@ pub const Value = struct {
22962296 };
22972297};
22982298
2299fn fail(isel: *Select, comptime format: []const u8, args: anytype) error{ OutOfMemory, AlreadyReported } {
2299fn fail(isel: *Select, comptime format: []const u8, args: anytype) codegen.Error {
23002300 @branchHint(.cold);
23012301 wip_mir_log.debug("codegen error: " ++ format, args);
23022302 return isel.pt.zcu.codegenFail(isel.nav_index, format, args);
23032303}
23042304
2305fn failUnimplemented(isel: *Select, comptime format: []const u8, args: anytype) error{ OutOfMemory, AlreadyReported }!void {
2305fn failUnimplemented(isel: *Select, comptime format: []const u8, args: anytype) codegen.Error!void {
23062306 @branchHint(.cold);
23072307 if (debug_trap_unimplemented_code) {
23082308 const gpa = isel.pt.zcu.gpa;
......@@ -2963,7 +2963,7 @@ pub fn verify(isel: *Select, check_values: bool) void {
29632963 }
29642964}
29652965
2966pub fn body(isel: *Select, air_body: []const Air.Inst.Index) error{ OutOfMemory, AlreadyReported }!void {
2966pub fn body(isel: *Select, air_body: []const Air.Inst.Index) codegen.Error!void {
29672967 const zcu = isel.pt.zcu;
29682968 const ip = &zcu.intern_pool;
29692969 const gpa = zcu.gpa;
......@@ -5341,7 +5341,7 @@ fn forgetReg(isel: *Select, dst_reg: Register) error{ OutOfMemory, AlreadyReport
53415341
53425342/// Frees a register by moving it to another place.
53435343/// Returns true on success, false on failure (i.e. dst_reg is locked/allocated or unallocatable).
5344fn fillReg(isel: *Select, dst_reg: Register) error{ OutOfMemory, AlreadyReported }!bool {
5344fn fillReg(isel: *Select, dst_reg: Register) codegen.Error!bool {
53455345 if (!isRegisterAllocatable(dst_reg)) return false;
53465346 const dst_live_vi = isel.live_registers.getPtr(dst_reg);
53475347 const dst_vi = switch (dst_live_vi.*) {
......@@ -5377,7 +5377,7 @@ fn fillReg(isel: *Select, dst_reg: Register) error{ OutOfMemory, AlreadyReported
53775377/// Frees a set of register. If locked is true, these registers are then locked.
53785378/// Requires all registers to be unlocked.
53795379/// Returns true on success.
5380fn fillRegsBatch(isel: *Select, regs: RegisterSet, locking: bool) error{ OutOfMemory, AlreadyReported }!void {
5380fn fillRegsBatch(isel: *Select, regs: RegisterSet, locking: bool) codegen.Error!void {
53815381 tracking_log.debug("batch fill: {f}", .{fmtRegisterSet(regs)});
53825382 // lock free registers
53835383 var regs_it = regs.iterator();
......@@ -5419,7 +5419,7 @@ fn fillRegsBatch(isel: *Select, regs: RegisterSet, locking: bool) error{ OutOfMe
54195419
54205420/// Frees a register by moving it to stack.
54215421/// Returns true on success, false on failure (i.e. dst_reg is locked/allocated or unallocatable).
5422fn fillRegToMemory(isel: *Select, dst_reg: Register) error{ OutOfMemory, AlreadyReported }!bool {
5422fn fillRegToMemory(isel: *Select, dst_reg: Register) codegen.Error!bool {
54235423 if (!isRegisterAllocatable(dst_reg)) return false;
54245424 const dst_live_vi = isel.live_registers.getPtr(dst_reg);
54255425 const dst_vi = switch (dst_live_vi.*) {
src/codegen/riscv64/CodeGen.zig+1-1
......@@ -856,7 +856,7 @@ pub fn generateLazy(
856856 atom_index: link.File.AtomId,
857857 w: *std.Io.Writer,
858858 debug_output: link.File.DebugInfoOutput,
859) codegen.EmitError!void {
859) link.EmitError!void {
860860 _ = atom_index;
861861 const comp = bin_file.comp;
862862 const gpa = comp.gpa;
src/codegen/riscv64/Emit.zig+1-1
......@@ -13,7 +13,7 @@ prev_di_pc: usize,
1313code_offset_mapping: std.AutoHashMapUnmanaged(Mir.Inst.Index, usize) = .empty,
1414relocs: std.ArrayList(Reloc) = .empty,
1515
16pub const Error = Lower.Error || codegen.EmitError || error{
16pub const Error = Lower.Error || link.EmitError || error{
1717 EmitFail,
1818};
1919
src/codegen/riscv64/Mir.zig+1-1
......@@ -111,7 +111,7 @@ pub fn emit(
111111 atom_index: link.File.AtomId,
112112 w: *std.Io.Writer,
113113 debug_output: link.File.DebugInfoOutput,
114) codegen.EmitError!void {
114) link.EmitError!void {
115115 _ = atom_index;
116116 const zcu = pt.zcu;
117117 const comp = zcu.comp;
src/codegen/sparc64/Emit.zig+1-1
......@@ -41,7 +41,7 @@ branch_forward_origins: std.AutoHashMapUnmanaged(Mir.Inst.Index, std.ArrayList(M
4141/// instruction
4242code_offset_mapping: std.AutoHashMapUnmanaged(Mir.Inst.Index, usize) = .empty,
4343
44const InnerError = codegen.EmitError || error{
44const InnerError = link.EmitError || error{
4545 EmitFail,
4646};
4747
src/codegen/sparc64/Mir.zig+1-1
......@@ -382,7 +382,7 @@ pub fn emit(
382382 atom_index: link.File.AtomId,
383383 w: *std.Io.Writer,
384384 debug_output: link.File.DebugInfoOutput,
385) codegen.EmitError!void {
385) link.EmitError!void {
386386 _ = atom_index;
387387 const zcu = pt.zcu;
388388 const func = zcu.funcInfo(func_index);
src/codegen/x86_64/CodeGen.zig+1-1
......@@ -1120,7 +1120,7 @@ pub fn generateLazy(
11201120 atom_id: link.File.AtomId,
11211121 w: *std.Io.Writer,
11221122 debug_output: link.File.DebugInfoOutput,
1123) codegen.EmitError!void {
1123) link.EmitError!void {
11241124 const gpa = pt.zcu.gpa;
11251125 // This function is for generating global code, so we use the root module.
11261126 const mod = pt.zcu.comp.root_mod;
src/codegen/x86_64/Emit.zig+1-1
......@@ -16,7 +16,7 @@ code_offset_mapping: std.ArrayList(u32),
1616relocs: std.ArrayList(Reloc),
1717table_relocs: std.ArrayList(TableReloc),
1818
19pub const Error = Lower.Error || codegen.EmitError || error{
19pub const Error = Lower.Error || codegen.Error || std.Io.Writer.Error || error{
2020 EmitFail,
2121} || std.posix.MMapError || std.posix.MRemapError || link.File.UpdateDebugInfoError;
2222
src/codegen/x86_64/Mir.zig+4-4
......@@ -1978,7 +1978,7 @@ pub fn emit(
19781978 atom_id: link.File.AtomId,
19791979 w: *std.Io.Writer,
19801980 debug_output: link.File.DebugInfoOutput,
1981) codegen.EmitError!void {
1981) link.EmitError!void {
19821982 const zcu = pt.zcu;
19831983 const comp = zcu.comp;
19841984 const gpa = comp.gpa;
......@@ -2021,7 +2021,7 @@ pub fn emit(
20212021 error.LowerFail, error.EmitFail => return zcu.codegenFailMsg(nav, em.lower.err_msg.?),
20222022 error.InvalidInstruction, error.CannotEncode => return zcu.codegenFail(nav, "emit MIR failed: {s} (Zig compiler bug)", .{@errorName(err)}),
20232023 else => return zcu.codegenFail(nav, "emit MIR failed: {s}", .{@errorName(err)}),
2024 error.AlreadyReported, error.Canceled, error.MappedFileIo, error.WriteFailed => |e| return e,
2024 error.AlreadyReported, error.Canceled, error.WriteFailed => |e| return e,
20252025 };
20262026}
20272027
......@@ -2033,7 +2033,7 @@ pub fn emitLazy(
20332033 atom_id: link.File.AtomId,
20342034 w: *std.Io.Writer,
20352035 debug_output: link.File.DebugInfoOutput,
2036) codegen.EmitError!void {
2036) link.EmitError!void {
20372037 const zcu = pt.zcu;
20382038 const comp = zcu.comp;
20392039 const gpa = comp.gpa;
......@@ -2065,7 +2065,7 @@ pub fn emitLazy(
20652065 error.LowerFail, error.EmitFail => return zcu.codegenFailTypeMsg(lazy_sym.ty, em.lower.err_msg.?),
20662066 error.InvalidInstruction, error.CannotEncode => return zcu.codegenFailType(lazy_sym.ty, "emit MIR failed: {s} (Zig compiler bug)", .{@errorName(err)}),
20672067 else => return zcu.codegenFailType(lazy_sym.ty, "emit MIR failed: {s}", .{@errorName(err)}),
2068 error.AlreadyReported, error.Canceled, error.MappedFileIo, error.WriteFailed => |e| return e,
2068 error.AlreadyReported, error.Canceled, error.WriteFailed => |e| return e,
20692069 };
20702070}
20712071
src/link.zig+1
......@@ -38,6 +38,7 @@ pub const Error = Allocator.Error || Io.Cancelable || error{
3838 /// instance in `Compilation.link_diags`.
3939 AlreadyReported,
4040};
41pub const EmitError = Error || Io.Writer.Error;
4142
4243pub const Diags = struct {
4344 /// Stored here so that function definitions can distinguish between
src/link/Coff.zig+10-8
......@@ -7356,7 +7356,7 @@ fn updateExportInner(
73567356 pt: Zcu.PerThread,
73577357 export_index: Zcu.Export.Index,
73587358 alias_syms: *std.array_hash_map.Auto(Symbol.Index, Symbol.Index),
7359) !void {
7359) Error!void {
73607360 const zcu = pt.zcu;
73617361 const gpa = zcu.gpa;
73627362 const ip = &zcu.intern_pool;
......@@ -7519,17 +7519,19 @@ fn updateExportInner(
75197519 }
75207520}
75217521
7522fn dumpStderr(coff: *Coff, tid: Zcu.PerThread.Id) !void {
7522fn dumpStderr(coff: *Coff, tid: Zcu.PerThread.Id) Io.File.Writer.Error!void {
75237523 const comp = coff.base.comp;
75247524 const io = comp.io;
75257525 var buffer: [512]u8 = undefined;
75267526 const stderr = try io.lockStderr(&buffer, null);
75277527 defer io.unlockStderr();
75287528 const w = &stderr.file_writer.interface;
7529 _ = try coff.dump(w, tid);
7529 _ = coff.dump(w, tid) catch |err| switch (err) {
7530 error.WriteFailed => return stderr.file_writer.err.?,
7531 };
75307532}
75317533
7532pub fn dump(coff: *Coff, w: *Io.Writer, tid: Zcu.PerThread.Id) !link.File.DumpResult {
7534pub fn dump(coff: *Coff, w: *Io.Writer, tid: Zcu.PerThread.Id) Io.Writer.Error!link.File.DumpResult {
75337535 if (coff.options.enable_link_snapshots) {
75347536 try coff.printNode(tid, w, .root, 0);
75357537 try w.writeAll("Section table:\n");
......@@ -7544,7 +7546,7 @@ pub fn dump(coff: *Coff, w: *Io.Writer, tid: Zcu.PerThread.Id) !link.File.DumpRe
75447546 return .disabled;
75457547}
75467548
7547fn printSection(coff: *Coff, w: *Io.Writer, name: String, si: Symbol.Index) !void {
7549fn printSection(coff: *Coff, w: *Io.Writer, name: String, si: Symbol.Index) Io.Writer.Error!void {
75487550 const sym = si.get(coff);
75497551 try w.print("{d:0>6}@{d:0>2} {x:08} n{d:0>8} | {s}\n", .{
75507552 si,
......@@ -7560,7 +7562,7 @@ fn printSymbol(
75607562 w: *Io.Writer,
75617563 tid: Zcu.PerThread.Id,
75627564 si: Symbol.Index,
7563) !void {
7565) Io.Writer.Error!void {
75647566 const sym = si.get(coff);
75657567 try w.print("{d:0>6}@{d:0>2} {x:08} {s} {s} {s} n{d:0>8}+{x:08}:{s: <26} | {x:08} ", .{
75667568 si,
......@@ -7622,7 +7624,7 @@ fn printNodeName(
76227624 w: *std.Io.Writer,
76237625 tid: Zcu.PerThread.Id,
76247626 node: Node,
7625) !void {
7627) Io.Writer.Error!void {
76267628 switch (node) {
76277629 else => {},
76287630 .image_section => |si| try w.print("({s})", .{
......@@ -7702,7 +7704,7 @@ pub fn printNode(
77027704 w: *Io.Writer,
77037705 ni: MappedFile.Node.Index,
77047706 indent: usize,
7705) !void {
7707) Io.Writer.Error!void {
77067708 const node = coff.getNode(ni);
77077709 try w.splatByteAll(' ', indent);
77087710 try w.writeAll(@tagName(node));
src/link/Dwarf2.zig+76-68
......@@ -16,8 +16,6 @@ frame: Frame,
1616debug_info: DebugInfo,
1717debug_line: DebugLine,
1818
19pub const UpdateError = link.Error || error{MappedFileIo};
20
2119pub const AddressSize = enum(u8) { @"32" = 4, @"64" = 8, _ };
2220
2321pub const Unit = struct {
......@@ -158,7 +156,7 @@ pub const Loc = union(enum) {
158156 }
159157 }
160158
161 fn write(loc: Loc, adapter: anytype) (UpdateError || Writer.Error)!void {
159 fn write(loc: Loc, adapter: anytype) link.EmitError!void {
162160 const writer = adapter.writer();
163161 switch (loc) {
164162 .empty => {},
......@@ -324,7 +322,7 @@ pub const Cfa = union(enum) {
324322 const RegOff = struct { reg: u32, off: i64 };
325323 const RegExpr = struct { reg: u32, expr: Loc };
326324
327 fn write(cfa: Cfa, wip_nav: *WipNav) (UpdateError || Writer.Error)!void {
325 fn write(cfa: Cfa, wip_nav: *WipNav) link.EmitError!void {
328326 const dfw = &wip_nav.fde_writer.interface;
329327 switch (cfa) {
330328 .nop => try dfw.writeByte(DW.CFA.nop),
......@@ -487,11 +485,11 @@ pub const WipNav = struct {
487485 debug.* = undefined;
488486 }
489487
490 pub fn genFuncHeaders(debug: *Debug) UpdateError!void {
488 pub fn genFuncHeaders(debug: *Debug) link.Error!void {
491489 try debug.wip_nav.genFuncHeaders();
492490 }
493491
494 pub fn genDebugFrame(debug: *Debug, loc: u32, cfa: Cfa) UpdateError!void {
492 pub fn genDebugFrame(debug: *Debug, loc: u32, cfa: Cfa) link.Error!void {
495493 return debug.wip_nav.genDebugFrame(loc, cfa);
496494 }
497495
......@@ -502,10 +500,10 @@ pub const WipNav = struct {
502500 opt_name: ?[]const u8,
503501 ty: Type,
504502 loc: Loc,
505 ) UpdateError!void {
503 ) link.Error!void {
506504 if (true) return;
507505 return debug.genLocalVarDebugInfoInner(tag, opt_name, ty, loc) catch |err| switch (err) {
508 error.WriteFailed => debug.info_writer.err.?,
506 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
509507 else => |e| e,
510508 };
511509 }
......@@ -515,7 +513,7 @@ pub const WipNav = struct {
515513 opt_name: ?[]const u8,
516514 ty: Type,
517515 loc: Loc,
518 ) (UpdateError || Writer.Error)!void {
516 ) link.EmitError!void {
519517 assert(debug.wip_nav.func != null);
520518 try debug.abbrevCode(switch (tag) {
521519 .arg => if (opt_name) |_| .arg else .unnamed_arg,
......@@ -533,10 +531,10 @@ pub const WipNav = struct {
533531 tag: LocalConstTag,
534532 opt_name: ?[]const u8,
535533 val: Value,
536 ) UpdateError!void {
534 ) link.Error!void {
537535 if (true) return;
538536 return debug.genLocalConstDebugInfoInner(tag, opt_name, val) catch |err| switch (err) {
539 error.WriteFailed => debug.info_writer.err.?,
537 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
540538 else => |e| e,
541539 };
542540 }
......@@ -545,7 +543,7 @@ pub const WipNav = struct {
545543 tag: LocalConstTag,
546544 opt_name: ?[]const u8,
547545 val: Value,
548 ) (UpdateError || Writer.Error)!void {
546 ) link.EmitError!void {
549547 assert(debug.wip_nav.func != null);
550548 const zcu = debug.pt.zcu;
551549 const ty = val.typeOf(zcu);
......@@ -571,23 +569,23 @@ pub const WipNav = struct {
571569 debug.any_children = true;
572570 }
573571
574 pub fn genVarArgsDebugInfo(debug: *Debug) UpdateError!void {
572 pub fn genVarArgsDebugInfo(debug: *Debug) link.Error!void {
575573 if (true) return;
576574 return debug.genVarArgsDebugInfoInner() catch |err| switch (err) {
577 error.WriteFailed => debug.info_writer.err.?,
575 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
578576 else => |e| e,
579577 };
580578 }
581 fn genVarArgsDebugInfoInner(debug: *Debug) (UpdateError || Writer.Error)!void {
579 fn genVarArgsDebugInfoInner(debug: *Debug) link.EmitError!void {
582580 assert(debug.wip_nav.func != null);
583581 try debug.abbrevCode(.is_var_args);
584582 debug.any_children = true;
585583 }
586584
587 pub fn advancePcAndLine(debug: *Debug, delta_line: i33, delta_pc: u64) UpdateError!void {
585 pub fn advancePcAndLine(debug: *Debug, delta_line: i33, delta_pc: u64) link.Error!void {
588586 if (true) return;
589587 return debug.advancePcAndLineInner(delta_line, delta_pc) catch |err| switch (err) {
590 error.WriteFailed => debug.line_writer.err.?,
588 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
591589 };
592590 }
593591 fn advancePcAndLineInner(debug: *Debug, delta_line: i33, delta_pc: u64) Writer.Error!void {
......@@ -625,10 +623,10 @@ pub const WipNav = struct {
625623 (header.line_range * remaining_op_advance) + header.opcode_base));
626624 }
627625
628 pub fn setColumn(debug: *Debug, column: u32) UpdateError!void {
626 pub fn setColumn(debug: *Debug, column: u32) link.Error!void {
629627 if (true) return;
630628 return debug.setColumnInner(column) catch |err| switch (err) {
631 error.WriteFailed => debug.line_writer.err.?,
629 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
632630 };
633631 }
634632 fn setColumnInner(debug: *Debug, column: u32) Writer.Error!void {
......@@ -637,44 +635,44 @@ pub const WipNav = struct {
637635 try dlw.writeUleb128(column + 1);
638636 }
639637
640 pub fn negateStmt(debug: *Debug) UpdateError!void {
638 pub fn negateStmt(debug: *Debug) link.Error!void {
641639 if (true) return;
642640 return debug.negateStmtInner() catch |err| switch (err) {
643 error.WriteFailed => debug.line_writer.err.?,
641 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
644642 };
645643 }
646644 fn negateStmtInner(debug: *Debug) Writer.Error!void {
647645 try debug.line_writer.interface.writeByte(DW.LNS.negate_stmt);
648646 }
649647
650 pub fn setPrologueEnd(debug: *Debug) UpdateError!void {
648 pub fn setPrologueEnd(debug: *Debug) link.Error!void {
651649 if (true) return;
652650 return debug.setPrologueEndInner() catch |err| switch (err) {
653 error.WriteFailed => debug.line_writer.err.?,
651 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
654652 };
655653 }
656654 fn setPrologueEndInner(debug: *Debug) Writer.Error!void {
657655 try debug.line_writer.interface.writeByte(DW.LNS.set_prologue_end);
658656 }
659657
660 pub fn setEpilogueBegin(debug: *Debug) UpdateError!void {
658 pub fn setEpilogueBegin(debug: *Debug) link.Error!void {
661659 if (true) return;
662660 return debug.setEpilogueBeginInner() catch |err| switch (err) {
663 error.WriteFailed => debug.line_writer.err.?,
661 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
664662 };
665663 }
666664 fn setEpilogueBeginInner(debug: *Debug) Writer.Error!void {
667665 try debug.line_writer.interface.writeByte(DW.LNS.set_epilogue_begin);
668666 }
669667
670 pub fn enterBlock(debug: *Debug, code_off: u64) UpdateError!void {
668 pub fn enterBlock(debug: *Debug, code_off: u64) link.Error!void {
671669 if (true) return;
672670 return debug.enterBlockInner(code_off) catch |err| switch (err) {
673 error.WriteFailed => debug.info_writer.err.?,
671 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
674672 else => |e| e,
675673 };
676674 }
677 fn enterBlockInner(debug: *Debug, code_off: u64) (UpdateError || Writer.Error)!void {
675 fn enterBlockInner(debug: *Debug, code_off: u64) link.EmitError!void {
678676 const dwarf = debug.wip_nav.dwarf;
679677 const diw = &debug.info_writer.interface;
680678 const block = try debug.blocks.addOne(dwarf.linkFile().comp.gpa);
......@@ -688,14 +686,14 @@ pub const WipNav = struct {
688686 debug.any_children = false;
689687 }
690688
691 pub fn leaveBlock(debug: *Debug, code_off: u64) UpdateError!void {
689 pub fn leaveBlock(debug: *Debug, code_off: u64) link.Error!void {
692690 if (true) return;
693691 return debug.leaveBlockInner(code_off) catch |err| switch (err) {
694 error.WriteFailed => debug.info_writer.err.?,
692 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
695693 else => |e| e,
696694 };
697695 }
698 fn leaveBlockInner(debug: *Debug, code_off: u64) (UpdateError || Writer.Error)!void {
696 fn leaveBlockInner(debug: *Debug, code_off: u64) link.EmitError!void {
699697 const dwarf = debug.wip_nav.dwarf;
700698 const block_bytes = comptime uleb128Bytes(@backingInt(AbbrevCode.block));
701699 const block = debug.blocks.pop().?;
......@@ -722,10 +720,10 @@ pub const WipNav = struct {
722720 code_off: u64,
723721 line: u32,
724722 column: u32,
725 ) UpdateError!void {
723 ) link.Error!void {
726724 if (true) return;
727725 return debug.enterInlineFuncInner(func, code_off, line, column) catch |err| switch (err) {
728 error.WriteFailed => debug.info_writer.err.?,
726 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
729727 else => |e| e,
730728 };
731729 }
......@@ -735,7 +733,7 @@ pub const WipNav = struct {
735733 code_off: u64,
736734 line: u32,
737735 column: u32,
738 ) (UpdateError || Writer.Error)!void {
736 ) link.EmitError!void {
739737 const dwarf = debug.wip_nav.dwarf;
740738 const zcu = debug.pt.zcu;
741739 const diw = &debug.info_writer.interface;
......@@ -754,10 +752,10 @@ pub const WipNav = struct {
754752 debug.any_children = false;
755753 }
756754
757 pub fn leaveInlineFunc(debug: *Debug, func: InternPool.Index, code_off: u64) UpdateError!void {
755 pub fn leaveInlineFunc(debug: *Debug, func: InternPool.Index, code_off: u64) link.Error!void {
758756 if (true) return;
759757 return debug.leaveInlineFuncInner(func, code_off) catch |err| switch (err) {
760 error.WriteFailed => debug.info_writer.err.?,
758 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.info_writer),
761759 else => |e| e,
762760 };
763761 }
......@@ -765,7 +763,7 @@ pub const WipNav = struct {
765763 debug: *Debug,
766764 func: InternPool.Index,
767765 code_off: u64,
768 ) (UpdateError || Writer.Error)!void {
766 ) link.EmitError!void {
769767 const dwarf = debug.wip_nav.dwarf;
770768 const inlined_func_bytes = comptime uleb128Bytes(@backingInt(AbbrevCode.inlined_func));
771769 const block = debug.blocks.pop().?;
......@@ -787,13 +785,13 @@ pub const WipNav = struct {
787785 debug.any_children = true;
788786 }
789787
790 pub fn setInlineFunc(debug: *Debug, func: InternPool.Index) UpdateError!void {
788 pub fn setInlineFunc(debug: *Debug, func: InternPool.Index) link.Error!void {
791789 return debug.setInlineFuncInner(func) catch |err| switch (err) {
792 error.WriteFailed => debug.line_writer.err.?,
790 error.WriteFailed => return debug.wip_nav.reportWriteError(&debug.line_writer),
793791 else => |e| e,
794792 };
795793 }
796 fn setInlineFuncInner(debug: *Debug, func: InternPool.Index) (UpdateError || Writer.Error)!void {
794 fn setInlineFuncInner(debug: *Debug, func: InternPool.Index) link.EmitError!void {
797795 const wip_nav = &debug.wip_nav;
798796 const zcu = debug.pt.zcu;
799797 const dwarf = wip_nav.dwarf;
......@@ -855,7 +853,7 @@ pub const WipNav = struct {
855853 wip_nav.func = func;
856854 }
857855
858 fn abbrevCode(debug: *Debug, abbrev_code: AbbrevCode) (UpdateError || Writer.Error)!void {
856 fn abbrevCode(debug: *Debug, abbrev_code: AbbrevCode) link.EmitError!void {
859857 try debug.info_writer.interface.writeUleb128(try debug.wip_nav.dwarf.refAbbrevCode(abbrev_code));
860858 }
861859
......@@ -872,7 +870,7 @@ pub const WipNav = struct {
872870 debug: *Debug,
873871 target: MappedFile.Node.Index,
874872 addend: i64,
875 ) (UpdateError || Writer.Error)!void {
873 ) link.EmitError!void {
876874 const dwarf = debug.wip_nav.dwarf;
877875 const diw = &debug.info_writer.interface;
878876 const offset = diw.end;
......@@ -892,7 +890,7 @@ pub const WipNav = struct {
892890 );
893891 }
894892
895 fn strp(debug: *Debug, str: []const u8) (UpdateError || Writer.Error)!void {
893 fn strp(debug: *Debug, str: []const u8) link.EmitError!void {
896894 if (true) @panic("TODO");
897895 const dwarf = debug.wip_nav.dwarf;
898896 try debug.infoSectionOffset(.debug_str, try dwarf.debug_str.addString(dwarf, str), 0);
......@@ -902,14 +900,14 @@ pub const WipNav = struct {
902900 debug: *Debug,
903901 comptime fmt: []const u8,
904902 args: anytype,
905 ) (UpdateError || Writer.Error)!void {
903 ) link.EmitError!void {
906904 const gpa = &debug.wip_nav.dwarf.gpa;
907905 const str = try std.fmt.allocPrint(gpa, fmt, args);
908906 defer gpa.free(str);
909907 return debug.strp(str);
910908 }
911909
912 fn infoExprLoc(debug: *Debug, loc: Loc) (UpdateError || Writer.Error)!void {
910 fn infoExprLoc(debug: *Debug, loc: Loc) link.EmitError!void {
913911 var buf: [64]u8 = undefined;
914912 var counter: ExprLocCounter = .init(debug.wip_nav.dwarf, &buf);
915913 try loc.write(&counter);
......@@ -922,13 +920,13 @@ pub const WipNav = struct {
922920 fn endian(ctx: @This()) std.lang.Endian {
923921 return ctx.debug.wip_nav.dwarf.endian;
924922 }
925 fn addrSym(ctx: @This(), si: link.File.SymbolId) (UpdateError || Writer.Error)!void {
923 fn addrSym(ctx: @This(), si: link.File.SymbolId) link.EmitError!void {
926924 try ctx.debug.infoAddrSym(si, 0);
927925 }
928926 fn infoEntry(
929927 ctx: @This(),
930928 node: MappedFile.Node.Index,
931 ) (UpdateError || Writer.Error)!void {
929 ) link.EmitError!void {
932930 try ctx.debug.infoSectionOffset(node, 0);
933931 }
934932 } = .{ .debug = debug };
......@@ -940,7 +938,7 @@ pub const WipNav = struct {
940938 debug: *Debug,
941939 si: link.File.SymbolId,
942940 sym_off: u64,
943 ) (UpdateError || Writer.Error)!void {
941 ) link.EmitError!void {
944942 const diw = &debug.info_writer.interface;
945943 try debug.infoExternalReloc(.{
946944 .source_off = @intCast(diw.end),
......@@ -950,20 +948,20 @@ pub const WipNav = struct {
950948 try diw.splatByteAll(0, @backingInt(debug.wip_nav.dwarf.address_size));
951949 }
952950
953 fn refNav(debug: *Debug, nav_index: InternPool.Nav.Index) (UpdateError || Writer.Error)!void {
951 fn refNav(debug: *Debug, nav_index: InternPool.Nav.Index) link.EmitError!void {
954952 try debug.infoSectionOffset(try debug.wip_nav.dwarf.getNavNode(nav_index), 0);
955953 }
956954
957 fn refType(debug: *Debug, ty: Type) (UpdateError || Writer.Error)!void {
955 fn refType(debug: *Debug, ty: Type) link.EmitError!void {
958956 return debug.refValue(ty.toValue());
959957 }
960958
961 fn refValue(debug: *Debug, value: Value) (UpdateError || Writer.Error)!void {
959 fn refValue(debug: *Debug, value: Value) link.EmitError!void {
962960 if (true) @panic("TODO");
963961 try debug.infoSectionOffset(.debug_info, try debug.getValueNode(value), 0);
964962 }
965963
966 fn getValueNode(debug: *Debug, value: Value) UpdateError!MappedFile.Node.Index {
964 fn getValueNode(debug: *Debug, value: Value) link.Error!MappedFile.Node.Index {
967965 if (value.typeOf(debug.zcu).toIntern() != .type_type) {
968966 assert(value.typeOf(debug.zcu).comptimeOnly(debug.zcu));
969967 }
......@@ -972,7 +970,7 @@ pub const WipNav = struct {
972970 return dwarf.values.items[@backingInt(index)];
973971 }
974972
975 fn blockValue(debug: *Debug, val: Value) (UpdateError || Writer.Error)!void {
973 fn blockValue(debug: *Debug, val: Value) link.EmitError!void {
976974 const ty = val.typeOf(debug.pt.zcu);
977975 const diw = &debug.info_writer.interface;
978976 const size = ty.abiSize(debug.pt.zcu);
......@@ -1004,13 +1002,13 @@ pub const WipNav = struct {
10041002 wip_nav.* = undefined;
10051003 }
10061004
1007 pub fn genFuncHeaders(wip_nav: *WipNav) UpdateError!void {
1005 pub fn genFuncHeaders(wip_nav: *WipNav) link.Error!void {
10081006 wip_nav.genDebugFrameHeader() catch |err| switch (err) {
1009 error.WriteFailed => return wip_nav.fde_writer.err.?,
1007 error.WriteFailed => return wip_nav.reportWriteError(&wip_nav.fde_writer),
10101008 else => |e| return e,
10111009 };
10121010 }
1013 fn genDebugFrameHeader(wip_nav: *WipNav) (UpdateError || Writer.Error)!void {
1011 fn genDebugFrameHeader(wip_nav: *WipNav) link.EmitError!void {
10141012 assert(wip_nav.func != null);
10151013 const dwarf = wip_nav.dwarf;
10161014 const dfw = &wip_nav.fde_writer.interface;
......@@ -1050,13 +1048,13 @@ pub const WipNav = struct {
10501048 }
10511049 }
10521050
1053 pub fn genDebugFrame(wip_nav: *WipNav, loc: u32, cfa: Cfa) UpdateError!void {
1051 pub fn genDebugFrame(wip_nav: *WipNav, loc: u32, cfa: Cfa) link.Error!void {
10541052 return wip_nav.genDebugFrameInner(loc, cfa) catch |err| switch (err) {
1055 error.WriteFailed => wip_nav.fde_writer.err.?,
1056 else => |e| e,
1053 error.WriteFailed => return wip_nav.reportWriteError(&wip_nav.fde_writer),
1054 else => |e| return e,
10571055 };
10581056 }
1059 fn genDebugFrameInner(wip_nav: *WipNav, loc: u32, cfa: Cfa) (UpdateError || Writer.Error)!void {
1057 fn genDebugFrameInner(wip_nav: *WipNav, loc: u32, cfa: Cfa) link.EmitError!void {
10601058 assert(wip_nav.func != null);
10611059 const loc_cfa: Cfa = .{ .advance_loc = loc };
10621060 try loc_cfa.write(wip_nav);
......@@ -1123,7 +1121,7 @@ pub const WipNav = struct {
11231121 wip_nav: *WipNav,
11241122 target: MappedFile.Node.Index,
11251123 addend: i64,
1126 ) (UpdateError || Writer.Error)!void {
1124 ) link.EmitError!void {
11271125 const dwarf = wip_nav.dwarf;
11281126 const dfw = &wip_nav.fde_writer.interface;
11291127 const offset = dfw.end;
......@@ -1143,7 +1141,7 @@ pub const WipNav = struct {
11431141 );
11441142 }
11451143
1146 fn frameExprLoc(wip_nav: *WipNav, loc: Loc) (UpdateError || Writer.Error)!void {
1144 fn frameExprLoc(wip_nav: *WipNav, loc: Loc) link.EmitError!void {
11471145 var buf: [64]u8 = undefined;
11481146 var counter: ExprLocCounter = .init(wip_nav.dwarf, &buf);
11491147 try loc.write(&counter);
......@@ -1156,10 +1154,10 @@ pub const WipNav = struct {
11561154 fn endian(ctx: @This()) std.lang.Endian {
11571155 return ctx.wip_nav.dwarf.endian;
11581156 }
1159 fn addrSym(ctx: @This(), si: link.File.SymbolId) (UpdateError || Writer.Error)!void {
1157 fn addrSym(ctx: @This(), si: link.File.SymbolId) link.EmitError!void {
11601158 try ctx.wip_nav.frameAddrSym(si, 0);
11611159 }
1162 fn infoEntry(ctx: @This(), node: MappedFile.Node.Index) (UpdateError || Writer.Error)!void {
1160 fn infoEntry(ctx: @This(), node: MappedFile.Node.Index) link.EmitError!void {
11631161 try ctx.wip_nav.frameSectionOffset(node, 0);
11641162 }
11651163 } = .{ .wip_nav = wip_nav };
......@@ -1171,7 +1169,7 @@ pub const WipNav = struct {
11711169 wip_nav: *WipNav,
11721170 si: link.File.SymbolId,
11731171 sym_off: u64,
1174 ) (UpdateError || Writer.Error)!void {
1172 ) link.EmitError!void {
11751173 const dwarf = wip_nav.dwarf;
11761174 const dfw = &wip_nav.fde_writer.interface;
11771175 const offset = dfw.end;
......@@ -1188,6 +1186,16 @@ pub const WipNav = struct {
11881186 },
11891187 );
11901188 }
1189
1190 fn reportWriteError(wip_nav: *WipNav, mfnw: *const MappedFile.Node.Writer) link.Error {
1191 switch (mfnw.err.?) {
1192 else => |e| return e,
1193 error.MappedFileIo => return wip_nav.dwarf.linkFile().comp.link_diags.fail(
1194 "failed to write output file: {t}",
1195 .{mfnw.mf.io_err.?},
1196 ),
1197 }
1198 }
11911199};
11921200
11931201pub fn init(lf: *link.File, format: DW.Format) Dwarf {
......@@ -1283,7 +1291,7 @@ pub fn initUnits(dwarf: *Dwarf, zcu: *Zcu) Allocator.Error!void {
12831291 };
12841292}
12851293
1286fn getNavNode(dwarf: *Dwarf, nav_index: InternPool.Nav.Index) UpdateError!MappedFile.Node.Index {
1294fn getNavNode(dwarf: *Dwarf, nav_index: InternPool.Nav.Index) link.Error!MappedFile.Node.Index {
12871295 if (true) @panic("TODO");
12881296 const zcu = dwarf.linkFile().comp.zcu.?;
12891297 const ip = &zcu.intern_pool;
......@@ -1329,7 +1337,7 @@ pub fn genEhFrameHdr(
13291337 eh_frame_hdr_ai: link.File.AtomId,
13301338 eh_frame_hdr: *EhFrameHdr,
13311339 eh_frame_si: link.File.SymbolId,
1332) UpdateError!void {
1340) link.Error!void {
13331341 eh_frame_hdr.* = .{
13341342 .version = 1,
13351343 .eh_frame_ptr_enc = .{ .type = .sdata4, .rel = .pcrel },
......@@ -1423,7 +1431,7 @@ pub fn updateEhFrameFde(dwarf: *Dwarf, fde: []u8, fde_offset: u64) void {
14231431fn refAbbrevCode(
14241432 dwarf: *Dwarf,
14251433 abbrev_code: AbbrevCode,
1426) (UpdateError || Writer.Error)!@typeInfo(AbbrevCode).@"enum".tag_type {
1434) link.EmitError!@typeInfo(AbbrevCode).@"enum".tag_type {
14271435 if (true) @panic("TODO");
14281436 const Entry = {};
14291437 const DebugAbbrev = {};
src/link/Elf/ZigObject.zig-1
......@@ -1549,7 +1549,6 @@ pub fn updateFunc(
15491549 if (debug_wip_nav) |*dn| .{ .dwarf = dn } else .none,
15501550 ) catch |err| switch (err) {
15511551 error.WriteFailed => return error.OutOfMemory,
1552 error.MappedFileIo => unreachable, // MappedFile is not being used
15531552 else => |e| return e,
15541553 };
15551554 const code = aw.written();
src/link/Elf2.zig+13-9
......@@ -181,10 +181,10 @@ lazy: std.EnumArray(link.File.LazySymbol.Kind, struct {
181181}),
182182pending_uavs: std.ArrayList(Node.UavMapIndex),
183183symbol_relocs: std.ArrayList(SymbolReloc),
184/// Set of relocations which must be re-applied if the size of the TLS segment changes.
185tls_size_symbol_relocs: std.array_hash_map.Auto(SymbolReloc.Index, void),
186184node_relocs: std.ArrayList(NodeReloc),
187185got_relocs: std.ArrayList(GotReloc),
186/// Set of relocations which must be re-applied if the size of the TLS segment changes.
187tls_size_symbol_relocs: std.array_hash_map.Auto(SymbolReloc.Index, void),
188188/// Index matches the index into `shdrs`. Like `shdrs`, this map excludes `SHN_UNDEF`.
189189section_by_name: std.array_hash_map.Auto(String(.shstrtab), void),
190190/// Key is the name of a global symbol which has been moved to a new symtab index. Any relocation
......@@ -3708,15 +3708,15 @@ fn create(
37083708 }),
37093709 .pending_uavs = .empty,
37103710 .symbol_relocs = .empty,
3711 .tls_size_symbol_relocs = .empty,
37123711 .node_relocs = .empty,
37133712 .got_relocs = .empty,
3713 .tls_size_symbol_relocs = .empty,
37143714 .section_by_name = .empty,
37153715 .changed_symtab_index = .empty,
37163716 .textrel_count = 0,
37173717
37183718 .dwarf = .init(&elf.base, switch (comp.config.debug_format) {
3719 .strip => .@"32",
3719 .strip => .@"32", // for .eh_frame
37203720 .dwarf => |v| v,
37213721 .code_view => unreachable,
37223722 }),
......@@ -3768,9 +3768,9 @@ pub fn deinit(elf: *Elf) void {
37683768 for (&elf.lazy.values) |*lazy| lazy.map.deinit(gpa);
37693769 elf.pending_uavs.deinit(gpa);
37703770 elf.symbol_relocs.deinit(gpa);
3771 elf.tls_size_symbol_relocs.deinit(gpa);
37723771 elf.node_relocs.deinit(gpa);
37733772 elf.got_relocs.deinit(gpa);
3773 elf.tls_size_symbol_relocs.deinit(gpa);
37743774 elf.section_by_name.deinit(gpa);
37753775 elf.changed_symtab_index.deinit(gpa);
37763776
......@@ -8342,7 +8342,9 @@ fn updateFuncInner(
83428342 &nw.interface,
83438343 debug_output,
83448344 ) catch |err| switch (err) {
8345 error.WriteFailed => return nw.err.?,
8345 error.WriteFailed => {
8346 if (nw.err) |e| return e;
8347 },
83468348 else => |e| return e,
83478349 };
83488350 const func_length = nw.interface.end;
......@@ -9899,17 +9901,19 @@ fn updateExportInner(
98999901 };
99009902}
99019903
9902fn dumpStderr(elf: *Elf, tid: Zcu.PerThread.Id) !void {
9904fn dumpStderr(elf: *Elf, tid: Zcu.PerThread.Id) Io.File.Writer.Error!void {
99039905 const comp = elf.base.comp;
99049906 const io = comp.io;
99059907 var buffer: [512]u8 = undefined;
99069908 const stderr = try io.lockStderr(&buffer, null);
99079909 defer io.unlockStderr();
99089910 const w = &stderr.file_writer.interface;
9909 _ = try elf.dump(w, tid);
9911 _ = elf.dump(w, tid) catch |err| switch (err) {
9912 error.WriteFailed => return stderr.file_writer.err.?,
9913 };
99109914}
99119915
9912pub fn dump(elf: *Elf, w: *Io.Writer, tid: Zcu.PerThread.Id) !link.File.DumpResult {
9916pub fn dump(elf: *Elf, w: *Io.Writer, tid: Zcu.PerThread.Id) Io.Writer.Error!link.File.DumpResult {
99139917 if (elf.options.enable_link_snapshots) {
99149918 try elf.printNode(tid, w, .root, 0);
99159919 return .enabled;
src/link/MachO/ZigObject.zig-1
......@@ -790,7 +790,6 @@ pub fn updateFunc(
790790 if (debug_wip_nav) |*wip_nav| .{ .dwarf = wip_nav } else .none,
791791 ) catch |err| switch (err) {
792792 error.WriteFailed => return error.OutOfMemory,
793 error.MappedFileIo => unreachable, // MappedFile is not being used
794793 else => |e| return e,
795794 };
796795 const code = aw.written();