frontend: introduce error.Canceled

This commit is contained in:
Andrew Kelley 2025-11-24 11:58:24 -08:00
parent 3f34f5e433
commit ece62a0223
7 changed files with 55 additions and 35 deletions

View file

@ -2851,6 +2851,7 @@ fn cleanupAfterUpdate(comp: *Compilation, tmp_dir_rand_int: u64) void {
pub const UpdateError = error{ pub const UpdateError = error{
OutOfMemory, OutOfMemory,
Canceled,
Unexpected, Unexpected,
CurrentWorkingDirectoryUnlinked, CurrentWorkingDirectoryUnlinked,
}; };
@ -2930,6 +2931,7 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) UpdateE
}, },
}, },
error.OutOfMemory => return error.OutOfMemory, error.OutOfMemory => return error.OutOfMemory,
error.Canceled => return error.Canceled,
error.InvalidFormat => return comp.setMiscFailure( error.InvalidFormat => return comp.setMiscFailure(
.check_whole_cache, .check_whole_cache,
"failed to check cache: invalid manifest file format", "failed to check cache: invalid manifest file format",
@ -5010,7 +5012,7 @@ fn performAllTheWork(
} }
} }
const JobError = Allocator.Error; const JobError = Allocator.Error || Io.Cancelable;
pub fn queueJob(comp: *Compilation, job: Job) !void { pub fn queueJob(comp: *Compilation, job: Job) !void {
try comp.work_queues[Job.stage(job)].pushBack(comp.gpa, job); 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 {
pt.ensureFuncBodyUpToDate(func) catch |err| switch (err) { pt.ensureFuncBodyUpToDate(func) catch |err| switch (err) {
error.OutOfMemory => |e| return e, error.OutOfMemory => |e| return e,
error.Canceled => |e| return e,
error.AnalysisFail => return, error.AnalysisFail => return,
}; };
}, },
@ -5137,6 +5140,7 @@ fn processOneJob(tid: usize, comp: *Compilation, job: Job) JobError!void {
}; };
maybe_err catch |err| switch (err) { maybe_err catch |err| switch (err) {
error.OutOfMemory => |e| return e, error.OutOfMemory => |e| return e,
error.Canceled => |e| return e,
error.AnalysisFail => return, error.AnalysisFail => return,
}; };
@ -5166,7 +5170,7 @@ fn processOneJob(tid: usize, comp: *Compilation, job: Job) JobError!void {
const pt: Zcu.PerThread = .activate(comp.zcu.?, @enumFromInt(tid)); const pt: Zcu.PerThread = .activate(comp.zcu.?, @enumFromInt(tid));
defer pt.deactivate(); defer pt.deactivate();
Type.fromInterned(ty).resolveFully(pt) catch |err| switch (err) { Type.fromInterned(ty).resolveFully(pt) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory, error.OutOfMemory, error.Canceled => |e| return e,
error.AnalysisFail => return, error.AnalysisFail => return,
}; };
}, },
@ -5177,7 +5181,7 @@ fn processOneJob(tid: usize, comp: *Compilation, job: Job) JobError!void {
const pt: Zcu.PerThread = .activate(comp.zcu.?, @enumFromInt(tid)); const pt: Zcu.PerThread = .activate(comp.zcu.?, @enumFromInt(tid));
defer pt.deactivate(); defer pt.deactivate();
pt.semaMod(mod) catch |err| switch (err) { pt.semaMod(mod) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory, error.OutOfMemory, error.Canceled => |e| return e,
error.AnalysisFail => return, error.AnalysisFail => return,
}; };
}, },
@ -5190,8 +5194,8 @@ fn processOneJob(tid: usize, comp: *Compilation, job: Job) JobError!void {
// TODO Surface more error details. // TODO Surface more error details.
comp.lockAndSetMiscFailure( comp.lockAndSetMiscFailure(
.windows_import_lib, .windows_import_lib,
"unable to generate DLL import .lib file for {s}: {s}", "unable to generate DLL import .lib file for {s}: {t}",
.{ link_lib, @errorName(err) }, .{ link_lib, err },
); );
}; };
}, },
@ -6066,14 +6070,10 @@ fn buildLibZigC(comp: *Compilation, prog_node: std.Progress.Node) void {
}; };
} }
fn reportRetryableCObjectError( fn reportRetryableCObjectError(comp: *Compilation, c_object: *CObject, err: anyerror) error{OutOfMemory}!void {
comp: *Compilation,
c_object: *CObject,
err: anyerror,
) error{OutOfMemory}!void {
c_object.status = .failure_retryable; c_object.status = .failure_retryable;
switch (comp.failCObj(c_object, "{s}", .{@errorName(err)})) { switch (comp.failCObj(c_object, "{t}", .{err})) {
error.AnalysisFail => return, error.AnalysisFail => return,
else => |e| return e, else => |e| return e,
} }
@ -7317,7 +7317,7 @@ fn failCObj(
c_object: *CObject, c_object: *CObject,
comptime format: []const u8, comptime format: []const u8,
args: anytype, args: anytype,
) SemaError { ) error{ OutOfMemory, AnalysisFail } {
@branchHint(.cold); @branchHint(.cold);
const diag_bundle = blk: { const diag_bundle = blk: {
const diag_bundle = try comp.gpa.create(CObject.Diag.Bundle); const diag_bundle = try comp.gpa.create(CObject.Diag.Bundle);
@ -7341,7 +7341,7 @@ fn failCObjWithOwnedDiagBundle(
comp: *Compilation, comp: *Compilation,
c_object: *CObject, c_object: *CObject,
diag_bundle: *CObject.Diag.Bundle, diag_bundle: *CObject.Diag.Bundle,
) SemaError { ) error{ OutOfMemory, AnalysisFail } {
@branchHint(.cold); @branchHint(.cold);
assert(diag_bundle.diags.len > 0); assert(diag_bundle.diags.len > 0);
{ {
@ -7357,7 +7357,7 @@ fn failCObjWithOwnedDiagBundle(
return error.AnalysisFail; return error.AnalysisFail;
} }
fn failWin32Resource(comp: *Compilation, win32_resource: *Win32Resource, comptime format: []const u8, args: anytype) SemaError { fn failWin32Resource(comp: *Compilation, win32_resource: *Win32Resource, comptime format: []const u8, args: anytype) error{ OutOfMemory, AnalysisFail } {
@branchHint(.cold); @branchHint(.cold);
var bundle: ErrorBundle.Wip = undefined; var bundle: ErrorBundle.Wip = undefined;
try bundle.init(comp.gpa); try bundle.init(comp.gpa);
@ -7384,7 +7384,7 @@ fn failWin32ResourceWithOwnedBundle(
comp: *Compilation, comp: *Compilation,
win32_resource: *Win32Resource, win32_resource: *Win32Resource,
err_bundle: ErrorBundle, err_bundle: ErrorBundle,
) SemaError { ) error{ OutOfMemory, AnalysisFail } {
@branchHint(.cold); @branchHint(.cold);
{ {
comp.mutex.lock(); comp.mutex.lock();

View file

@ -6696,7 +6696,7 @@ pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref
const field_index = sema.structFieldIndex(block, stack_trace_ty, field_name, LazySrcLoc.unneeded) catch |err| switch (err) { const field_index = sema.structFieldIndex(block, stack_trace_ty, field_name, LazySrcLoc.unneeded) catch |err| switch (err) {
error.AnalysisFail => @panic("std.builtin.StackTrace is corrupt"), error.AnalysisFail => @panic("std.builtin.StackTrace is corrupt"),
error.ComptimeReturn, error.ComptimeBreak => unreachable, error.ComptimeReturn, error.ComptimeBreak => unreachable,
error.OutOfMemory => |e| return e, error.OutOfMemory, error.Canceled => |e| return e,
}; };
return try block.addInst(.{ return try block.addInst(.{
@ -13924,6 +13924,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
return sema.fail(block, operand_src, "unable to resolve '{s}': working directory has been unlinked", .{name}); return sema.fail(block, operand_src, "unable to resolve '{s}': working directory has been unlinked", .{name});
}, },
error.OutOfMemory => |e| return e, error.OutOfMemory => |e| return e,
error.Canceled => |e| return e,
}; };
try sema.declareDependency(.{ .embed_file = ef_idx }); try sema.declareDependency(.{ .embed_file = ef_idx });
@ -34345,7 +34346,7 @@ pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void {
if (struct_type.layout == .@"packed") { if (struct_type.layout == .@"packed") {
sema.backingIntType(struct_type) catch |err| switch (err) { sema.backingIntType(struct_type) catch |err| switch (err) {
error.OutOfMemory, error.AnalysisFail => |e| return e, error.AnalysisFail, error.OutOfMemory, error.Canceled => |e| return e,
error.ComptimeBreak, error.ComptimeReturn => unreachable, error.ComptimeBreak, error.ComptimeReturn => unreachable,
}; };
return; return;
@ -34893,7 +34894,7 @@ pub fn resolveStructFieldTypes(
defer tracked_unit.end(zcu); defer tracked_unit.end(zcu);
sema.structFields(struct_type) catch |err| switch (err) { sema.structFields(struct_type) catch |err| switch (err) {
error.AnalysisFail, error.OutOfMemory => |e| return e, error.AnalysisFail, error.OutOfMemory, error.Canceled => |e| return e,
error.ComptimeBreak, error.ComptimeReturn => unreachable, error.ComptimeBreak, error.ComptimeReturn => unreachable,
}; };
} }
@ -34926,7 +34927,7 @@ pub fn resolveStructFieldInits(sema: *Sema, ty: Type) SemaError!void {
defer tracked_unit.end(zcu); defer tracked_unit.end(zcu);
sema.structFieldInits(struct_type) catch |err| switch (err) { sema.structFieldInits(struct_type) catch |err| switch (err) {
error.AnalysisFail, error.OutOfMemory => |e| return e, error.AnalysisFail, error.OutOfMemory, error.Canceled => |e| return e,
error.ComptimeBreak, error.ComptimeReturn => unreachable, error.ComptimeBreak, error.ComptimeReturn => unreachable,
}; };
struct_type.setHaveFieldInits(ip); struct_type.setHaveFieldInits(ip);
@ -34960,7 +34961,7 @@ pub fn resolveUnionFieldTypes(sema: *Sema, ty: Type, union_type: InternPool.Load
union_type.setStatus(ip, .field_types_wip); union_type.setStatus(ip, .field_types_wip);
errdefer union_type.setStatus(ip, .none); errdefer union_type.setStatus(ip, .none);
sema.unionFields(ty.toIntern(), union_type) catch |err| switch (err) { sema.unionFields(ty.toIntern(), union_type) catch |err| switch (err) {
error.AnalysisFail, error.OutOfMemory => |e| return e, error.AnalysisFail, error.OutOfMemory, error.Canceled => |e| return e,
error.ComptimeBreak, error.ComptimeReturn => unreachable, error.ComptimeBreak, error.ComptimeReturn => unreachable,
}; };
union_type.setStatus(ip, .have_field_types); union_type.setStatus(ip, .have_field_types);
@ -37027,6 +37028,7 @@ fn notePathToComptimeAllocPtr(
const derivation = comptime_ptr.pointerDerivationAdvanced(arena, pt, false, sema) catch |err| switch (err) { const derivation = comptime_ptr.pointerDerivationAdvanced(arena, pt, false, sema) catch |err| switch (err) {
error.OutOfMemory => |e| return e, error.OutOfMemory => |e| return e,
error.Canceled => @panic("TODO"), // pls don't be cancelable mlugg
error.AnalysisFail => unreachable, error.AnalysisFail => unreachable,
}; };
@ -37367,7 +37369,7 @@ pub fn resolveDeclaredEnum(
) catch |err| switch (err) { ) catch |err| switch (err) {
error.ComptimeBreak => unreachable, error.ComptimeBreak => unreachable,
error.ComptimeReturn => unreachable, error.ComptimeReturn => unreachable,
error.OutOfMemory => |e| return e, error.OutOfMemory, error.Canceled => |e| return e,
error.AnalysisFail => { error.AnalysisFail => {
if (!zcu.failed_analysis.contains(sema.owner)) { if (!zcu.failed_analysis.contains(sema.owner)) {
try zcu.transitive_failed_analysis.put(gpa, sema.owner, {}); try zcu.transitive_failed_analysis.put(gpa, sema.owner, {});

View file

@ -3837,7 +3837,7 @@ fn resolveStructInner(
} }
return error.AnalysisFail; return error.AnalysisFail;
}, },
error.OutOfMemory => |e| return e, error.OutOfMemory, error.Canceled => |e| return e,
}; };
} }
@ -3896,6 +3896,7 @@ fn resolveUnionInner(
return error.AnalysisFail; return error.AnalysisFail;
}, },
error.OutOfMemory => |e| return e, error.OutOfMemory => |e| return e,
error.Canceled => |e| return e,
}; };
} }

View file

@ -1,12 +1,15 @@
const std = @import("std");
const builtin = @import("builtin");
const build_options = @import("build_options"); const build_options = @import("build_options");
const Type = @import("Type.zig"); const builtin = @import("builtin");
const std = @import("std");
const Io = std.Io;
const assert = std.debug.assert; const assert = std.debug.assert;
const BigIntConst = std.math.big.int.Const; const BigIntConst = std.math.big.int.Const;
const BigIntMutable = std.math.big.int.Mutable; const BigIntMutable = std.math.big.int.Mutable;
const Target = std.Target; const Target = std.Target;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const Type = @import("Type.zig");
const Zcu = @import("Zcu.zig"); const Zcu = @import("Zcu.zig");
const Sema = @import("Sema.zig"); const Sema = @import("Sema.zig");
const InternPool = @import("InternPool.zig"); const InternPool = @import("InternPool.zig");
@ -2410,6 +2413,7 @@ pub const PointerDeriveStep = union(enum) {
pub fn pointerDerivation(ptr_val: Value, arena: Allocator, pt: Zcu.PerThread) Allocator.Error!PointerDeriveStep { pub fn pointerDerivation(ptr_val: Value, arena: Allocator, pt: Zcu.PerThread) Allocator.Error!PointerDeriveStep {
return ptr_val.pointerDerivationAdvanced(arena, pt, false, null) catch |err| switch (err) { return ptr_val.pointerDerivationAdvanced(arena, pt, false, null) catch |err| switch (err) {
error.OutOfMemory => |e| return e, error.OutOfMemory => |e| return e,
error.Canceled => @panic("TODO"), // pls remove from error set mlugg
error.AnalysisFail => unreachable, error.AnalysisFail => unreachable,
}; };
} }

View file

@ -2755,9 +2755,11 @@ pub const LazySrcLoc = struct {
} }
}; };
pub const SemaError = error{ OutOfMemory, AnalysisFail }; pub const SemaError = error{ OutOfMemory, Canceled, AnalysisFail };
pub const CompileError = error{ pub const CompileError = error{
OutOfMemory, OutOfMemory,
/// The compilation update is no longer desired.
Canceled,
/// When this is returned, the compile error for the failure has already been recorded. /// When this is returned, the compile error for the failure has already been recorded.
AnalysisFail, AnalysisFail,
/// In a comptime scope, a return instruction was encountered. This error is only seen when /// In a comptime scope, a return instruction was encountered. This error is only seen when

View file

@ -1,26 +1,31 @@
//! This type provides a wrapper around a `*Zcu` for uses which require a thread `Id`. //! This type provides a wrapper around a `*Zcu` for uses which require a thread `Id`.
//! Any operation which mutates `InternPool` state lives here rather than on `Zcu`. //! Any operation which mutates `InternPool` state lives here rather than on `Zcu`.
const Air = @import("../Air.zig"); const std = @import("std");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const assert = std.debug.assert; const assert = std.debug.assert;
const Ast = std.zig.Ast; const Ast = std.zig.Ast;
const AstGen = std.zig.AstGen; const AstGen = std.zig.AstGen;
const BigIntConst = std.math.big.int.Const; const BigIntConst = std.math.big.int.Const;
const BigIntMutable = std.math.big.int.Mutable; const BigIntMutable = std.math.big.int.Mutable;
const Cache = std.Build.Cache;
const log = std.log.scoped(.zcu);
const mem = std.mem;
const Zir = std.zig.Zir;
const Zoir = std.zig.Zoir;
const ZonGen = std.zig.ZonGen;
const Io = std.Io;
const Air = @import("../Air.zig");
const Builtin = @import("../Builtin.zig"); const Builtin = @import("../Builtin.zig");
const build_options = @import("build_options"); const build_options = @import("build_options");
const builtin = @import("builtin"); const builtin = @import("builtin");
const Cache = std.Build.Cache;
const dev = @import("../dev.zig"); const dev = @import("../dev.zig");
const InternPool = @import("../InternPool.zig"); const InternPool = @import("../InternPool.zig");
const AnalUnit = InternPool.AnalUnit; const AnalUnit = InternPool.AnalUnit;
const introspect = @import("../introspect.zig"); const introspect = @import("../introspect.zig");
const log = std.log.scoped(.zcu);
const Module = @import("../Package.zig").Module; const Module = @import("../Package.zig").Module;
const Sema = @import("../Sema.zig"); const Sema = @import("../Sema.zig");
const std = @import("std");
const mem = std.mem;
const target_util = @import("../target.zig"); const target_util = @import("../target.zig");
const trace = @import("../tracy.zig").trace; const trace = @import("../tracy.zig").trace;
const Type = @import("../Type.zig"); const Type = @import("../Type.zig");
@ -29,9 +34,6 @@ const Zcu = @import("../Zcu.zig");
const Compilation = @import("../Compilation.zig"); const Compilation = @import("../Compilation.zig");
const codegen = @import("../codegen.zig"); const codegen = @import("../codegen.zig");
const crash_report = @import("../crash_report.zig"); const crash_report = @import("../crash_report.zig");
const Zir = std.zig.Zir;
const Zoir = std.zig.Zoir;
const ZonGen = std.zig.ZonGen;
zcu: *Zcu, zcu: *Zcu,
@ -678,6 +680,7 @@ pub fn ensureMemoizedStateUpToDate(pt: Zcu.PerThread, stage: InternPool.Memoized
// TODO: same as for `ensureComptimeUnitUpToDate` etc // TODO: same as for `ensureComptimeUnitUpToDate` etc
return error.OutOfMemory; return error.OutOfMemory;
}, },
error.Canceled => |e| return e,
error.ComptimeReturn => unreachable, error.ComptimeReturn => unreachable,
error.ComptimeBreak => unreachable, error.ComptimeBreak => unreachable,
}; };
@ -842,6 +845,7 @@ pub fn ensureComptimeUnitUpToDate(pt: Zcu.PerThread, cu_id: InternPool.ComptimeU
// for reporting OOM errors without allocating. // for reporting OOM errors without allocating.
return error.OutOfMemory; return error.OutOfMemory;
}, },
error.Canceled => |e| return e,
error.ComptimeReturn => unreachable, error.ComptimeReturn => unreachable,
error.ComptimeBreak => unreachable, error.ComptimeBreak => unreachable,
}; };
@ -1030,6 +1034,7 @@ pub fn ensureNavValUpToDate(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zcu
// for reporting OOM errors without allocating. // for reporting OOM errors without allocating.
return error.OutOfMemory; return error.OutOfMemory;
}, },
error.Canceled => |e| return e,
error.ComptimeReturn => unreachable, error.ComptimeReturn => unreachable,
error.ComptimeBreak => unreachable, error.ComptimeBreak => unreachable,
}; };
@ -1443,6 +1448,7 @@ pub fn ensureNavTypeUpToDate(pt: Zcu.PerThread, nav_id: InternPool.Nav.Index) Zc
// for reporting OOM errors without allocating. // for reporting OOM errors without allocating.
return error.OutOfMemory; return error.OutOfMemory;
}, },
error.Canceled => |e| return e,
error.ComptimeReturn => unreachable, error.ComptimeReturn => unreachable,
error.ComptimeBreak => unreachable, error.ComptimeBreak => unreachable,
}; };
@ -1668,6 +1674,7 @@ pub fn ensureFuncBodyUpToDate(pt: Zcu.PerThread, func_index: InternPool.Index) Z
// for reporting OOM errors without allocating. // for reporting OOM errors without allocating.
return error.OutOfMemory; return error.OutOfMemory;
}, },
error.Canceled => |e| return e,
}; };
if (was_outdated) { if (was_outdated) {
@ -2360,6 +2367,7 @@ pub fn embedFile(
import_string: []const u8, import_string: []const u8,
) error{ ) error{
OutOfMemory, OutOfMemory,
Canceled,
ImportOutsideModulePath, ImportOutsideModulePath,
CurrentWorkingDirectoryUnlinked, CurrentWorkingDirectoryUnlinked,
}!Zcu.EmbedFile.Index { }!Zcu.EmbedFile.Index {
@ -4123,7 +4131,7 @@ fn recreateEnumType(
pt: Zcu.PerThread, pt: Zcu.PerThread,
old_ty: InternPool.Index, old_ty: InternPool.Index,
key: InternPool.Key.NamespaceType.Declared, key: InternPool.Key.NamespaceType.Declared,
) Allocator.Error!InternPool.Index { ) (Allocator.Error || Io.Cancelable)!InternPool.Index {
const zcu = pt.zcu; const zcu = pt.zcu;
const gpa = zcu.gpa; const gpa = zcu.gpa;
const ip = &zcu.intern_pool; const ip = &zcu.intern_pool;
@ -4234,6 +4242,7 @@ fn recreateEnumType(
body_end, body_end,
) catch |err| switch (err) { ) catch |err| switch (err) {
error.OutOfMemory => |e| return e, error.OutOfMemory => |e| return e,
error.Canceled => |e| return e,
error.AnalysisFail => {}, // call sites are responsible for checking `[transitive_]failed_analysis` to detect this error.AnalysisFail => {}, // call sites are responsible for checking `[transitive_]failed_analysis` to detect this
}; };

View file

@ -27,6 +27,7 @@ pub fn formatSema(ctx: FormatContext, writer: *Writer) Writer.Error!void {
error.OutOfMemory => @panic("OOM"), // We're not allowed to return this from a format function error.OutOfMemory => @panic("OOM"), // We're not allowed to return this from a format function
error.ComptimeBreak, error.ComptimeReturn => unreachable, error.ComptimeBreak, error.ComptimeReturn => unreachable,
error.AnalysisFail => unreachable, // TODO: re-evaluate when we use `sema` more fully error.AnalysisFail => unreachable, // TODO: re-evaluate when we use `sema` more fully
error.Canceled => @panic("TODO"), // pls stop returning this error mlugg
else => |e| return e, else => |e| return e,
}; };
} }
@ -36,6 +37,7 @@ pub fn format(ctx: FormatContext, writer: *Writer) Writer.Error!void {
return print(ctx.val, writer, ctx.depth, ctx.pt, null) catch |err| switch (err) { return print(ctx.val, writer, ctx.depth, ctx.pt, null) catch |err| switch (err) {
error.OutOfMemory => @panic("OOM"), // We're not allowed to return this from a format function error.OutOfMemory => @panic("OOM"), // We're not allowed to return this from a format function
error.ComptimeBreak, error.ComptimeReturn, error.AnalysisFail => unreachable, error.ComptimeBreak, error.ComptimeReturn, error.AnalysisFail => unreachable,
error.Canceled => @panic("TODO"), // pls stop returning this error mlugg
else => |e| return e, else => |e| return e,
}; };
} }