cli: write composed specialization identities iteratively
This commit is contained in:
1 parent
f5e2af15e8
commit
d98ddbc073
7 files changed
+284
-37
No files matched your search
+235
-37
@@ -451,44 +451,60 @@ const Generator = struct {
|
||||
return null;
|
||||
}
|
||||
|
||||
fn identity(g: *Generator, t: ast.Type, out: *std.ArrayList(u8)) Error!void {
|
||||
switch (t.kind) {
|
||||
.name => |name| {
|
||||
if (g.typeDecl(name, t.span)) |d| {
|
||||
if (std.mem.indexOfScalar(u8, d.name, '.') == null) try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, "{s}.", .{g.m.name}));
|
||||
try out.appendSlice(g.arena, d.name);
|
||||
} else if (g.instanceTypeKey(name)) |key| {
|
||||
try out.appendSlice(g.arena, key[5..]);
|
||||
} else try out.appendSlice(g.arena, name);
|
||||
},
|
||||
.array => |a| {
|
||||
try out.appendSlice(g.arena, "[");
|
||||
try g.identity(a.elem.*, out);
|
||||
try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, ";{d}]", .{a.len}));
|
||||
},
|
||||
.wrapped => |w| {
|
||||
try out.appendSlice(g.arena, w.wrapper);
|
||||
try out.append(g.arena, '<');
|
||||
try g.identity(w.inner.*, out);
|
||||
try out.append(g.arena, '>');
|
||||
},
|
||||
.cap => |k| try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, "cap<{s}>", .{k})),
|
||||
.file => |mode| try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, "file<{s}>", .{mode})),
|
||||
.applied => unreachable,
|
||||
.function_ref => |f| {
|
||||
try out.appendSlice(g.arena, "fn ");
|
||||
const instance_key = for (g.instances.items) |instance| {
|
||||
if (std.mem.eql(u8, instance.name, f.name)) break instance.key;
|
||||
} else null;
|
||||
if (instance_key) |key| {
|
||||
try out.appendSlice(g.arena, key[3..]);
|
||||
} else {
|
||||
if (std.mem.indexOfScalar(u8, f.name, '.') == null) try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, "{s}.", .{g.m.name}));
|
||||
try out.appendSlice(g.arena, f.name);
|
||||
}
|
||||
},
|
||||
fn identity(g: *Generator, initial: ast.Type, out: *std.ArrayList(u8)) Error!void {
|
||||
var pending: std.ArrayList(struct { span: ast.Span, array_len: ?u64 }) = .empty;
|
||||
var t = initial;
|
||||
// Substitution can return a previously composed type whose nesting
|
||||
// exceeds a single guarded ty() traversal. Preserve preorder text and
|
||||
// postorder key checks without growing the native stack.
|
||||
while (true) {
|
||||
switch (t.kind) {
|
||||
.name => |name| {
|
||||
if (g.typeDecl(name, t.span)) |d| {
|
||||
if (std.mem.indexOfScalar(u8, d.name, '.') == null) try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, "{s}.", .{g.m.name}));
|
||||
try out.appendSlice(g.arena, d.name);
|
||||
} else if (g.instanceTypeKey(name)) |key| {
|
||||
try out.appendSlice(g.arena, key[5..]);
|
||||
} else try out.appendSlice(g.arena, name);
|
||||
},
|
||||
.array => |a| {
|
||||
try out.appendSlice(g.arena, "[");
|
||||
try pending.append(g.arena, .{ .span = t.span, .array_len = a.len });
|
||||
t = a.elem.*;
|
||||
continue;
|
||||
},
|
||||
.wrapped => |w| {
|
||||
try out.appendSlice(g.arena, w.wrapper);
|
||||
try out.append(g.arena, '<');
|
||||
try pending.append(g.arena, .{ .span = t.span, .array_len = null });
|
||||
t = w.inner.*;
|
||||
continue;
|
||||
},
|
||||
.cap => |k| try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, "cap<{s}>", .{k})),
|
||||
.file => |mode| try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, "file<{s}>", .{mode})),
|
||||
.applied => unreachable,
|
||||
.function_ref => |f| {
|
||||
try out.appendSlice(g.arena, "fn ");
|
||||
const instance_key = for (g.instances.items) |instance| {
|
||||
if (std.mem.eql(u8, instance.name, f.name)) break instance.key;
|
||||
} else null;
|
||||
if (instance_key) |key| {
|
||||
try out.appendSlice(g.arena, key[3..]);
|
||||
} else {
|
||||
if (std.mem.indexOfScalar(u8, f.name, '.') == null) try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, "{s}.", .{g.m.name}));
|
||||
try out.appendSlice(g.arena, f.name);
|
||||
}
|
||||
},
|
||||
}
|
||||
if (out.items.len > 16384) return g.invalid(.implementation_limit, t.span, "canonical specialization key exceeds 16384 bytes", "type");
|
||||
break;
|
||||
}
|
||||
while (pending.pop()) |frame| {
|
||||
if (frame.array_len) |length| {
|
||||
try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, ";{d}]", .{length}));
|
||||
} else try out.append(g.arena, '>');
|
||||
if (out.items.len > 16384) return g.invalid(.implementation_limit, frame.span, "canonical specialization key exceeds 16384 bytes", "type");
|
||||
}
|
||||
if (out.items.len > 16384) return g.invalid(.implementation_limit, t.span, "canonical specialization key exceeds 16384 bytes", "type");
|
||||
}
|
||||
|
||||
fn named(g: *Generator, name: []const u8, args: []const ast.Type, span: ast.Span, env: Env) Error!ast.Type {
|
||||
@@ -862,3 +878,185 @@ test "generic use work metadata errors propagate and temporary entries are relea
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Independent old key writer, used only on shallow test types.
|
||||
const ReferenceIdentity = struct {
|
||||
fn identity(g: *Generator, t: ast.Type, out: *std.ArrayList(u8)) Error!void {
|
||||
switch (t.kind) {
|
||||
.name => |name| {
|
||||
if (g.typeDecl(name, t.span)) |d| {
|
||||
if (std.mem.indexOfScalar(u8, d.name, '.') == null) try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, "{s}.", .{g.m.name}));
|
||||
try out.appendSlice(g.arena, d.name);
|
||||
} else if (g.instanceTypeKey(name)) |key| {
|
||||
try out.appendSlice(g.arena, key[5..]);
|
||||
} else try out.appendSlice(g.arena, name);
|
||||
},
|
||||
.array => |a| {
|
||||
try out.appendSlice(g.arena, "[");
|
||||
try ReferenceIdentity.identity(g, a.elem.*, out);
|
||||
try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, ";{d}]", .{a.len}));
|
||||
},
|
||||
.wrapped => |w| {
|
||||
try out.appendSlice(g.arena, w.wrapper);
|
||||
try out.append(g.arena, '<');
|
||||
try ReferenceIdentity.identity(g, w.inner.*, out);
|
||||
try out.append(g.arena, '>');
|
||||
},
|
||||
.cap => |k| try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, "cap<{s}>", .{k})),
|
||||
.file => |mode| try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, "file<{s}>", .{mode})),
|
||||
.applied => unreachable,
|
||||
.function_ref => |f| {
|
||||
try out.appendSlice(g.arena, "fn ");
|
||||
const instance_key = for (g.instances.items) |instance| {
|
||||
if (std.mem.eql(u8, instance.name, f.name)) break instance.key;
|
||||
} else null;
|
||||
if (instance_key) |key| {
|
||||
try out.appendSlice(g.arena, key[3..]);
|
||||
} else {
|
||||
if (std.mem.indexOfScalar(u8, f.name, '.') == null) try out.appendSlice(g.arena, try std.fmt.allocPrint(g.arena, "{s}.", .{g.m.name}));
|
||||
try out.appendSlice(g.arena, f.name);
|
||||
}
|
||||
},
|
||||
}
|
||||
if (out.items.len > 16384) return g.invalid(.implementation_limit, t.span, "canonical specialization key exceeds 16384 bytes", "type");
|
||||
}
|
||||
};
|
||||
|
||||
fn identitySpan(line: u32) ast.Span {
|
||||
return .{ .start = .{ .line = line, .col = 7 }, .end = .{ .line = line, .col = 11 } };
|
||||
}
|
||||
|
||||
fn identityArray(a: A, elem: ast.Type, length: u64, line: u32) A.Error!ast.Type {
|
||||
const child = try a.create(ast.Type);
|
||||
child.* = elem;
|
||||
return .{ .span = identitySpan(line), .kind = .{ .array = .{ .elem = child, .len = length } } };
|
||||
}
|
||||
|
||||
fn identityWrapper(a: A, name: []const u8, elem: ast.Type, line: u32) A.Error!ast.Type {
|
||||
const child = try a.create(ast.Type);
|
||||
child.* = elem;
|
||||
return .{ .span = identitySpan(line), .kind = .{ .wrapped = .{ .wrapper = name, .inner = child } } };
|
||||
}
|
||||
|
||||
fn identityFixture(a: A) A.Error!Generator {
|
||||
const declarations = try a.alloc(ast.TypeDecl, 2);
|
||||
declarations[0] = .{ .name = "Plain", .span = identitySpan(1), .kind = .{ .@"struct" = &.{} } };
|
||||
declarations[1] = .{ .name = "lib.Item", .span = identitySpan(2), .kind = .{ .@"struct" = &.{} } };
|
||||
var g: Generator = .{ .arena = a, .m = .{ .name = "entry", .types = declarations, .functions = &.{} } };
|
||||
// Writer fixtures exercise resolved identity lookup, not complete source
|
||||
// validation or publication of these pending synthetic instances.
|
||||
try g.instances.appendSlice(a, &.{
|
||||
.{ .name = "__box", .air_name = "__box_air", .key = "type:lib.Box<u8>" },
|
||||
.{ .name = "__call", .air_name = "__call_air", .key = "fn:lib.call<u8>" },
|
||||
});
|
||||
return g;
|
||||
}
|
||||
|
||||
fn expectIdentityMatches(a: A, fixture: Generator, t: ast.Type, prefix_length: usize) !void {
|
||||
var original = fixture;
|
||||
var current = fixture;
|
||||
var old_out: std.ArrayList(u8) = .empty;
|
||||
var new_out: std.ArrayList(u8) = .empty;
|
||||
try old_out.appendNTimes(a, 'p', prefix_length);
|
||||
try new_out.appendNTimes(a, 'p', prefix_length);
|
||||
const before = ReferenceIdentity.identity(&original, t, &old_out);
|
||||
const after = current.identity(t, &new_out);
|
||||
if (before) |_| try after else |err| try std.testing.expectError(err, after);
|
||||
try std.testing.expectEqualStrings(old_out.items, new_out.items);
|
||||
try std.testing.expectEqualDeep(original.problem, current.problem);
|
||||
try std.testing.expectEqual(original.depth, current.depth);
|
||||
try std.testing.expectEqual(original.type_depth, current.type_depth);
|
||||
}
|
||||
|
||||
test "specialization identity preserves shallow keys and postorder error positions" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
const fixture = try identityFixture(a);
|
||||
var leaves: std.ArrayList(ast.Type) = .empty;
|
||||
for ([_][]const u8{ "u8", "u64", "usize", "void", "never", "bool", "region", "Plain", "lib.Item", "__box" }) |name| try leaves.append(a, .{ .span = identitySpan(10), .kind = .{ .name = name } });
|
||||
for (authority.kinds) |kind| try leaves.append(a, .{ .span = identitySpan(11), .kind = .{ .cap = kind.name } });
|
||||
for ([_][]const u8{ "read", "write", "append" }) |mode| try leaves.append(a, .{ .span = identitySpan(12), .kind = .{ .file = mode } });
|
||||
for ([_][]const u8{ "call", "lib.call", "__call" }) |name| try leaves.append(a, .{ .span = identitySpan(13), .kind = .{ .function_ref = .{ .name = name } } });
|
||||
for (leaves.items) |leaf| {
|
||||
try expectIdentityMatches(a, fixture, leaf, 0);
|
||||
for ([_]u64{ 0, 1, @as(u64, 1) << 32, std.math.maxInt(u64) }) |length| try expectIdentityMatches(a, fixture, try identityArray(a, leaf, length, 40), 0);
|
||||
for ([_][]const u8{ "ref", "mut", "slice", "slice_mut", "own", "own_array", "buffer", "task", "sender", "receiver", "mutex", "guard", "island", "frozen", "arc", "iref", "atomic" }) |wrapper| {
|
||||
const wrapped = try identityWrapper(a, wrapper, leaf, 50);
|
||||
try expectIdentityMatches(a, fixture, wrapped, 0);
|
||||
try expectIdentityMatches(a, fixture, try identityArray(a, wrapped, 123, 51), 0);
|
||||
try expectIdentityMatches(a, fixture, try identityWrapper(a, wrapper, try identityArray(a, leaf, 456, 52), 53), 0);
|
||||
}
|
||||
}
|
||||
const leaf: ast.Type = .{ .span = identitySpan(10), .kind = .{ .name = "u8" } };
|
||||
const array = try identityArray(a, leaf, 1, 40);
|
||||
const wrapper = try identityWrapper(a, "own", leaf, 50);
|
||||
for ([_]usize{ 16350, 16375, 16380, 16382, 16383, 16384, 16385 }) |prefix| for ([_]ast.Type{ leaf, array, wrapper, try identityWrapper(a, "ref", array, 60), try identityArray(a, wrapper, std.math.maxInt(u64), 70) }) |t| try expectIdentityMatches(a, fixture, t, prefix);
|
||||
}
|
||||
|
||||
test "specialization identity handles composed deep keys without changing the byte limit" {
|
||||
var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena.deinit();
|
||||
const a = arena.allocator();
|
||||
const leaf: ast.Type = .{ .span = identitySpan(10), .kind = .{ .name = "u8" } };
|
||||
var t = leaf;
|
||||
for (0..4095) |i| t = try identityArray(a, t, 1, @intCast(i + 100));
|
||||
var g: Generator = .{ .arena = a, .m = .{ .name = "keys", .types = &.{}, .functions = &.{} } };
|
||||
var out: std.ArrayList(u8) = .empty;
|
||||
try g.identity(t, &out);
|
||||
var expected: std.ArrayList(u8) = .empty;
|
||||
try expected.appendNTimes(a, '[', 4095);
|
||||
try expected.appendSlice(a, "u8");
|
||||
for (0..4095) |_| try expected.appendSlice(a, ";1]");
|
||||
try std.testing.expectEqualStrings(expected.items, out.items);
|
||||
try std.testing.expectEqual(@as(usize, 16382), out.items.len);
|
||||
try std.testing.expect(g.problem == null);
|
||||
// The final array suffix is the first postorder checkpoint over the limit.
|
||||
t = try identityArray(a, t, 1, 5000);
|
||||
out.clearRetainingCapacity();
|
||||
try std.testing.expectError(error.InvalidGeneric, g.identity(t, &out));
|
||||
try std.testing.expectEqual(identitySpan(5000), g.problem.?.span);
|
||||
try std.testing.expectEqual(@as(usize, 16386), out.items.len);
|
||||
// Prefixes alone may exceed the limit before the old leaf checkpoint.
|
||||
for (4096..32768) |i| t = try identityArray(a, t, 1, @intCast(i + 100));
|
||||
g.problem = null;
|
||||
out.clearRetainingCapacity();
|
||||
try std.testing.expectError(error.InvalidGeneric, g.identity(t, &out));
|
||||
try std.testing.expectEqual(identitySpan(10), g.problem.?.span);
|
||||
try std.testing.expectEqual(@as(usize, 32770), out.items.len);
|
||||
try std.testing.expectEqualStrings("E7006", g.problem.?.code);
|
||||
try std.testing.expectEqual(lower.InvalidReason.implementation_limit, g.problem.?.reason.?);
|
||||
}
|
||||
|
||||
test "specialization identity OOM propagates with prefix bytes and arena cleanup" {
|
||||
var scratch: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const fixture = try identityFixture(a);
|
||||
var t: ast.Type = .{ .span = identitySpan(10), .kind = .{ .function_ref = .{ .name = "__call" } } };
|
||||
for (0..64) |i| t = if (i % 2 == 0) try identityArray(a, t, std.math.maxInt(u64), @intCast(i + 100)) else try identityWrapper(a, "own", t, @intCast(i + 100));
|
||||
var original = fixture;
|
||||
var expected: std.ArrayList(u8) = .empty;
|
||||
try ReferenceIdentity.identity(&original, t, &expected);
|
||||
var fail_index: usize = 0;
|
||||
while (true) : (fail_index += 1) {
|
||||
try std.testing.expect(fail_index < 100);
|
||||
var failing: std.testing.FailingAllocator = .init(std.testing.allocator, .{ .fail_index = fail_index });
|
||||
var arena: std.heap.ArenaAllocator = .init(failing.allocator());
|
||||
var g = fixture;
|
||||
g.arena = arena.allocator();
|
||||
var out: std.ArrayList(u8) = .empty;
|
||||
const result = g.identity(t, &out);
|
||||
if (failing.has_induced_failure) {
|
||||
try std.testing.expectError(error.OutOfMemory, result);
|
||||
try std.testing.expect(g.problem == null);
|
||||
} else {
|
||||
try result;
|
||||
try std.testing.expectEqualStrings(expected.items, out.items);
|
||||
}
|
||||
try std.testing.expect(std.mem.startsWith(u8, expected.items, out.items));
|
||||
arena.deinit();
|
||||
try std.testing.expectEqual(failing.allocated_bytes, failing.freed_bytes);
|
||||
if (!failing.has_induced_failure) break;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user