Phase 2: effects and capabilities (RFC 0006)

- air: cap<K> and built-in enums (cap_error) as types; enum.switch over
  built-in enums; cap.narrow terminator with ok/err targets; intrinsic
  syntax (registry still empty: E0004); registries for effects,
  capability kinds and built-in enums, synced with reference docs
- verifier: effect stage (E3101, flat, direct callees); cap.narrow form
  check (E3202) and ok/err parameter rules (E5002, E1001); capabilities
  are copyable, not zeroable, not comparable; sizes cap 8, built-in tag
- interp: host authority table; @main instantiation from grants by
  parameter name with not_instantiated reasons; capabilities render as
  opaque
- CLI: --grant P=KIND[:SCOPE], exit code 6; runner: grant: and
  not_instantiated: headers, pending only for unsupported features
- conformance: 18 Phase 2 tests; 4000 mutants without a crash
This commit is contained in:
dev committed 2026-10-04 13:42:35 +03:00
1 parent fb4338cea6
commit 9c29a80b72
41 files changed
+908 -58

No files matched your search

+2 -1
View File
@@ -28,8 +28,9 @@ pub fn accountedSize(m: air.Module, t: air.Type, depth: usize) ?u64 {
return switch (t) {
.int => |it| it.bits() / 8,
.bool => 1,
.ref, .mut => 8,
.ref, .mut, .cap => 8,
.slice, .slice_mut => 16,
.builtin => |b| tagSize(b.variants().len),
.array => |a| std.math.mul(u64, a.len, accountedSize(m, a.elem, depth + 1) orelse return null) catch null,
.named => |i| switch (m.types[i].kind) {
.@"struct" => |fs| blk: {
+20 -12
View File
@@ -52,39 +52,47 @@ fn checkTerminator(c: *Context, f: air.Function, inst: air.Inst) !void {
try arity(c, f, inst.span, x.default);
},
.enum_switch => |x| {
const variants = c.module.types[x.decl].kind.@"enum";
const m = c.module;
for (x.arms, 0..) |arm, i| {
for (x.arms[0..i]) |prev| if (prev.variant == arm.variant)
try invalid(c, f, arm.span, "duplicate enum.switch arm", null);
const blk = f.blocks[arm.block];
if (blk.params.len != variants[arm.variant].payload.len) {
try c.report("E5002", arm.span, "arm target parameters do not match the variant payload", &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "block", .value = .{ .str = blk.name } },
.{ .name = "expected", .value = .{ .int = @intCast(variants[arm.variant].payload.len) } },
.{ .name = "found", .value = .{ .int = @intCast(blk.params.len) } },
});
}
try paramCount(c, f, arm.span, arm.block, m.variantPayload(x.ty, arm.variant).len, "arm target parameters do not match the variant payload");
}
if (x.default) |d| {
try arity(c, f, inst.span, d);
} else {
for (variants, 0..) |v, vi| {
for (0..m.variantCount(x.ty)) |vi| {
const covered = for (x.arms) |arm| {
if (arm.variant == vi) break true;
} else false;
if (!covered) try c.report("E5002", inst.span, "enum.switch is not exhaustive and has no default", &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "missing_variant", .value = .{ .str = v.name } },
.{ .name = "missing_variant", .value = .{ .str = m.variantName(x.ty, vi) } },
});
}
}
},
// `ok` receives the narrowed capability, `err` the `cap_error` (spec 05 §10.4).
.cap_narrow => |x| {
try paramCount(c, f, inst.span, x.ok, 1, "cap.narrow `ok` target takes exactly one parameter");
try paramCount(c, f, inst.span, x.err, 1, "cap.narrow `err` target takes exactly one parameter");
},
.ret, .trap => {},
else => unreachable,
}
}
fn paramCount(c: *Context, f: air.Function, span: air.Span, block: u32, expected: usize, message: []const u8) !void {
const blk = f.blocks[block];
if (blk.params.len == expected) return;
try c.report("E5002", span, message, &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "block", .value = .{ .str = blk.name } },
.{ .name = "expected", .value = .{ .int = @intCast(expected) } },
.{ .name = "found", .value = .{ .int = @intCast(blk.params.len) } },
});
}
fn arity(c: *Context, f: air.Function, span: air.Span, t: air.Target) !void {
const blk = f.blocks[t.block];
if (blk.params.len == t.args.len) return;
+49
View File
@@ -0,0 +1,49 @@
//! Effect checking (spec 05 §9.3). Reports E3101.
//!
//! Applies to modules that declare the feature `effects`. Effects are flat
//! atoms: a call needs every effect of its callee in the caller's list.
//! Checking each function against its direct callees makes the rule hold
//! transitively, including through recursion.
const std = @import("std");
const air = @import("../air.zig");
const verifier = @import("../verifier.zig");
const Context = verifier.Context;
pub fn check(c: *Context) !void {
const m = c.module;
if (!m.declares("effects")) return;
for (m.functions) |f| {
for (f.blocks) |b| for (b.insts) |inst| switch (inst.op) {
.call => |x| {
const callee = m.functions[x.callee];
for (callee.effects) |e| if (!contains(f.effects, e)) {
try c.report("E3101", inst.span, "callee effect not declared by the caller", &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "callee", .value = .{ .str = callee.name } },
.{ .name = "effect", .value = .{ .str = e } },
});
};
},
else => {},
};
}
}
fn contains(list: []const []const u8, name: []const u8) bool {
for (list) |x| if (std.mem.eql(u8, x, name)) return true;
return false;
}
const h = "module t\nair 0\nlang 0.1\nfeatures [effects]\n";
test "callee effects must be declared" {
const g = "fn @g() -> void\neffects [io.write]\nentry:\n ret\n";
try verifier.expectCodes(h ++ "fn @f() -> void\neffects [io.write]\nentry:\n call @g()\n ret\n" ++ g, &.{});
try verifier.expectCodes(h ++ "fn @f() -> void\neffects []\nentry:\n call @g()\n ret\n" ++ g, &.{"E3101"});
// Flat: atomic does not cover atomic.weak.
try verifier.expectCodes(h ++ "fn @f() -> void\neffects [atomic]\nentry:\n call @w()\n ret\nfn @w() -> void\neffects [atomic.weak]\nentry:\n ret\n", &.{"E3101"});
// Without the feature, lists are recorded but not checked.
try verifier.expectCodes("module t\nair 0\nlang 0.1\nfeatures []\nfn @f() -> void\neffects []\nentry:\n call @g()\n ret\n" ++ g, &.{});
}
+17 -3
View File
@@ -418,16 +418,30 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
try fc.expectTarget(span, x.default);
},
.enum_switch => |x| {
try fc.expectValue(span, x.value, .{ .named = x.decl });
const variants = m.types[x.decl].kind.@"enum";
try fc.expectValue(span, x.value, x.ty);
for (x.arms) |arm| {
const params = f.blocks[arm.block].params;
for (params, variants[arm.variant].payload) |p, t| {
for (params, m.variantPayload(x.ty, arm.variant)) |p, t| {
if (!p.ty.eql(t)) try fc.mismatch(arm.span, try fc.c.valueName(f, p.value), t, p.ty);
}
}
if (x.default) |d| try fc.expectTarget(span, d);
},
.cap_narrow => |x| {
const t = fc.typeOf(x.cap);
if (t != .cap) return fc.kindError(span, x.cap, "cap<K>");
// No form of any kind widens; an unregistered form is reported as
// an attempt to widen (spec 05 §10.4).
if (!air.authority.hasForm(t.cap, x.form)) try fc.c.report("E3202", span, "narrowing form not defined for this capability kind", &.{
.{ .name = "function", .value = .{ .str = f.name } },
.{ .name = "kind", .value = .{ .str = air.authority.kinds[t.cap].name } },
.{ .name = "form", .value = .{ .str = x.form } },
});
const ok = f.blocks[x.ok].params[0];
if (!ok.ty.eql(t)) try fc.mismatch(span, try fc.c.valueName(f, ok.value), t, ok.ty);
const err = f.blocks[x.err].params[0];
if (!err.ty.eql(.{ .builtin = .cap_error })) try fc.mismatch(span, try fc.c.valueName(f, err.value), .{ .builtin = .cap_error }, err.ty);
},
.ret => |v| switch (f.ret) {
.never => try fc.c.report("E1001", span, "`ret` in a function returning never", &.{
.{ .name = "function", .value = .{ .str = f.name } },