authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-11-24 11:58:24-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-11-24 14:34:18-08:00
logece62a0223cfad5e49a5c75a944a21e735a01a05
tree225c943cd10757137362c4073244d8e4d294ff19
parent3f34f5e43349214c862882a83bd951701a6c735f

frontend: introduce error.Canceled


7 files changed, 55 insertions(+), 35 deletions(-)

src/Compilation.zig+15-15
......@@ -2851,6 +2851,7 @@ fn cleanupAfterUpdate(comp: *Compilation, tmp_dir_rand_int: u64) void {
28512851
28522852pub const UpdateError = error{
28532853 OutOfMemory,
2854 Canceled,
28542855 Unexpected,
28552856 CurrentWorkingDirectoryUnlinked,
28562857};
......@@ -2930,6 +2931,7 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) UpdateE
29302931 },
29312932 },
29322933 error.OutOfMemory => return error.OutOfMemory,
2934 error.Canceled => return error.Canceled,
29332935 error.InvalidFormat => return comp.setMiscFailure(
29342936 .check_whole_cache,
29352937 "failed to check cache: invalid manifest file format",
......@@ -5010,7 +5012,7 @@ fn performAllTheWork(
50105012 }
50115013}
50125014
5013const JobError = Allocator.Error;
5015const JobError = Allocator.Error || Io.Cancelable;
50145016
50155017pub fn queueJob(comp: *Compilation, job: Job) !void {
50165018 try comp.work_queues[Job.stage(job)].pushBack(comp.gpa, job);
......@@ -5117,6 +5119,7 @@ fn processOneJob(tid: usize, comp: *Compilation, job: Job) JobError!void {
51175119
51185120 pt.ensureFuncBodyUpToDate(func) catch |err| switch (err) {
51195121 error.OutOfMemory => |e| return e,
5122 error.Canceled => |e| return e,
51205123 error.AnalysisFail => return,
51215124 };
51225125 },
......@@ -5137,6 +5140,7 @@ fn processOneJob(tid: usize, comp: *Compilation, job: Job) JobError!void {
51375140 };
51385141 maybe_err catch |err| switch (err) {
51395142 error.OutOfMemory => |e| return e,
5143 error.Canceled => |e| return e,
51405144 error.AnalysisFail => return,
51415145 };
51425146
......@@ -5166,7 +5170,7 @@ fn processOneJob(tid: usize, comp: *Compilation, job: Job) JobError!void {
51665170 const pt: Zcu.PerThread = .activate(comp.zcu.?, @enumFromInt(tid));
51675171 defer pt.deactivate();
51685172 Type.fromInterned(ty).resolveFully(pt) catch |err| switch (err) {
5169 error.OutOfMemory => return error.OutOfMemory,
5173 error.OutOfMemory, error.Canceled => |e| return e,
51705174 error.AnalysisFail => return,
51715175 };
51725176 },
......@@ -5177,7 +5181,7 @@ fn processOneJob(tid: usize, comp: *Compilation, job: Job) JobError!void {
51775181 const pt: Zcu.PerThread = .activate(comp.zcu.?, @enumFromInt(tid));
51785182 defer pt.deactivate();
51795183 pt.semaMod(mod) catch |err| switch (err) {
5180 error.OutOfMemory => return error.OutOfMemory,
5184 error.OutOfMemory, error.Canceled => |e| return e,
51815185 error.AnalysisFail => return,
51825186 };
51835187 },
......@@ -5190,8 +5194,8 @@ fn processOneJob(tid: usize, comp: *Compilation, job: Job) JobError!void {
51905194 // TODO Surface more error details.
51915195 comp.lockAndSetMiscFailure(
51925196 .windows_import_lib,
5193 "unable to generate DLL import .lib file for {s}: {s}",
5194 .{ link_lib, @errorName(err) },
5197 "unable to generate DLL import .lib file for {s}: {t}",
5198 .{ link_lib, err },
51955199 );
51965200 };
51975201 },
......@@ -6066,14 +6070,10 @@ fn buildLibZigC(comp: *Compilation, prog_node: std.Progress.Node) void {
60666070 };
60676071}
60686072
6069fn reportRetryableCObjectError(
6070 comp: *Compilation,
6071 c_object: *CObject,
6072 err: anyerror,
6073) error{OutOfMemory}!void {
6073fn reportRetryableCObjectError(comp: *Compilation, c_object: *CObject, err: anyerror) error{OutOfMemory}!void {
60746074 c_object.status = .failure_retryable;
60756075
6076 switch (comp.failCObj(c_object, "{s}", .{@errorName(err)})) {
6076 switch (comp.failCObj(c_object, "{t}", .{err})) {
60776077 error.AnalysisFail => return,
60786078 else => |e| return e,
60796079 }
......@@ -7317,7 +7317,7 @@ fn failCObj(
73177317 c_object: *CObject,
73187318 comptime format: []const u8,
73197319 args: anytype,
7320) SemaError {
7320) error{ OutOfMemory, AnalysisFail } {
73217321 @branchHint(.cold);
73227322 const diag_bundle = blk: {
73237323 const diag_bundle = try comp.gpa.create(CObject.Diag.Bundle);
......@@ -7341,7 +7341,7 @@ fn failCObjWithOwnedDiagBundle(
73417341 comp: *Compilation,
73427342 c_object: *CObject,
73437343 diag_bundle: *CObject.Diag.Bundle,
7344) SemaError {
7344) error{ OutOfMemory, AnalysisFail } {
73457345 @branchHint(.cold);
73467346 assert(diag_bundle.diags.len > 0);
73477347 {
......@@ -7357,7 +7357,7 @@ fn failCObjWithOwnedDiagBundle(
73577357 return error.AnalysisFail;
73587358}
73597359
7360fn failWin32Resource(comp: *Compilation, win32_resource: *Win32Resource, comptime format: []const u8, args: anytype) SemaError {
7360fn failWin32Resource(comp: *Compilation, win32_resource: *Win32Resource, comptime format: []const u8, args: anytype) error{ OutOfMemory, AnalysisFail } {
73617361 @branchHint(.cold);
73627362 var bundle: ErrorBundle.Wip = undefined;
73637363 try bundle.init(comp.gpa);
......@@ -7384,7 +7384,7 @@ fn failWin32ResourceWithOwnedBundle(
73847384 comp: *Compilation,
73857385 win32_resource: *Win32Resource,
73867386 err_bundle: ErrorBundle,
7387) SemaError {
7387) error{ OutOfMemory, AnalysisFail } {
73887388 @branchHint(.cold);
73897389 {
73907390 comp.mutex.lock();
src/Sema.zig+8-6
......@@ -6696,7 +6696,7 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref
66966696 const field_index = sema.structFieldIndex(block, stack_trace_ty, field_name, LazySrcLoc.unneeded) catch |err| switch (err) {
66976697 error.AnalysisFail => @panic("std.builtin.StackTrace is corrupt"),
66986698 error.ComptimeReturn, error.ComptimeBreak => unreachable,
6699 error.OutOfMemory => |e| return e,
6699 error.OutOfMemory, error.Canceled => |e| return e,
67006700 };
67016701
67026702 return try block.addInst(.{
......@@ -13924,6 +13924,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
1392413924 return sema.fail(block, operand_src, "unable to resolve '{s}': working directory has been unlinked", .{name});
1392513925 },
1392613926 error.OutOfMemory => |e| return e,
13927 error.Canceled => |e| return e,
1392713928 };
1392813929 try sema.declareDependency(.{ .embed_file = ef_idx });
1392913930
......@@ -34345,7 +34346,7 @@ pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void {
3434534346
3434634347 if (struct_type.layout == .@"packed") {
3434734348 sema.backingIntType(struct_type) catch |err| switch (err) {
34348 error.OutOfMemory, error.AnalysisFail => |e| return e,
34349 error.AnalysisFail, error.OutOfMemory, error.Canceled => |e| return e,
3434934350 error.ComptimeBreak, error.ComptimeReturn => unreachable,
3435034351 };
3435134352 return;
......@@ -34893,7 +34894,7 @@ pub fn resolveStructFieldTypes(
3489334894 defer tracked_unit.end(zcu);
3489434895
3489534896 sema.structFields(struct_type) catch |err| switch (err) {
34896 error.AnalysisFail, error.OutOfMemory => |e| return e,
34897 error.AnalysisFail, error.OutOfMemory, error.Canceled => |e| return e,
3489734898 error.ComptimeBreak, error.ComptimeReturn => unreachable,
3489834899 };
3489934900}
......@@ -34926,7 +34927,7 @@ pub fn resolveStructFieldInits(sema: *Sema, ty: Type) SemaError!void {
3492634927 defer tracked_unit.end(zcu);
3492734928
3492834929 sema.structFieldInits(struct_type) catch |err| switch (err) {
34929 error.AnalysisFail, error.OutOfMemory => |e| return e,
34930 error.AnalysisFail, error.OutOfMemory, error.Canceled => |e| return e,
3493034931 error.ComptimeBreak, error.ComptimeReturn => unreachable,
3493134932 };
3493234933 struct_type.setHaveFieldInits(ip);
......@@ -34960,7 +34961,7 @@ pub fn resolveUnionFieldTypes(sema: *Sema, ty: Type, union_type: InternPool.Load
3496034961 union_type.setStatus(ip, .field_types_wip);
3496134962 errdefer union_type.setStatus(ip, .none);
3496234963 sema.unionFields(ty.toIntern(), union_type) catch |err| switch (err) {
34963 error.AnalysisFail, error.OutOfMemory => |e| return e,
34964 error.AnalysisFail, error.OutOfMemory, error.Canceled => |e| return e,
3496434965 error.ComptimeBreak, error.ComptimeReturn => unreachable,
3496534966 };
3496634967 union_type.setStatus(ip, .have_field_types);
......@@ -37027,6 +37028,7 @@ fn notePathToComptimeAllocPtr(
3702737028
3702837029 const derivation = comptime_ptr.pointerDerivationAdvanced(arena, pt, false, sema) catch |err| switch (err) {
3702937030 error.OutOfMemory => |e| return e,
37031 error.Canceled => @panic("TODO"), // pls don't be cancelable mlugg
3703037032 error.AnalysisFail => unreachable,
3703137033 };
3703237034
......@@ -37367,7 +37369,7 @@ pub fn resolveDeclaredEnum(
3736737369 ) catch |err| switch (err) {
3736837370 error.ComptimeBreak => unreachable,
3736937371 error.ComptimeReturn => unreachable,
37370 error.OutOfMemory => |e| return e,
37372 error.OutOfMemory, error.Canceled => |e| return e,
3737137373 error.AnalysisFail => {
3737237374 if (!zcu.failed_analysis.contains(sema.owner)) {
3737337375 try zcu.transitive_failed_analysis.put(gpa, sema.owner, {});
src/Type.zig+2-1
......@@ -3837,7 +3837,7 @@ fn resolveStructInner(
38373837 }
38383838 return error.AnalysisFail;
38393839 },
3840 error.OutOfMemory => |e| return e,
3840 error.OutOfMemory, error.Canceled => |e| return e,
38413841 };
38423842}
38433843
......@@ -3896,6 +3896,7 @@ fn resolveUnionInner(
38963896 return error.AnalysisFail;
38973897 },
38983898 error.OutOfMemory => |e| return e,
3899 error.Canceled => |e| return e,
38993900 };
39003901}
39013902
src/Value.zig+7-3
......@@ -1,12 +1,15 @@
1const std = @import("std");
2const builtin = @import("builtin");
31const build_options = @import("build_options");
4const Type = @import("Type.zig");
2const builtin = @import("builtin");
3
4const std = @import("std");
5const Io = std.Io;
56const assert = std.debug.assert;
67const BigIntConst = std.math.big.int.Const;
78const BigIntMutable = std.math.big.int.Mutable;
89const Target = std.Target;
910const Allocator = std.mem.Allocator;
11
12const Type = @import("Type.zig");
1013const Zcu = @import("Zcu.zig");
1114const Sema = @import("Sema.zig");
1215const InternPool = @import("InternPool.zig");
......@@ -2410,6 +2413,7 @@ pub const PointerDeriveStep = union(enum) {
24102413pub fn pointerDerivation(ptr_val: Value, arena: Allocator, pt: Zcu.PerThread) Allocator.Error!PointerDeriveStep {
24112414 return ptr_val.pointerDerivationAdvanced(arena, pt, false, null) catch |err| switch (err) {
24122415 error.OutOfMemory => |e| return e,
2416 error.Canceled => @panic("TODO"), // pls remove from error set mlugg
24132417 error.AnalysisFail => unreachable,
24142418 };
24152419}
src/Zcu.zig+3-1
......@@ -2755,9 +2755,11 @@ pub const LazySrcLoc = struct {
27552755 }
27562756};
27572757
2758pub const SemaError = error{ OutOfMemory, AnalysisFail };
2758pub const SemaError = error{ OutOfMemory, Canceled, AnalysisFail };
27592759pub const CompileError = error{
27602760 OutOfMemory,
2761 /// The compilation update is no longer desired.
2762 Canceled,
27612763 /// When this is returned, the compile error for the failure has already been recorded.
27622764 AnalysisFail,
27632765 /// In a comptime scope, a return instruction was encountered. This error is only seen when
src/Zcu/PerThread.zig+18-9
......@@ -1,26 +1,31 @@
11//! This type provides a wrapper around a `*Zcu` for uses which require a thread `Id`.
22//! Any operation which mutates `InternPool` state lives here rather than on `Zcu`.
33
4const Air = @import("../Air.zig");
4const std = @import("std");
55const Allocator = std.mem.Allocator;
66const assert = std.debug.assert;
77const Ast = std.zig.Ast;
88const AstGen = std.zig.AstGen;
99const BigIntConst = std.math.big.int.Const;
1010const BigIntMutable = std.math.big.int.Mutable;
11const Cache = std.Build.Cache;
12const log = std.log.scoped(.zcu);
13const mem = std.mem;
14const Zir = std.zig.Zir;
15const Zoir = std.zig.Zoir;
16const ZonGen = std.zig.ZonGen;
17const Io = std.Io;
18
19const Air = @import("../Air.zig");
1120const Builtin = @import("../Builtin.zig");
1221const build_options = @import("build_options");
1322const builtin = @import("builtin");
14const Cache = std.Build.Cache;
1523const dev = @import("../dev.zig");
1624const InternPool = @import("../InternPool.zig");
1725const AnalUnit = InternPool.AnalUnit;
1826const introspect = @import("../introspect.zig");
19const log = std.log.scoped(.zcu);
2027const Module = @import("../Package.zig").Module;
2128const Sema = @import("../Sema.zig");
22const std = @import("std");
23const mem = std.mem;
2429const target_util = @import("../target.zig");
2530const trace = @import("../tracy.zig").trace;
2631const Type = @import("../Type.zig");
......@@ -29,9 +34,6 @@ const Zcu = @import("../Zcu.zig");
2934const Compilation = @import("../Compilation.zig");
3035const codegen = @import("../codegen.zig");
3136const crash_report = @import("../crash_report.zig");
32const Zir = std.zig.Zir;
33const Zoir = std.zig.Zoir;
34const ZonGen = std.zig.ZonGen;
3537
3638zcu: *Zcu,
3739
......@@ -678,6 +680,7 @@ pub fn ensureMemoizedStateUpToDate(pt: Zcu.PerThread, stage: InternPool.Memoized
678680 // TODO: same as for `ensureComptimeUnitUpToDate` etc
679681 return error.OutOfMemory;
680682 },
683 error.Canceled => |e| return e,
681684 error.ComptimeReturn => unreachable,
682685 error.ComptimeBreak => unreachable,
683686 };
......@@ -842,6 +845,7 @@ pub fn ensureComptimeUnitUpToDate(pt: Zcu.PerThread, cu_id: InternPool.ComptimeU
842845 // for reporting OOM errors without allocating.
843846 return error.OutOfMemory;
844847 },
848 error.Canceled => |e| return e,
845849 error.ComptimeReturn => unreachable,
846850 error.ComptimeBreak => unreachable,
847851 };
......@@ -1030,6 +1034,7 @@ pub fn ensureNavValUpToDate(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu
10301034 // for reporting OOM errors without allocating.
10311035 return error.OutOfMemory;
10321036 },
1037 error.Canceled => |e| return e,
10331038 error.ComptimeReturn => unreachable,
10341039 error.ComptimeBreak => unreachable,
10351040 };
......@@ -1443,6 +1448,7 @@ pub fn ensureNavTypeUpToDate(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zc
14431448 // for reporting OOM errors without allocating.
14441449 return error.OutOfMemory;
14451450 },
1451 error.Canceled => |e| return e,
14461452 error.ComptimeReturn => unreachable,
14471453 error.ComptimeBreak => unreachable,
14481454 };
......@@ -1668,6 +1674,7 @@ pub fn ensureFuncBodyUpToDate(pt: Zcu.PerThread, func_index: InternPool.Index) Z
16681674 // for reporting OOM errors without allocating.
16691675 return error.OutOfMemory;
16701676 },
1677 error.Canceled => |e| return e,
16711678 };
16721679
16731680 if (was_outdated) {
......@@ -2360,6 +2367,7 @@ pub fn embedFile(
23602367 import_string: []const u8,
23612368) error{
23622369 OutOfMemory,
2370 Canceled,
23632371 ImportOutsideModulePath,
23642372 CurrentWorkingDirectoryUnlinked,
23652373}!Zcu.EmbedFile.Index {
......@@ -4123,7 +4131,7 @@ fn recreateEnumType(
41234131 pt: Zcu.PerThread,
41244132 old_ty: InternPool.Index,
41254133 key: InternPool.Key.NamespaceType.Declared,
4126) Allocator.Error!InternPool.Index {
4134) (Allocator.Error || Io.Cancelable)!InternPool.Index {
41274135 const zcu = pt.zcu;
41284136 const gpa = zcu.gpa;
41294137 const ip = &zcu.intern_pool;
......@@ -4234,6 +4242,7 @@ fn recreateEnumType(
42344242 body_end,
42354243 ) catch |err| switch (err) {
42364244 error.OutOfMemory => |e| return e,
4245 error.Canceled => |e| return e,
42374246 error.AnalysisFail => {}, // call sites are responsible for checking `[transitive_]failed_analysis` to detect this
42384247 };
42394248
src/print_value.zig+2
......@@ -27,6 +27,7 @@ pub fn formatSema(ctx: FormatContext, writer: *Writer) Writer.Error!void {
2727 error.OutOfMemory => @panic("OOM"), // We're not allowed to return this from a format function
2828 error.ComptimeBreak, error.ComptimeReturn => unreachable,
2929 error.AnalysisFail => unreachable, // TODO: re-evaluate when we use `sema` more fully
30 error.Canceled => @panic("TODO"), // pls stop returning this error mlugg
3031 else => |e| return e,
3132 };
3233}
......@@ -36,6 +37,7 @@ pub fn format(ctx: FormatContext, writer: *Writer) Writer.Error!void {
3637 return print(ctx.val, writer, ctx.depth, ctx.pt, null) catch |err| switch (err) {
3738 error.OutOfMemory => @panic("OOM"), // We're not allowed to return this from a format function
3839 error.ComptimeBreak, error.ComptimeReturn, error.AnalysisFail => unreachable,
40 error.Canceled => @panic("TODO"), // pls stop returning this error mlugg
3941 else => |e| return e,
4042 };
4143}