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.
232 lines
12 KiB
Zig
232 lines
12 KiB
Zig
//! AIR feature negotiation (spec 07 §8.1, §8.7; reference/air-features.md).
|
|
//!
|
|
//! `registry` MUST mirror docs/reference/air-features.md (enforced by a test).
|
|
//! `supported` lists the features this toolchain implements.
|
|
|
|
const std = @import("std");
|
|
|
|
pub const Status = enum {
|
|
/// Name and dependencies are fixed; semantics are specified in the phase
|
|
/// that publishes the feature. Modules may not use it yet.
|
|
reserved,
|
|
/// Semantics are specified and immutable.
|
|
published,
|
|
};
|
|
|
|
pub const Feature = struct {
|
|
name: []const u8,
|
|
depends: []const []const u8,
|
|
status: Status,
|
|
/// Plan phase that publishes the feature; null if unscheduled.
|
|
phase: ?u8,
|
|
};
|
|
|
|
pub const registry = [_]Feature{
|
|
.{ .name = "arc", .depends = &.{ "capabilities", "effects", "heap", "islands", "ownership", "resources" }, .status = .published, .phase = 4 },
|
|
.{ .name = "arrays", .depends = &.{"ownership"}, .status = .published, .phase = 1 },
|
|
.{ .name = "atomics", .depends = &.{ "effects", "ownership" }, .status = .published, .phase = 5 },
|
|
.{ .name = "borrow_joins", .depends = &.{"ownership"}, .status = .reserved, .phase = null },
|
|
.{ .name = "cancellation", .depends = &.{ "effects", "ownership", "tasks" }, .status = .published, .phase = 3 },
|
|
.{ .name = "capabilities", .depends = &.{ "effects", "ownership" }, .status = .published, .phase = 2 },
|
|
.{ .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 },
|
|
.{ .name = "linear_aggregates", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .published, .phase = 6 },
|
|
.{ .name = "linear_buffers", .depends = &.{ "arrays", "capabilities", "effects", "heap", "linear_aggregates", "ownership", "resources" }, .status = .published, .phase = 6 },
|
|
.{ .name = "ownership", .depends = &.{}, .status = .published, .phase = 1 },
|
|
.{ .name = "receiver_dup", .depends = &.{ "cancellation", "capabilities", "channels", "effects", "heap", "ownership", "resources", "tasks" }, .status = .reserved, .phase = null },
|
|
.{ .name = "regions", .depends = &.{ "capabilities", "effects", "heap", "ownership", "resources" }, .status = .published, .phase = 2 },
|
|
.{ .name = "resource_joins", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .reserved, .phase = null },
|
|
.{ .name = "resources", .depends = &.{ "capabilities", "effects", "ownership" }, .status = .published, .phase = 2 },
|
|
.{ .name = "rwlock", .depends = &.{ "cancellation", "effects", "ownership", "sync", "tasks" }, .status = .reserved, .phase = null },
|
|
.{ .name = "sync", .depends = &.{ "cancellation", "effects", "ownership", "tasks" }, .status = .published, .phase = 3 },
|
|
.{ .name = "task_borrow_returns", .depends = &.{ "ownership", "tasks" }, .status = .reserved, .phase = null },
|
|
.{ .name = "task_transfer", .depends = &.{ "ownership", "tasks" }, .status = .reserved, .phase = null },
|
|
.{ .name = "tasks", .depends = &.{"ownership"}, .status = .published, .phase = 3 },
|
|
.{ .name = "unsafe", .depends = &.{ "arrays", "effects", "ownership" }, .status = .published, .phase = 2 },
|
|
};
|
|
|
|
/// Features implemented by this toolchain, sorted.
|
|
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,
|
|
unsupported_feature,
|
|
feature_not_declared,
|
|
missing_dependency,
|
|
duplicate_feature,
|
|
reserved_type,
|
|
};
|
|
|
|
pub const Problem = struct {
|
|
reason: Reason,
|
|
feature: []const u8,
|
|
/// For `missing_dependency`: the dependency that is not declared.
|
|
requires: ?[]const u8 = null,
|
|
/// Index into the declared list, for locating the diagnostic.
|
|
index: usize,
|
|
};
|
|
|
|
pub fn find(reg: []const Feature, name: []const u8) ?Feature {
|
|
for (reg) |f| if (std.mem.eql(u8, f.name, name)) return f;
|
|
return null;
|
|
}
|
|
|
|
fn contains(list: []const []const u8, name: []const u8) bool {
|
|
for (list) |x| if (std.mem.eql(u8, x, name)) return true;
|
|
return false;
|
|
}
|
|
|
|
/// Validates a declared feature set before the module body is examined.
|
|
/// Problems are reported in declaration order; for one feature at most one
|
|
/// problem is reported, checked in the order duplicate, unknown,
|
|
/// unsupported, missing dependency.
|
|
pub fn validate(
|
|
arena: std.mem.Allocator,
|
|
declared: []const []const u8,
|
|
reg: []const Feature,
|
|
sup: []const []const u8,
|
|
) std.mem.Allocator.Error![]const Problem {
|
|
var problems: std.ArrayList(Problem) = .empty;
|
|
for (declared, 0..) |name, i| {
|
|
if (contains(declared[0..i], name)) {
|
|
try problems.append(arena, .{ .reason = .duplicate_feature, .feature = name, .index = i });
|
|
continue;
|
|
}
|
|
const f = find(reg, name) orelse {
|
|
try problems.append(arena, .{ .reason = .unknown_feature, .feature = name, .index = i });
|
|
continue;
|
|
};
|
|
if (f.status != .published or !contains(sup, name)) {
|
|
try problems.append(arena, .{ .reason = .unsupported_feature, .feature = name, .index = i });
|
|
continue;
|
|
}
|
|
for (f.depends) |dep| if (!contains(declared, dep)) {
|
|
try problems.append(arena, .{ .reason = .missing_dependency, .feature = name, .requires = dep, .index = i });
|
|
break;
|
|
};
|
|
}
|
|
return problems.items;
|
|
}
|
|
|
|
/// The sorted dependency closure of `base` plus `extra`: every registered
|
|
/// feature named, and everything they depend on. Unknown names are skipped.
|
|
pub fn closure(arena: std.mem.Allocator, reg: []const Feature, base: []const []const u8, extra: ?[]const u8) std.mem.Allocator.Error![][]const u8 {
|
|
var out: std.ArrayList([]const u8) = .empty;
|
|
const add = struct {
|
|
fn f(a: std.mem.Allocator, o: *std.ArrayList([]const u8), r: []const Feature, name: []const u8) std.mem.Allocator.Error!void {
|
|
const feat = find(r, name) orelse return;
|
|
if (!contains(o.items, feat.name)) try o.append(a, feat.name);
|
|
for (feat.depends) |d| if (!contains(o.items, d)) try o.append(a, d);
|
|
}
|
|
}.f;
|
|
for (base) |n| try add(arena, &out, reg, n);
|
|
if (extra) |e| try add(arena, &out, reg, e);
|
|
sort(out.items);
|
|
return out.items;
|
|
}
|
|
|
|
/// `features [a, b, …]` in canonical form (spec 07 §8.5): the header line a
|
|
/// module needs for the features in `list`.
|
|
pub fn canonicalHeader(arena: std.mem.Allocator, list: []const []const u8) std.mem.Allocator.Error![]const u8 {
|
|
var w: std.Io.Writer.Allocating = .init(arena);
|
|
w.writer.writeAll("features [") catch return error.OutOfMemory;
|
|
for (list, 0..) |n, i| w.writer.print("{s}{s}", .{ if (i == 0) "" else ", ", n }) catch return error.OutOfMemory;
|
|
w.writer.writeAll("]") catch return error.OutOfMemory;
|
|
return w.written();
|
|
}
|
|
|
|
fn lessThan(_: void, a: []const u8, b: []const u8) bool {
|
|
return std.mem.lessThan(u8, a, b);
|
|
}
|
|
|
|
/// Canonical order of a feature list (spec 07 §8.5).
|
|
pub fn sort(list: [][]const u8) void {
|
|
std.mem.sort([]const u8, list, {}, lessThan);
|
|
}
|
|
|
|
// ---------------------------------------------------------------- tests
|
|
|
|
const test_registry = [_]Feature{
|
|
.{ .name = "ownership", .depends = &.{}, .status = .published, .phase = 1 },
|
|
.{ .name = "regions", .depends = &.{"ownership"}, .status = .published, .phase = 1 },
|
|
.{ .name = "islands", .depends = &.{ "ownership", "regions" }, .status = .published, .phase = 4 },
|
|
.{ .name = "tasks", .depends = &.{"ownership"}, .status = .reserved, .phase = 3 },
|
|
};
|
|
|
|
fn expectProblems(declared: []const []const u8, sup: []const []const u8, want: []const Reason) !void {
|
|
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
|
defer arena_state.deinit();
|
|
const ps = try validate(arena_state.allocator(), declared, &test_registry, sup);
|
|
try std.testing.expectEqual(want.len, ps.len);
|
|
for (want, ps) |w, p| try std.testing.expectEqual(w, p.reason);
|
|
}
|
|
|
|
test "feature validation" {
|
|
const all = [_][]const u8{ "islands", "ownership", "regions", "tasks" };
|
|
try expectProblems(&.{}, &all, &.{});
|
|
try expectProblems(&.{ "islands", "ownership", "regions" }, &all, &.{});
|
|
try expectProblems(&.{ "islands", "ownership" }, &all, &.{.missing_dependency});
|
|
try expectProblems(&.{"regions"}, &all, &.{.missing_dependency});
|
|
try expectProblems(&.{"magic"}, &all, &.{.unknown_feature});
|
|
try expectProblems(&.{ "ownership", "ownership" }, &all, &.{.duplicate_feature});
|
|
try expectProblems(&.{ "ownership", "tasks" }, &all, &.{.unsupported_feature}); // reserved
|
|
try expectProblems(&.{"ownership"}, &.{}, &.{.unsupported_feature}); // not implemented here
|
|
}
|
|
|
|
test "closure and canonical header" {
|
|
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
|
defer arena_state.deinit();
|
|
const a = arena_state.allocator();
|
|
const c = try closure(a, &test_registry, &.{"ownership"}, "islands");
|
|
try std.testing.expectEqualStrings("features [islands, ownership, regions]", try canonicalHeader(a, c));
|
|
try std.testing.expectEqualStrings("features []", try canonicalHeader(a, try closure(a, &test_registry, &.{"magic"}, null)));
|
|
}
|
|
|
|
test "registry is sorted, closed under dependencies and acyclic" {
|
|
for (registry, 0..) |f, i| {
|
|
if (i > 0) try std.testing.expect(std.mem.lessThan(u8, registry[i - 1].name, f.name));
|
|
for (f.depends) |d| {
|
|
const dep = find(®istry, d) orelse return error.UnknownDependency;
|
|
// Dependencies are themselves closed: a feature lists its full closure.
|
|
for (dep.depends) |dd| try std.testing.expect(contains(f.depends, dd));
|
|
if (f.phase) |fp| try std.testing.expect(dep.phase != null and dep.phase.? <= fp);
|
|
try std.testing.expect(!contains(dep.depends, f.name));
|
|
}
|
|
}
|
|
}
|
|
|
|
test "registry mirrors docs/reference/air-features.md" {
|
|
const md = @embedFile("air_features_md");
|
|
var seen: usize = 0;
|
|
var lines = std.mem.splitScalar(u8, md, '\n');
|
|
var in_registry = false;
|
|
while (lines.next()) |line| {
|
|
if (std.mem.startsWith(u8, line, "## ")) in_registry = std.mem.eql(u8, line, "## Registry");
|
|
// Table rows look like: | `name` | reserved | 1 | `dep`, `dep` | description |
|
|
if (!in_registry or !std.mem.startsWith(u8, line, "| `")) continue;
|
|
var cols = std.mem.tokenizeScalar(u8, line, '|');
|
|
const name = std.mem.trim(u8, cols.next().?, " `");
|
|
const status = std.mem.trim(u8, cols.next().?, " ");
|
|
const phase_text = std.mem.trim(u8, cols.next().?, " ");
|
|
const phase: ?u8 = if (std.mem.eql(u8, phase_text, "—")) null else try std.fmt.parseInt(u8, phase_text, 10);
|
|
const deps_text = std.mem.trim(u8, cols.next().?, " ");
|
|
const f = find(®istry, name) orelse {
|
|
std.debug.print("feature {s} is in air-features.md but not in the registry\n", .{name});
|
|
return error.RegistryOutOfSync;
|
|
};
|
|
try std.testing.expectEqualStrings(@tagName(f.status), status);
|
|
try std.testing.expectEqual(f.phase, phase);
|
|
var n: usize = 0;
|
|
var deps = std.mem.tokenizeAny(u8, deps_text, "`, —");
|
|
while (deps.next()) |d| : (n += 1) try std.testing.expect(contains(f.depends, d));
|
|
try std.testing.expectEqual(f.depends.len, n);
|
|
seen += 1;
|
|
}
|
|
try std.testing.expectEqual(registry.len, seen);
|
|
}
|