air: feature negotiation for AIR format 0 (DD-014)
- reference/air-features.md: normative feature registry (arrays, atomics, capabilities, channels, effects, islands, ownership, regions, sync, tasks; all reserved), dependency closures, reasons for E0006 - spec 07 §8.1/§8.5/§8.7: features are mandatory, validated before the body, full dependency closure declared, sorted canonically; every extension belongs to a feature; published feature semantics immutable - spec 05 §8: features vs effects vs capabilities - src/air/features.zig: registry, validation, sync test with the docs - parser validates features first; E0006 carries `reason` - `aji features` prints supported AIR format, language and features - conformance: unknown, reserved and duplicate feature tests - plan: feature publication tasks in phases 1-5
This commit is contained in:
1 parent
03dda1245a
commit
19dff5dd52
24 files changed
+366
-17
No files matched your search
@@ -30,9 +30,11 @@ pub const successors = module.successors;
|
||||
|
||||
pub const parse = @import("air/parse.zig");
|
||||
pub const print = @import("air/print.zig");
|
||||
pub const features = @import("air/features.zig");
|
||||
|
||||
test {
|
||||
_ = module;
|
||||
_ = parse;
|
||||
_ = print;
|
||||
_ = features;
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
//! 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.
|
||||
phase: u8,
|
||||
};
|
||||
|
||||
pub const registry = [_]Feature{
|
||||
.{ .name = "arrays", .depends = &.{}, .status = .reserved, .phase = 1 },
|
||||
.{ .name = "atomics", .depends = &.{"ownership"}, .status = .reserved, .phase = 5 },
|
||||
.{ .name = "capabilities", .depends = &.{ "effects", "ownership" }, .status = .reserved, .phase = 2 },
|
||||
.{ .name = "channels", .depends = &.{"ownership"}, .status = .reserved, .phase = 3 },
|
||||
.{ .name = "effects", .depends = &.{}, .status = .reserved, .phase = 2 },
|
||||
.{ .name = "islands", .depends = &.{ "ownership", "regions" }, .status = .reserved, .phase = 4 },
|
||||
.{ .name = "ownership", .depends = &.{}, .status = .reserved, .phase = 1 },
|
||||
.{ .name = "regions", .depends = &.{"ownership"}, .status = .reserved, .phase = 1 },
|
||||
.{ .name = "sync", .depends = &.{"ownership"}, .status = .reserved, .phase = 3 },
|
||||
.{ .name = "tasks", .depends = &.{"ownership"}, .status = .reserved, .phase = 3 },
|
||||
};
|
||||
|
||||
/// Features implemented by this toolchain, sorted. Empty: Phase 0 baseline.
|
||||
pub const supported = [_][]const u8{};
|
||||
|
||||
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));
|
||||
try std.testing.expect(dep.phase <= f.phase);
|
||||
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 = try std.fmt.parseInt(u8, std.mem.trim(u8, cols.next().?, " "), 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);
|
||||
}
|
||||
+28
-6
@@ -9,6 +9,7 @@
|
||||
const std = @import("std");
|
||||
const diag = @import("../diag.zig");
|
||||
const air = @import("module.zig");
|
||||
const features_mod = @import("features.zig");
|
||||
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Span = air.Span;
|
||||
@@ -272,13 +273,30 @@ const Parser = struct {
|
||||
});
|
||||
_ = try p.expect(.nl, "end of line");
|
||||
|
||||
_ = try p.expectWord("features");
|
||||
const features = try p.parsePathList();
|
||||
for (features) |f| try p.report("E0006", lang_tok.span, "unsupported feature", &.{
|
||||
.{ .name = "feature", .value = .{ .str = f } },
|
||||
});
|
||||
const features_tok = try p.expectWord("features");
|
||||
const features = try p.arena.dupe([]const u8, try p.parsePathList());
|
||||
_ = try p.expect(.nl, "end of line");
|
||||
|
||||
// Features are validated before the body (spec 07 §8.1). If any is
|
||||
// unusable the body is not examined: its constructs may belong to it.
|
||||
const problems = try features_mod.validate(p.arena, features, &features_mod.registry, &features_mod.supported);
|
||||
for (problems) |pr| {
|
||||
if (pr.requires) |req| {
|
||||
try p.report("E0006", features_tok.span, "feature dependency not declared", &.{
|
||||
.{ .name = "reason", .value = .{ .str = @tagName(pr.reason) } },
|
||||
.{ .name = "feature", .value = .{ .str = pr.feature } },
|
||||
.{ .name = "requires", .value = .{ .str = req } },
|
||||
});
|
||||
} else {
|
||||
try p.report("E0006", features_tok.span, "feature cannot be used", &.{
|
||||
.{ .name = "reason", .value = .{ .str = @tagName(pr.reason) } },
|
||||
.{ .name = "feature", .value = .{ .str = pr.feature } },
|
||||
});
|
||||
}
|
||||
}
|
||||
if (problems.len != 0) return error.SyntaxError;
|
||||
features_mod.sort(features);
|
||||
|
||||
try p.prescanDeclarations();
|
||||
|
||||
var types: std.ArrayList(air.TypeDecl) = .empty;
|
||||
@@ -444,7 +462,10 @@ const Parser = struct {
|
||||
if (std.mem.eql(u8, t.text, "void")) return .void;
|
||||
if (std.mem.eql(u8, t.text, "never")) return .never;
|
||||
if (std.mem.eql(u8, t.text, "f32") or std.mem.eql(u8, t.text, "f64")) {
|
||||
try p.report("E0006", t.span, "floating-point types are reserved (spec 07 §8.4)", &.{.{ .name = "type", .value = .{ .str = t.text } }});
|
||||
try p.report("E0006", t.span, "floating-point types are reserved (spec 07 §8.4)", &.{
|
||||
.{ .name = "reason", .value = .{ .str = "reserved_type" } },
|
||||
.{ .name = "type", .value = .{ .str = t.text } },
|
||||
});
|
||||
return .void;
|
||||
}
|
||||
try p.report("E0004", t.span, "unknown type", &.{.{ .name = "name", .value = .{ .str = t.text } }});
|
||||
@@ -967,6 +988,7 @@ test "resolution diagnostics" {
|
||||
try expectCodes(header ++ "fn @f() -> f32\neffects []\nentry:\n trap\n", &.{"E0006"});
|
||||
try expectCodes(header ++ "fn @f() -> u8\neffects []\nentry:\n trap\nfn @f() -> u8\neffects []\nentry:\n trap\n", &.{"E0003"});
|
||||
try expectCodes("module t\nair 1\nlang 0.1\nfeatures []\n", &.{"E0005"});
|
||||
try expectCodes("module t\nair 0\nlang 0.1\nfeatures [magic, ownership, ownership]\nfn @f( -> broken\n", &.{ "E0006", "E0006", "E0006" });
|
||||
try expectCodes(header ++ "fn @f( -> u8\n", &.{"E0001"});
|
||||
try expectCodes(header ++ "type\n", &.{"E0001"}); // found by fuzzing
|
||||
}
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@ pub const registry = [_]Code{
|
||||
.{ .id = "E0003", .kind = "parse.duplicate_name", .meaning = "Function, type, block, field or variant name defined twice" },
|
||||
.{ .id = "E0004", .kind = "parse.unknown_name", .meaning = "Unknown opcode, type, function, field or variant" },
|
||||
.{ .id = "E0005", .kind = "module.unsupported_version", .meaning = "AIR format or language version not implemented" },
|
||||
.{ .id = "E0006", .kind = "module.unsupported_feature", .meaning = "Required feature or reserved type not implemented" },
|
||||
.{ .id = "E0006", .kind = "module.unsupported_feature", .meaning = "Feature unknown, unsupported, undeclared or dependency-incomplete, or reserved type used" },
|
||||
// E1xxx — Types and values
|
||||
.{ .id = "E1001", .kind = "type.mismatch", .meaning = "Operand/result type mismatch" },
|
||||
.{ .id = "E1002", .kind = "type.invalid_cast", .meaning = "Cast not permitted by safe semantics" },
|
||||
|
||||
@@ -20,6 +20,7 @@ const usage =
|
||||
\\ verify, then run @main and print the run record;
|
||||
\\ --fuel stops after N executed instructions
|
||||
\\ codes list diagnostic codes as JSON Lines
|
||||
\\ features print supported AIR format, language version and features
|
||||
\\ explain <code> show one diagnostic code
|
||||
\\ version print toolchain version
|
||||
\\
|
||||
@@ -54,6 +55,16 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
|
||||
try out.print("aji {s} (air format {d})\n", .{ aji.version, aji.air.format_version });
|
||||
return .accepted;
|
||||
}
|
||||
if (std.mem.eql(u8, cmd, "features")) {
|
||||
var s: std.json.Stringify = .{ .writer = out };
|
||||
try s.write(.{
|
||||
.air = aji.air.format_version,
|
||||
.lang = std.fmt.comptimePrint("{d}.{d}", .{ aji.air.lang_version[0], aji.air.lang_version[1] }),
|
||||
.features = &aji.air.features.supported,
|
||||
});
|
||||
try out.writeByte('\n');
|
||||
return .accepted;
|
||||
}
|
||||
if (std.mem.eql(u8, cmd, "codes")) {
|
||||
for (aji.diag.registry) |c| try writeCode(out, c);
|
||||
return .accepted;
|
||||
|
||||
Reference in new issue
Block a user