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));
|
||||
|
||||
@@ -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 } },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user