air: compare and print nested types without recursion
This commit is contained in:
1 parent
d0fcc3d473
commit
53030efcea
7 files changed
+180
-67
No files matched your search
+60
-13
@@ -105,19 +105,29 @@ pub const Type = union(enum) {
|
||||
|
||||
pub const Array = struct { elem: Type, len: u64 };
|
||||
|
||||
pub fn eql(a: Type, b: Type) bool {
|
||||
if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false;
|
||||
return switch (a) {
|
||||
.int => |x| b.int == x,
|
||||
.named => |x| b.named == x,
|
||||
.local, .ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| x.eql(b.inner().?),
|
||||
.region, .scope, .shield => true,
|
||||
.array => |x| x.len == b.array.len and x.elem.eql(b.array.elem),
|
||||
.cap => |k| b.cap == k,
|
||||
.builtin => |e| b.builtin == e,
|
||||
.resource => |r| b.resource == r,
|
||||
.bool, .void, .never => true,
|
||||
};
|
||||
pub fn eql(initial_a: Type, initial_b: Type) bool {
|
||||
var a = initial_a;
|
||||
var b = initial_b;
|
||||
while (true) {
|
||||
if (std.meta.activeTag(a) != std.meta.activeTag(b)) return false;
|
||||
switch (a) {
|
||||
.int => |x| return b.int == x,
|
||||
.named => |x| return b.named == x,
|
||||
.local, .ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| {
|
||||
a = x.*;
|
||||
b = b.inner().?;
|
||||
},
|
||||
.array => |x| {
|
||||
if (x.len != b.array.len) return false;
|
||||
a = x.elem;
|
||||
b = b.array.elem;
|
||||
},
|
||||
.cap => |k| return b.cap == k,
|
||||
.builtin => |e| return b.builtin == e,
|
||||
.resource => |r| return b.resource == r,
|
||||
.region, .scope, .shield, .bool, .void, .never => return true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Element or pointee type of storage, borrow and slice types.
|
||||
@@ -836,3 +846,40 @@ test "IntType host types" {
|
||||
try std.testing.expect(IntType.Zig(.usize) == u64);
|
||||
try std.testing.expectEqual(@as(u8, 128), IntType.u128.bits());
|
||||
}
|
||||
|
||||
test "deep structural type equality checks inner tags and every array length" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const a = scratch.allocator();
|
||||
const left = try a.alloc(Type.Array, 65_536);
|
||||
const right = try a.alloc(Type.Array, left.len);
|
||||
var x: Type = .{ .int = .u8 };
|
||||
var y = x;
|
||||
for (left, right, 0..) |*l, *r, i| {
|
||||
l.* = .{ .elem = x, .len = i + 1 };
|
||||
r.* = .{ .elem = y, .len = i + 1 };
|
||||
x = .{ .array = l };
|
||||
y = .{ .array = r };
|
||||
}
|
||||
try std.testing.expect(x.eql(y) and y.eql(x));
|
||||
right[0].elem = .bool;
|
||||
try std.testing.expect(!x.eql(y) and !y.eql(x));
|
||||
right[0].elem = .{ .int = .u8 };
|
||||
right[32_768].len += 1;
|
||||
try std.testing.expect(!x.eql(y) and !y.eql(x));
|
||||
right[32_768].len -= 1;
|
||||
try std.testing.expect(x.eql(y));
|
||||
try std.testing.expect(!x.eql(.{ .ref = &y }));
|
||||
}
|
||||
|
||||
test "structural equality preserves each unary constructor identity" {
|
||||
const x: Type = .{ .named = 0 };
|
||||
const y: Type = .{ .named = 1 };
|
||||
inline for (.{ .local, .ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic }) |tag| {
|
||||
const left: Type = @unionInit(Type, @tagName(tag), &x);
|
||||
const equal: Type = @unionInit(Type, @tagName(tag), &x);
|
||||
const different: Type = @unionInit(Type, @tagName(tag), &y);
|
||||
try std.testing.expect(left.eql(equal));
|
||||
try std.testing.expect(!left.eql(different) and !left.eql(x));
|
||||
}
|
||||
}
|
||||
+104
-52
@@ -4,8 +4,9 @@ const std = @import("std");
|
||||
const air = @import("module.zig");
|
||||
|
||||
const Writer = std.Io.Writer;
|
||||
const PrintError = std.mem.Allocator.Error || Writer.Error;
|
||||
|
||||
pub fn print(m: air.Module, w: *Writer) Writer.Error!void {
|
||||
pub fn print(allocator: std.mem.Allocator, m: air.Module, w: *Writer) PrintError!void {
|
||||
try w.print("module {s}\nair {d}\nlang {d}.{d}\nfeatures [", .{ m.name, m.format_version, m.lang[0], m.lang[1] });
|
||||
try list(w, m.features);
|
||||
try w.writeAll("]\n");
|
||||
@@ -19,7 +20,7 @@ pub fn print(m: air.Module, w: *Writer) Writer.Error!void {
|
||||
for (fields, 0..) |f, i| {
|
||||
try w.writeAll(if (i == 0) " " else ", ");
|
||||
try w.print("{s}: ", .{f.name});
|
||||
try printType(m, f.ty, w);
|
||||
try printType(allocator, m, f.ty, w);
|
||||
}
|
||||
try w.writeAll(if (fields.len == 0) "}\n" else " }\n");
|
||||
},
|
||||
@@ -32,7 +33,7 @@ pub fn print(m: air.Module, w: *Writer) Writer.Error!void {
|
||||
try w.writeByte('(');
|
||||
for (v.payload, 0..) |t, j| {
|
||||
if (j != 0) try w.writeAll(", ");
|
||||
try printType(m, t, w);
|
||||
try printType(allocator, m, t, w);
|
||||
}
|
||||
try w.writeByte(')');
|
||||
}
|
||||
@@ -44,9 +45,9 @@ pub fn print(m: air.Module, w: *Writer) Writer.Error!void {
|
||||
|
||||
for (m.functions) |f| {
|
||||
try w.print("\nfn @{s}(", .{f.name});
|
||||
try params(m, f, f.params, w);
|
||||
try params(allocator, m, f, f.params, w);
|
||||
try w.writeAll(") -> ");
|
||||
try printType(m, f.ret, w);
|
||||
try printType(allocator, m, f.ret, w);
|
||||
if (f.ret_from) |i| try w.print(" from %{s}", .{f.values[f.params[i].value].name});
|
||||
try w.writeAll("\neffects [");
|
||||
try list(w, f.effects);
|
||||
@@ -65,7 +66,7 @@ pub fn print(m: air.Module, w: *Writer) Writer.Error!void {
|
||||
try w.writeAll(b.name);
|
||||
if (b.params.len != 0) {
|
||||
try w.writeByte('(');
|
||||
try params(m, f, b.params, w);
|
||||
try params(allocator, m, f, b.params, w);
|
||||
try w.writeByte(')');
|
||||
}
|
||||
try w.writeAll(":\n");
|
||||
@@ -76,7 +77,7 @@ pub fn print(m: air.Module, w: *Writer) Writer.Error!void {
|
||||
if (inst.result2) |r2| try w.print(", %{s}", .{f.values[r2].name});
|
||||
try w.writeAll(" = ");
|
||||
}
|
||||
try printOp(m, f, inst.op, w);
|
||||
try printOp(allocator, m, f, inst.op, w);
|
||||
try w.writeByte('\n');
|
||||
}
|
||||
}
|
||||
@@ -90,46 +91,49 @@ fn list(w: *Writer, items: []const []const u8) Writer.Error!void {
|
||||
}
|
||||
}
|
||||
|
||||
fn params(m: air.Module, f: air.Function, ps: []const air.Param, w: *Writer) Writer.Error!void {
|
||||
fn params(allocator: std.mem.Allocator, m: air.Module, f: air.Function, ps: []const air.Param, w: *Writer) PrintError!void {
|
||||
for (ps, 0..) |p, i| {
|
||||
if (i != 0) try w.writeAll(", ");
|
||||
try w.print("%{s}: ", .{f.values[p.value].name});
|
||||
try printType(m, p.ty, w);
|
||||
try printType(allocator, m, p.ty, w);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn printType(m: air.Module, t: air.Type, w: *Writer) Writer.Error!void {
|
||||
switch (t) {
|
||||
.int => |it| try w.writeAll(@tagName(it)),
|
||||
.named => |i| try w.print("@{s}", .{m.types[i].name}),
|
||||
.local, .ref, .mut, .slice, .slice_mut => |inner| {
|
||||
try w.print("{s}<", .{@tagName(t)});
|
||||
try printType(m, inner.*, w);
|
||||
try w.writeByte('>');
|
||||
},
|
||||
.array => |a| {
|
||||
try w.writeAll("array<");
|
||||
try printType(m, a.elem, w);
|
||||
try w.print(", {d}>", .{a.len});
|
||||
},
|
||||
.cap => |k| try w.print("cap<{s}>", .{air.authority.kinds[k].name}),
|
||||
.builtin => |b| try w.writeAll(@tagName(b)),
|
||||
.resource => |r| try w.writeAll(r.text()),
|
||||
.own, .own_array, .buffer => |inner| {
|
||||
try w.print("{s}<", .{@tagName(t)});
|
||||
try printType(m, inner.*, w);
|
||||
try w.writeByte('>');
|
||||
},
|
||||
.region => try w.writeAll("region"),
|
||||
.scope => try w.writeAll("scope"),
|
||||
.task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |inner| {
|
||||
try w.print("{s}<", .{@tagName(t)});
|
||||
try printType(m, inner.*, w);
|
||||
try w.writeByte('>');
|
||||
},
|
||||
.shield => try w.writeAll("shield"),
|
||||
.bool, .void, .never => try w.writeAll(@tagName(t)),
|
||||
/// The allocator belongs to the compiler/serializer, never to an Aji program.
|
||||
/// Save suffixes explicitly so arbitrary finite anonymous nesting does not
|
||||
/// consume native stack. Named declarations remain names, not expanded graphs.
|
||||
pub fn printType(allocator: std.mem.Allocator, m: air.Module, initial: air.Type, w: *Writer) PrintError!void {
|
||||
const Suffix = union(enum) { close, array: u64 };
|
||||
var pending: std.ArrayList(Suffix) = .empty;
|
||||
defer pending.deinit(allocator);
|
||||
var t = initial;
|
||||
while (true) {
|
||||
switch (t) {
|
||||
.array => |row| {
|
||||
try w.writeAll("array<");
|
||||
try pending.append(allocator, .{ .array = row.len });
|
||||
t = row.elem;
|
||||
continue;
|
||||
},
|
||||
.local, .ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |inner| {
|
||||
try w.print("{s}<", .{@tagName(t)});
|
||||
try pending.append(allocator, .close);
|
||||
t = inner.*;
|
||||
continue;
|
||||
},
|
||||
.int => |it| try w.writeAll(@tagName(it)),
|
||||
.named => |i| try w.print("@{s}", .{m.types[i].name}),
|
||||
.cap => |k| try w.print("cap<{s}>", .{air.authority.kinds[k].name}),
|
||||
.builtin => |b| try w.writeAll(@tagName(b)),
|
||||
.resource => |r| try w.writeAll(r.text()),
|
||||
.region, .scope, .shield, .bool, .void, .never => try w.writeAll(@tagName(t)),
|
||||
}
|
||||
break;
|
||||
}
|
||||
while (pending.pop()) |suffix| switch (suffix) {
|
||||
.close => try w.writeByte('>'),
|
||||
.array => |len| try w.print(", {d}>", .{len}),
|
||||
};
|
||||
}
|
||||
|
||||
fn scalarName(t: air.Type) []const u8 {
|
||||
@@ -169,7 +173,7 @@ pub fn literal(it: air.IntType, bits: u128, w: *Writer) Writer.Error!void {
|
||||
}
|
||||
}
|
||||
|
||||
fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!void {
|
||||
fn printOp(allocator: std.mem.Allocator, m: air.Module, f: air.Function, op: air.Op, w: *Writer) PrintError!void {
|
||||
switch (op) {
|
||||
.@"const" => |c| switch (c.ty) {
|
||||
.bool => try w.print("const.bool {s}", .{if (c.bits != 0) "true" else "false"}),
|
||||
@@ -212,12 +216,12 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
},
|
||||
.array_make => |x| {
|
||||
try w.writeAll("array.make ");
|
||||
try printType(m, x.ty, w);
|
||||
try printType(allocator, m, x.ty, w);
|
||||
try values(f, x.args, w);
|
||||
},
|
||||
.aggregate_unpack => |x| {
|
||||
try w.writeAll(if (x.ty == .array) "array.unpack " else "struct.unpack ");
|
||||
try printType(m, x.ty, w);
|
||||
try printType(allocator, m, x.ty, w);
|
||||
try w.writeByte(' ');
|
||||
try value(f, x.value, w);
|
||||
try w.print(", next -> @{s}", .{f.blocks[x.next].name});
|
||||
@@ -267,7 +271,7 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
},
|
||||
inline .enum_switch, .enum_take => |x| {
|
||||
try w.writeAll(if (op == .enum_take) "enum.take " else "enum.switch ");
|
||||
try printType(m, x.ty, w);
|
||||
try printType(allocator, m, x.ty, w);
|
||||
try w.writeByte(' ');
|
||||
try value(f, x.value, w);
|
||||
for (x.arms) |a| try w.print(", {s} -> @{s}", .{ m.variantName(x.ty, a.variant), f.blocks[a.block].name });
|
||||
@@ -286,7 +290,7 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
.trap => try w.writeAll("trap"),
|
||||
.local_alloc => |t| {
|
||||
try w.writeAll("local.alloc ");
|
||||
try printType(m, t, w);
|
||||
try printType(allocator, m, t, w);
|
||||
},
|
||||
.local_store => |x| try two(f, w, "local.store", x.local, x.value),
|
||||
.local_zero => |v| try one(f, w, "local.zero", v),
|
||||
@@ -303,7 +307,7 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
.store => |x| try two(f, w, "store", x.borrow, x.value),
|
||||
.array_splat => |x| {
|
||||
try w.writeAll("array.splat ");
|
||||
try printType(m, x.ty, w);
|
||||
try printType(allocator, m, x.ty, w);
|
||||
try w.writeByte(' ');
|
||||
try value(f, x.value, w);
|
||||
},
|
||||
@@ -326,7 +330,7 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
},
|
||||
.buffer_new => |x| {
|
||||
try w.writeAll("buffer.new ");
|
||||
try printType(m, x.ty, w);
|
||||
try printType(allocator, m, x.ty, w);
|
||||
try w.print(" %{s}, %{s}", .{ f.values[x.n].name, f.values[x.cap].name });
|
||||
try okErr(f, w, x.ok, x.err);
|
||||
},
|
||||
@@ -351,7 +355,7 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
inline .own_new, .own_pack => |x, tag| {
|
||||
if (tag == .own_pack) {
|
||||
try w.writeAll("own.pack ");
|
||||
try printType(m, x.ty, w);
|
||||
try printType(allocator, m, x.ty, w);
|
||||
try w.writeByte(' ');
|
||||
try value(f, x.value, w);
|
||||
try w.writeAll(", ");
|
||||
@@ -403,7 +407,7 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
.task_shield_end => |v| try one(f, w, "task.shield.end", v),
|
||||
.channel_make => |x| {
|
||||
try w.writeAll("channel.make ");
|
||||
try printType(m, x.elem, w);
|
||||
try printType(allocator, m, x.elem, w);
|
||||
try w.writeAll(", ");
|
||||
try value(f, x.cap, w);
|
||||
try w.writeAll(", ");
|
||||
@@ -525,6 +529,54 @@ fn two(f: air.Function, w: *Writer, op: []const u8, a: air.ValueId, b: air.Value
|
||||
|
||||
const parse = @import("parse.zig").parse;
|
||||
|
||||
test "deep array type printing retains suffix order and round trips canonically" {
|
||||
var scratch = std.heap.ArenaAllocator.init(std.testing.allocator);
|
||||
defer scratch.deinit();
|
||||
const allocator = scratch.allocator();
|
||||
const rows = try allocator.alloc(air.Type.Array, 65_536);
|
||||
var t: air.Type = .{ .int = .u8 };
|
||||
for (rows, 1..) |*row, len| {
|
||||
row.* = .{ .elem = t, .len = len };
|
||||
t = .{ .array = row };
|
||||
}
|
||||
const m: air.Module = .{ .name = "deep", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{ "arrays", "ownership" }, .types = &.{}, .functions = &.{} };
|
||||
var actual: Writer.Allocating = .init(allocator);
|
||||
try printType(allocator, m, t, &actual.writer);
|
||||
var expected: Writer.Allocating = .init(allocator);
|
||||
for (rows) |_| try expected.writer.writeAll("array<");
|
||||
try expected.writer.writeAll("u8");
|
||||
for (1..rows.len + 1) |len| try expected.writer.print(", {d}>", .{len});
|
||||
try std.testing.expectEqualStrings(expected.written(), actual.written());
|
||||
var source: Writer.Allocating = .init(allocator);
|
||||
try source.writer.writeAll("module deep\nair 0\nlang 0.1\nfeatures [arrays, ownership]\nfn @f(%x: ");
|
||||
try source.writer.writeAll(expected.written());
|
||||
try source.writer.writeAll(") -> void\neffects []\nentry:\n ret\n");
|
||||
try roundTrip(source.written());
|
||||
}
|
||||
|
||||
test "type printer retains all unary constructor spellings and leaf names" {
|
||||
const allocator = std.testing.allocator;
|
||||
const leaf: air.Type = .{ .named = 0 };
|
||||
const m: air.Module = .{ .name = "test", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{.{ .name = "Node", .span = .{ .line = 1, .col = 1 }, .kind = .{ .@"struct" = &.{} } }}, .functions = &.{} };
|
||||
inline for (.{ .local, .ref, .mut, .slice, .slice_mut, .own, .own_array, .buffer, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic }) |tag| {
|
||||
const t: air.Type = @unionInit(air.Type, @tagName(tag), &leaf);
|
||||
var out: Writer.Allocating = .init(allocator);
|
||||
defer out.deinit();
|
||||
try printType(allocator, m, t, &out.writer);
|
||||
try std.testing.expectEqualStrings(@tagName(tag) ++ "<@Node>", out.written());
|
||||
}
|
||||
}
|
||||
|
||||
test "type printer propagates suffix allocation and buffered writer failures" {
|
||||
const m: air.Module = .{ .name = "test", .format_version = 0, .lang = .{ 0, 1 }, .features = &.{}, .types = &.{}, .functions = &.{} };
|
||||
const row: air.Type.Array = .{ .elem = .{ .int = .u8 }, .len = 1 };
|
||||
var buf: [64]u8 = undefined;
|
||||
var out: Writer = .fixed(&buf);
|
||||
try std.testing.expectError(error.OutOfMemory, printType(std.testing.failing_allocator, m, .{ .array = &row }, &out));
|
||||
out = .fixed(buf[0..6]);
|
||||
try std.testing.expectError(error.WriteFailed, printType(std.testing.allocator, m, .{ .array = &row }, &out));
|
||||
}
|
||||
|
||||
fn roundTrip(src: []const u8) !void {
|
||||
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena_state.deinit();
|
||||
@@ -533,12 +585,12 @@ fn roundTrip(src: []const u8) !void {
|
||||
const r1 = try parse(arena, "a.air", src);
|
||||
try std.testing.expectEqual(@as(usize, 0), r1.diagnostics.len);
|
||||
var out1: Writer.Allocating = .init(arena);
|
||||
try print(r1.module.?, &out1.writer);
|
||||
try print(arena, r1.module.?, &out1.writer);
|
||||
|
||||
const r2 = try parse(arena, "b.air", out1.written());
|
||||
try std.testing.expectEqual(@as(usize, 0), r2.diagnostics.len);
|
||||
var out2: Writer.Allocating = .init(arena);
|
||||
try print(r2.module.?, &out2.writer);
|
||||
try print(arena, r2.module.?, &out2.writer);
|
||||
|
||||
try std.testing.expectEqualStrings(out1.written(), out2.written());
|
||||
}
|
||||
@@ -579,7 +631,7 @@ test "canonical form of a messy module" {
|
||||
const r = try parse(arena, "t.air", src);
|
||||
try std.testing.expectEqual(@as(usize, 0), r.diagnostics.len);
|
||||
var out: Writer.Allocating = .init(arena);
|
||||
try print(r.module.?, &out.writer);
|
||||
try print(arena, r.module.?, &out.writer);
|
||||
try std.testing.expectEqualStrings(
|
||||
\\module t.x
|
||||
\\air 0
|
||||
|
||||
+1
-1
@@ -65,7 +65,7 @@ pub const Context = struct {
|
||||
|
||||
pub fn typeName(c: *Context, t: air.Type) std.mem.Allocator.Error![]const u8 {
|
||||
var w: std.Io.Writer.Allocating = .init(c.arena);
|
||||
air.print.printType(c.module, t, &w.writer) catch return error.OutOfMemory;
|
||||
air.print.printType(c.arena, c.module, t, &w.writer) catch return error.OutOfMemory;
|
||||
return w.written();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user