E0006: closure and canonical_header fields
feature_not_declared gains closure (the feature and its dependencies) and canonical_header (the complete sorted header with everything the body needed so far); missing_dependency gains canonical_header. The code and reason are unchanged (spec 07 §8.7): a generic repair aid, the only diagnostic strong agents hit in the native smoke.
This commit is contained in:
1 parent
e83b71d82b
commit
5b7ecf9ffd
3 files changed
+56
-2
No files matched your search
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user