diff --git a/docs/reference/air-features.md b/docs/reference/air-features.md index aff2aba..1f62a5a 100644 --- a/docs/reference/air-features.md +++ b/docs/reference/air-features.md @@ -66,11 +66,13 @@ Feature problems are reported as `E0006` (`module.unsupported_feature`) with a ` |---|---|---| | `unknown_feature` | name not in this registry | `feature` | | `unsupported_feature` | registered but reserved, or not implemented by this toolchain | `feature` | -| `missing_dependency` | a dependency of a declared feature is not declared | `feature`, `requires` | +| `missing_dependency` | a dependency of a declared feature is not declared | `feature`, `requires`, `canonical_header` | | `duplicate_feature` | the same name is declared twice | `feature` | -| `feature_not_declared` | the body uses a construct of an undeclared feature | `feature`, `construct` | +| `feature_not_declared` | the body uses a construct of an undeclared feature | `feature`, `construct`, `closure`, `canonical_header` | | `reserved_type` | the body uses a reserved type such as `f32` (spec 07 §8.4) | `type` | +`canonical_header` is the complete header line the module needs, sorted, with the full dependency closure. Replacing the module's `features` line with it repairs every `missing_dependency` and `feature_not_declared` reported so far. `closure` lists the named feature and everything it depends on. + ## Discovery `aji features` prints the toolchain's AIR format, language version and supported features as one JSON object, e.g. `{"air":0,"lang":"0.1","features":[]}`. Agents query it before generating AIR. diff --git a/src/air/features.zig b/src/air/features.zig index 12158ba..fb9afc1 100644 --- a/src/air/features.zig +++ b/src/air/features.zig @@ -110,6 +110,33 @@ pub fn validate( 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); } @@ -148,6 +175,15 @@ test "feature validation" { 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)); diff --git a/src/air/parse.zig b/src/air/parse.zig index da34d9d..fef4ab5 100644 --- a/src/air/parse.zig +++ b/src/air/parse.zig @@ -212,6 +212,8 @@ const Parser = struct { types: []air.TypeDecl = &.{}, /// Features declared in the module header, sorted. declared: []const []const u8 = &.{}, + /// Undeclared features the body has needed so far (for `canonical_header`). + needed: std.ArrayList([]const u8) = .empty, // Function-level state. fn_name: []const u8 = "", @@ -309,10 +311,13 @@ const Parser = struct { const problems = try features_mod.validate(p.arena, features, &features_mod.registry, &features_mod.supported); for (problems) |pr| { if (pr.requires) |req| { + // The repair is mechanical: the header with the full closure. + const canonical = try features_mod.canonicalHeader(p.arena, try features_mod.closure(p.arena, &features_mod.registry, features, null)); 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 } }, + .{ .name = "canonical_header", .value = .{ .str = canonical } }, }); } else { try p.report("E0006", features_tok.span, "feature cannot be used", &.{ @@ -642,10 +647,21 @@ const Parser = struct { /// Reports `E0006 feature_not_declared` if `feature` is not in the header (spec 07 §8.1). fn requireFeature(p: *Parser, feature: []const u8, span: Span, construct: []const u8) Error!void { for (p.declared) |d| if (eql(d, feature)) return; + const reg = &features_mod.registry; + const needed = try features_mod.closure(p.arena, reg, &.{}, feature); + var closure_text: std.Io.Writer.Allocating = .init(p.arena); + for (needed, 0..) |n, i| closure_text.writer.print("{s}{s}", .{ if (i == 0) "" else ", ", n }) catch return error.OutOfMemory; + if (!for (p.needed.items) |n| { + if (eql(n, feature)) break true; + } else false) try p.needed.append(p.arena, feature); + const so_far = try std.mem.concat(p.arena, []const u8, &.{ p.declared, p.needed.items }); + const canonical = try features_mod.canonicalHeader(p.arena, try features_mod.closure(p.arena, reg, so_far, null)); try p.report("E0006", span, "construct belongs to a feature the module does not declare", &.{ .{ .name = "reason", .value = .{ .str = "feature_not_declared" } }, .{ .name = "feature", .value = .{ .str = feature } }, .{ .name = "construct", .value = .{ .str = construct } }, + .{ .name = "closure", .value = .{ .str = closure_text.written() } }, + .{ .name = "canonical_header", .value = .{ .str = canonical } }, }); }