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

+47 -7
View File
@@ -20,6 +20,8 @@ const Expect = union(enum) {
result: []const u8,
/// Expected trap code.
trap: []const u8,
/// Expected `reason` of a `not_instantiated` record.
not_instantiated: []const u8,
};
};
@@ -32,6 +34,7 @@ const default_fuel: u64 = 1_000_000;
const Header = struct {
expect: Expect,
fuel: u64,
grants: []const aji.interp.Grant,
phase: u8,
spec: []const u8,
};
@@ -121,8 +124,10 @@ fn runOne(arena: std.mem.Allocator, path: []const u8, source: []const u8) !Resul
const header = parseHeader(arena, source) catch |err|
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "bad header: {t}", .{err}) };
if (header.phase > aji.implemented_phase)
return .{ .outcome = .pending, .detail = try std.fmt.allocPrint(arena, "phase {d} not implemented", .{header.phase}) };
// A test is pending only while its phase is unfinished and it needs a
// feature this toolchain does not support yet.
if (header.phase > aji.implemented_phase) if (unsupportedFeature(source)) |name|
return .{ .outcome = .pending, .detail = try std.fmt.allocPrint(arena, "phase {d}: feature {s} not implemented", .{ header.phase, name }) };
// Blank header lines (keeping newlines) so spans in diagnostics stay accurate
// and the AIR grammar does not have to know about test metadata.
@@ -147,7 +152,7 @@ fn runOne(arena: std.mem.Allocator, path: []const u8, source: []const u8) !Resul
switch (header.expect) {
.accept => if (report.status == .accepted) return .{ .outcome = .pass },
.reject => |want| if (report.status == .rejected and eqlCodes(want, got.items)) return .{ .outcome = .pass },
.run => |want| if (report.status == .accepted) return runModule(arena, report.module.?, want, header.fuel),
.run => |want| if (report.status == .accepted) return runModule(arena, report.module.?, want, header),
}
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "expected {f}, got {t} {s}", .{
fmtExpect(header.expect), report.status, try std.mem.join(arena, " ", got.items),
@@ -156,12 +161,13 @@ fn runOne(arena: std.mem.Allocator, path: []const u8, source: []const u8) !Resul
/// Runs the module twice and requires byte-identical run records (spec 11)
/// before comparing against the expectation.
fn runModule(arena: std.mem.Allocator, module: aji.air.Module, want: Expect.Run, fuel: u64) !Result {
fn runModule(arena: std.mem.Allocator, module: aji.air.Module, want: Expect.Run, header: Header) !Result {
var records: [2][]const u8 = undefined;
var outcome: aji.interp.Outcome = undefined;
for (&records) |*rec| {
outcome = aji.interp.run(arena, module, "main", .{ .fuel = fuel }) catch |err| switch (err) {
error.EntryNotFound => return .{ .outcome = .fail, .detail = "module has no parameterless @main" },
outcome = aji.interp.run(arena, module, "main", .{ .fuel = header.fuel, .grants = header.grants }) catch |err| switch (err) {
error.EntryNotFound => return .{ .outcome = .fail, .detail = "module has no @main whose parameters are all capabilities" },
error.DuplicateGrant => return .{ .outcome = .fail, .detail = "a parameter is granted more than once" },
else => |e| return e,
};
var w: std.Io.Writer.Allocating = .init(arena);
@@ -174,6 +180,7 @@ fn runModule(arena: std.mem.Allocator, module: aji.air.Module, want: Expect.Run,
const ok = switch (want) {
.trap => |code| outcome == .trapped and std.mem.eql(u8, outcome.trapped.code.id, code),
.result => |json| outcome == .returned and std.mem.eql(u8, json, try valueJson(arena, module, outcome.returned)),
.not_instantiated => |reason| outcome == .not_instantiated and std.mem.eql(u8, outcome.not_instantiated.reason, reason),
};
if (ok) return .{ .outcome = .pass };
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "got {s}", .{records[0]}) };
@@ -186,6 +193,24 @@ fn valueJson(arena: std.mem.Allocator, module: aji.air.Module, v: aji.interp.Val
return w.written();
}
/// First feature of the module header that this toolchain does not support.
fn unsupportedFeature(source: []const u8) ?[]const u8 {
var lines = std.mem.splitScalar(u8, source, '\n');
while (lines.next()) |line| {
if (!std.mem.startsWith(u8, line, "features [")) continue;
const close = std.mem.indexOfScalar(u8, line, ']') orelse return null;
var names = std.mem.tokenizeAny(u8, line["features [".len..close], ", ");
while (names.next()) |name| {
const known = for (aji.air.features.supported) |s| {
if (std.mem.eql(u8, s, name)) break true;
} else false;
if (!known) return name;
}
return null;
}
return null;
}
fn isKey(s: []const u8) bool {
if (s.len == 0) return false;
for (s) |ch| if (!std.ascii.isLower(ch) and ch != '_') return false;
@@ -212,17 +237,19 @@ fn formatExpect(e: Expect, w: *std.Io.Writer) std.Io.Writer.Error!void {
.run => |r| switch (r) {
.result => |j| try w.print("run returning {s}", .{j}),
.trap => |c| try w.print("run trapping {s}", .{c}),
.not_instantiated => |reason| try w.print("not_instantiated ({s})", .{reason}),
},
}
}
const HeaderError = error{ MissingExpect, MissingPhase, MissingSpec, MissingRunResult, UnknownKey, UnknownCode, BadExpect, OutOfMemory, InvalidCharacter, Overflow };
const HeaderError = error{ BadGrant, MissingExpect, MissingPhase, MissingSpec, MissingRunResult, UnknownKey, UnknownCode, BadExpect, OutOfMemory, InvalidCharacter, Overflow };
fn parseHeader(arena: std.mem.Allocator, source: []const u8) HeaderError!Header {
var expect: ?Expect = null;
var run_result: ?Expect.Run = null;
var phase: ?u8 = null;
var fuel: u64 = default_fuel;
var grants: std.ArrayList(aji.interp.Grant) = .empty;
var spec: ?[]const u8 = null;
var lines = std.mem.splitScalar(u8, source, '\n');
@@ -260,6 +287,18 @@ fn parseHeader(arena: std.mem.Allocator, source: []const u8) HeaderError!Header
const code = aji.diag.lookup(value) orelse return error.UnknownCode;
if (code.id[0] != 'T') return error.UnknownCode;
run_result = .{ .trap = value };
} else if (std.mem.eql(u8, key, "not_instantiated")) {
if (run_result != null) return error.BadExpect;
run_result = .{ .not_instantiated = value };
} else if (std.mem.eql(u8, key, "grant")) {
const eq = std.mem.indexOfScalar(u8, value, '=') orelse return error.BadGrant;
const rest = value[eq + 1 ..];
const sep = std.mem.indexOfScalar(u8, rest, ':');
try grants.append(arena, .{
.parameter = value[0..eq],
.kind = if (sep) |c| rest[0..c] else rest,
.scope = if (sep) |c| rest[c + 1 ..] else null,
});
} else if (std.mem.eql(u8, key, "fuel")) {
fuel = try std.fmt.parseInt(u64, value, 10);
} else if (std.mem.eql(u8, key, "phase")) {
@@ -275,6 +314,7 @@ fn parseHeader(arena: std.mem.Allocator, source: []const u8) HeaderError!Header
return .{
.expect = expect orelse return error.MissingExpect,
.fuel = fuel,
.grants = grants.items,
.phase = phase orelse return error.MissingPhase,
.spec = spec orelse return error.MissingSpec,
};