- air: own<T>, own_array<T>, region, alloc_error; own.new/array/take/ free, region.create/new/array/reset/destroy; mem.alloc budget form; linear contents of owners gated by linear_aggregates - verifier: allocation types and capability kind (E1001, E3201), mem.alloc effect (E3101), ok/err counts; borrow-typed block params checked in the CFG stage with the region-allocation exception; linear owners and regions (E3002, E3001, E2001, E2002, E2101); region allocations as disjoint exclusive children of the shared region borrow; E2104 when reset/destroy would invalidate a live reference - interp: live-byte budgets charged along the narrowing chain, shared by copies, mandatory in the deterministic profile; heap cells; regions return quota only on reset/destroy - conformance: 25 heap/region tests including a 64 KiB region file copy; 4000 mutants without a crash
185 lines
8.5 KiB
Zig
185 lines
8.5 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 = "arrays", .depends = &.{"ownership"}, .status = .published, .phase = 1 },
|
|
.{ .name = "atomics", .depends = &.{"ownership"}, .status = .reserved, .phase = 5 },
|
|
.{ .name = "borrow_joins", .depends = &.{"ownership"}, .status = .reserved, .phase = null },
|
|
.{ .name = "capabilities", .depends = &.{ "effects", "ownership" }, .status = .published, .phase = 2 },
|
|
.{ .name = "channels", .depends = &.{"ownership"}, .status = .reserved, .phase = 3 },
|
|
.{ .name = "effects", .depends = &.{}, .status = .published, .phase = 2 },
|
|
.{ .name = "heap", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .published, .phase = 2 },
|
|
.{ .name = "islands", .depends = &.{ "capabilities", "effects", "heap", "ownership", "regions", "resources" }, .status = .reserved, .phase = 4 },
|
|
.{ .name = "linear_aggregates", .depends = &.{ "capabilities", "effects", "ownership", "resources" }, .status = .reserved, .phase = null },
|
|
.{ .name = "ownership", .depends = &.{}, .status = .published, .phase = 1 },
|
|
.{ .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 = "sync", .depends = &.{"ownership"}, .status = .reserved, .phase = 3 },
|
|
.{ .name = "tasks", .depends = &.{"ownership"}, .status = .reserved, .phase = 3 },
|
|
};
|
|
|
|
/// Features implemented by this toolchain, sorted.
|
|
pub const supported = [_][]const u8{ "arrays", "capabilities", "effects", "heap", "ownership", "regions", "resources" };
|
|
|
|
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;
|
|
}
|
|
|
|
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 "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);
|
|
}
|