cli: add explicit built-in error values
Add the error_values feature and error.const through specification, AIR, verification, interpretation and source lowering. Error values are Copy data with no allocation, effects or authority. Keep enum.make semantics frozen. Cover all registered tags, feature negotiation, payload/type errors and authority rejection in AIR/source; verify canonical and hosted CLI behavior. Record reference-bundle costs and the map preflight-validation prerequisite.
This commit is contained in:
1 parent
89c63c306b
commit
1eaf968f4f
56 files changed
+823
-4
No files matched your search
@@ -31,6 +31,7 @@ pub const registry = [_]Feature{
|
||||
.{ .name = "channel_transfer", .depends = &.{ "cancellation", "capabilities", "channels", "effects", "heap", "ownership", "resources", "tasks" }, .status = .reserved, .phase = null },
|
||||
.{ .name = "channels", .depends = &.{ "cancellation", "capabilities", "effects", "heap", "ownership", "resources", "tasks" }, .status = .published, .phase = 3 },
|
||||
.{ .name = "effects", .depends = &.{}, .status = .published, .phase = 2 },
|
||||
.{ .name = "error_values", .depends = &.{ "capabilities", "effects", "heap", "ownership", "resources" }, .status = .published, .phase = 6 },
|
||||
.{ .name = "guard_transfer", .depends = &.{ "cancellation", "effects", "ownership", "sync", "tasks" }, .status = .reserved, .phase = null },
|
||||
.{ .name = "heap", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .published, .phase = 2 },
|
||||
.{ .name = "islands", .depends = &.{ "capabilities", "effects", "heap", "ownership", "resources" }, .status = .published, .phase = 4 },
|
||||
@@ -50,7 +51,7 @@ pub const registry = [_]Feature{
|
||||
};
|
||||
|
||||
/// Features implemented by this toolchain, sorted.
|
||||
pub const supported = [_][]const u8{ "arc", "arrays", "atomics", "cancellation", "capabilities", "channels", "effects", "heap", "islands", "linear_aggregates", "linear_buffers", "ownership", "regions", "resources", "sync", "tasks", "unsafe" };
|
||||
pub const supported = [_][]const u8{ "arc", "arrays", "atomics", "cancellation", "capabilities", "channels", "effects", "error_values", "heap", "islands", "linear_aggregates", "linear_buffers", "ownership", "regions", "resources", "sync", "tasks", "unsafe" };
|
||||
|
||||
pub const Reason = enum {
|
||||
unknown_feature,
|
||||
|
||||
+3
-1
@@ -306,6 +306,8 @@ pub const Op = union(enum) {
|
||||
assume: ValueId,
|
||||
|
||||
@"const": struct { ty: Type, bits: u128 },
|
||||
/// Pure built-in error data (feature error_values, spec 05 §14).
|
||||
error_const: struct { which: authority.Builtin, variant: u32 },
|
||||
copy: ValueId,
|
||||
arith: struct { op: ArithOp, mode: ArithMode, ty: IntType, a: ValueId, b: ValueId },
|
||||
bit: struct { op: BitOp, ty: Type, a: ValueId, b: ValueId },
|
||||
@@ -559,7 +561,7 @@ pub fn forEachUse(op: Op, ctx: anytype, comptime f: fn (@TypeOf(ctx), ValueId) v
|
||||
},
|
||||
inline .buffer_pop, .buffer_free => |x| f(ctx, x.value),
|
||||
.buffer_len, .buffer_capacity, .buffer_view => |v| f(ctx, v),
|
||||
.@"const", .trap => {},
|
||||
.@"const", .error_const, .trap => {},
|
||||
.copy, .assert => |v| f(ctx, v),
|
||||
.arith => |x| {
|
||||
f(ctx, x.a);
|
||||
|
||||
+15
-1
@@ -873,6 +873,20 @@ const Parser = struct {
|
||||
const head = parts[0];
|
||||
|
||||
const op: air.Op = op: {
|
||||
if (eql(op_tok.text, "error.const")) {
|
||||
try p.requireFeature("error_values", op_tok.span, op_tok.text);
|
||||
const t = try p.expect(.word, "built-in error family and variant");
|
||||
const dot = std.mem.indexOfScalar(u8, t.text, '.') orelse return p.fail(t, "`B.variant`");
|
||||
const b = std.meta.stringToEnum(authority.Builtin, t.text[0..dot]) orelse {
|
||||
try p.report("E0004", t.span, "unknown built-in error family", &.{.{ .name = "name", .value = .{ .str = t.text[0..dot] } }});
|
||||
return error.SyntaxError;
|
||||
};
|
||||
const variant = b.variantIndex(t.text[dot + 1 ..]) orelse {
|
||||
try p.report("E0004", t.span, "unknown built-in error variant", &.{.{ .name = "name", .value = .{ .str = t.text[dot + 1 ..] } }});
|
||||
return error.SyntaxError;
|
||||
};
|
||||
break :op .{ .error_const = .{ .which = b, .variant = variant } };
|
||||
}
|
||||
if (eql(head, "const") and parts.len == 2) {
|
||||
if (eql(parts[1], "bool")) {
|
||||
const lit = try p.expect(.word, "`true` or `false`");
|
||||
@@ -1110,7 +1124,7 @@ const Parser = struct {
|
||||
.task_shield_begin, .channel_dup, .mutex_make, .mutex_take, .mutex_borrow => true,
|
||||
.island_root, .island_borrow, .island_freeze, .frozen_root, .frozen_borrow, .arc_from_frozen, .arc_clone, .arc_root, .arc_borrow, .iref_eq => true,
|
||||
.atomic_new, .atomic_take, .atomic_load, .atomic_rmw, .atomic_cmpxchg => true,
|
||||
.@"const", .copy, .arith, .bit, .cmp, .cast, .struct_make, .struct_get, .enum_make => true,
|
||||
.@"const", .error_const, .copy, .arith, .bit, .cmp, .cast, .struct_make, .struct_get, .enum_make => true,
|
||||
.local_alloc, .local_load, .borrow_ref, .borrow_mut, .borrow_field, .load => true,
|
||||
.buffer_len, .buffer_capacity, .buffer_view, .array_splat, .array_make, .value_swap, .borrow_index, .slice_make, .slice_len, .slice_sub, .slice_split => true,
|
||||
else => false,
|
||||
|
||||
@@ -179,6 +179,7 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
},
|
||||
else => unreachable,
|
||||
},
|
||||
.error_const => |x| try w.print("error.const {s}.{s}", .{ @tagName(x.which), x.which.variants()[x.variant] }),
|
||||
.copy => |v| {
|
||||
try w.writeAll("copy ");
|
||||
try value(f, v, w);
|
||||
@@ -717,6 +718,22 @@ test "round-trip whole-value linear operations" {
|
||||
);
|
||||
}
|
||||
|
||||
test "round-trip explicit error values" {
|
||||
try roundTrip(
|
||||
\\module test.errors
|
||||
\\air 0
|
||||
\\lang 0.1
|
||||
\\features [resources, ownership, heap, error_values, effects, capabilities]
|
||||
\\fn @main() -> alloc_error
|
||||
\\effects []
|
||||
\\entry:
|
||||
\\ %io = error.const io_error.invalid_input
|
||||
\\ %cap = error.const cap_error.not_subset
|
||||
\\ %e = error.const alloc_error.budget_exceeded
|
||||
\\ ret %e
|
||||
);
|
||||
}
|
||||
|
||||
test "round-trip initialized buffers" {
|
||||
try roundTrip(
|
||||
\\module test.buffers
|
||||
|
||||
@@ -581,6 +581,7 @@ const Lowerer = struct {
|
||||
.{ "arrays", &.{ "array<", "slice", "array.splat", "borrow.index" } },
|
||||
.{ "ownership", &.{ "local.", "borrow.", " load ", "= load ", "store ", "ref<", "mut<" } },
|
||||
.{ "linear_buffers", &.{ "buffer<", "buffer." } },
|
||||
.{ "error_values", &.{"error.const "} },
|
||||
.{ "linear_aggregates", &.{ "array.make ", "struct.unpack ", "array.unpack ", "enum.take ", "value.swap ", "own.pack " } },
|
||||
.{ "heap", &.{ "own.", "own<", "own_array<", "alloc_error" } },
|
||||
.{ "regions", &.{"region"} },
|
||||
@@ -1401,7 +1402,7 @@ const Lowerer = struct {
|
||||
|
||||
fn typeOfNoFail(l: *Lowerer, t: ast.Type) ?Ty {
|
||||
return switch (t.kind) {
|
||||
.name => |n| if (isIntName(n)) Ty{ .int = n } else if (std.mem.eql(u8, n, "bool")) .bool else l.enumOrStruct(n, t.span),
|
||||
.name => |n| if (isIntName(n)) Ty{ .int = n } else if (std.mem.eql(u8, n, "bool")) .bool else if (std.meta.stringToEnum(authority.Builtin, n)) |b| Ty{ .builtin = b } else l.enumOrStruct(n, t.span),
|
||||
else => null,
|
||||
};
|
||||
}
|
||||
@@ -1413,6 +1414,7 @@ const Lowerer = struct {
|
||||
}
|
||||
|
||||
fn enumType(l: *Lowerer, n: []const u8, span: Span) ?Ty {
|
||||
if (std.meta.stringToEnum(authority.Builtin, n)) |b| return .{ .builtin = b };
|
||||
const t = l.enumOrStruct(n, span) orelse return null;
|
||||
return if (l.m.types[t.named].kind == .@"enum") t else null;
|
||||
}
|
||||
@@ -1751,6 +1753,11 @@ const Lowerer = struct {
|
||||
|
||||
fn variantValue(l: *Lowerer, e: ast.Expr, enum_name: []const u8, variant: []const u8, args: []const ast.Expr) Error!Value {
|
||||
const t = l.enumType(enum_name, e.span) orelse return l.fail("E7002", e.span, "unknown enum", &.{.{ .name = "name", .value = enum_name }});
|
||||
if (t == .builtin) {
|
||||
_ = t.builtin.variantIndex(variant) orelse return l.fail("E7002", e.span, "no such variant", &.{ .{ .name = "enum", .value = enum_name }, .{ .name = "variant", .value = variant } });
|
||||
if (args.len != 0) return l.fail("E7005", e.span, "built-in error variants have no payload", &.{.{ .name = "variant", .value = variant }});
|
||||
return l.def("e", t, try l.fmt("error.const {s}.{s}", .{ enum_name, variant }), e.span);
|
||||
}
|
||||
const vs = l.m.types[t.named].kind.@"enum";
|
||||
const v = for (vs) |x| {
|
||||
if (std.mem.eql(u8, x.name, variant)) break x;
|
||||
|
||||
@@ -846,6 +846,7 @@ const Machine = struct {
|
||||
.bool => .{ .boolean = c.bits != 0 },
|
||||
else => unreachable,
|
||||
}),
|
||||
.error_const => |x| return bind(vals, inst.result, .{ .builtin = .{ .which = x.which, .variant = x.variant } }),
|
||||
.copy => |v| return bind(vals, inst.result, vals[v]),
|
||||
.arith => |x| {
|
||||
const bits = arith(x.op, x.mode, x.ty, vals[x.a].int.bits, vals[x.b].int.bits) catch |e| return .{ .trap = kindOf(e) };
|
||||
|
||||
@@ -258,6 +258,7 @@ const FnCtx = struct {
|
||||
fn resultType(m: air.Module, op: air.Op) ?Type {
|
||||
return switch (op) {
|
||||
.@"const" => |x| x.ty,
|
||||
.error_const => |x| .{ .builtin = x.which },
|
||||
.arith => |x| .{ .int = x.ty },
|
||||
.bit => |x| x.ty,
|
||||
.cmp => .bool,
|
||||
@@ -794,6 +795,7 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
const span = inst.span;
|
||||
switch (inst.op) {
|
||||
.@"const", .trap => {},
|
||||
.error_const => |x| if (x.variant >= x.which.variants().len) try fc.c.report("E1001", span, "invalid built-in error variant", &.{}),
|
||||
.copy => |v| if (!fc.typeOf(v).isValue() or m.isLinearType(fc.typeOf(v))) try fc.c.report("E1001", span, "only value types can be copied", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "found", .value = .{ .str = try fc.c.typeName(fc.typeOf(v)) } },
|
||||
|
||||
Reference in new issue
Block a user