Files
sx/src/ir/types.test.zig
agra 92638ae9b5 lang 2.1: Pack as a type-system value
Add a `pack` variant to IR `TypeInfo` — an ordered, interned sequence of
per-position element types (`PackInfo { elements: []const TypeId }`) — with
constructor (`packType`), structural equality + hashing, and a `pack(T0, …)`
printer. A pack is comptime-only: it lowers to flat positional args before
codegen and has no runtime layout, so `sizeOf` and `toLLVMType` bail loudly
rather than inventing a size. 5 unit tests (N=0/1/3, dedup, order/arity
distinctness, distinct-from-tuple, printer).

Also: give TypeTable an arena for the slices its constructors dupe (freed at
deinit), and add the missing `usize`/`isize` arms to `sizeOf` (a latent
non-exhaustive switch) so types.test.zig compiles and runs leak-free.
2026-05-29 15:24:46 +03:00

197 lines
7.0 KiB
Zig

// Tests for types.zig
const std = @import("std");
const types = @import("types.zig");
const TypeId = types.TypeId;
const TypeTable = types.TypeTable;
const TypeInfo = types.TypeInfo;
test "builtin types pre-populated" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
// Verify builtin slots
try std.testing.expectEqual(TypeInfo.void, table.get(.void));
try std.testing.expectEqual(TypeInfo.bool, table.get(.bool));
try std.testing.expectEqual(TypeInfo{ .signed = 32 }, table.get(.s32));
try std.testing.expectEqual(TypeInfo{ .unsigned = 8 }, table.get(.u8));
try std.testing.expectEqual(TypeInfo.f64, table.get(.f64));
try std.testing.expectEqual(TypeInfo.string, table.get(.string));
try std.testing.expectEqual(TypeInfo.any, table.get(.any));
}
test "intern deduplicates structural types" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const ptr1 = table.ptrTo(.s32);
const ptr2 = table.ptrTo(.s32);
try std.testing.expectEqual(ptr1, ptr2);
const ptr3 = table.ptrTo(.f64);
try std.testing.expect(ptr1 != ptr3);
}
test "slice and array interning" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const slice1 = table.sliceOf(.s32);
const slice2 = table.sliceOf(.s32);
try std.testing.expectEqual(slice1, slice2);
const arr1 = table.arrayOf(.u8, 10);
const arr2 = table.arrayOf(.u8, 10);
const arr3 = table.arrayOf(.u8, 20);
try std.testing.expectEqual(arr1, arr2);
try std.testing.expect(arr1 != arr3);
}
test "optional interning" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const opt1 = table.optionalOf(.s32);
const opt2 = table.optionalOf(.s32);
try std.testing.expectEqual(opt1, opt2);
const opt3 = table.optionalOf(.f64);
try std.testing.expect(opt1 != opt3);
}
test "function type interning" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const params = &[_]TypeId{ .s32, .s32 };
const fn1 = table.functionType(params, .s64);
const fn2 = table.functionType(params, .s64);
try std.testing.expectEqual(fn1, fn2);
const fn3 = table.functionType(params, .f64);
try std.testing.expect(fn1 != fn3);
}
test "string pool interning" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const id1 = table.internString("Point");
const id2 = table.internString("Point");
const id3 = table.internString("Rect");
try std.testing.expectEqual(id1, id2);
try std.testing.expect(id1 != id3);
try std.testing.expectEqualStrings("Point", table.getString(id1));
try std.testing.expectEqualStrings("Rect", table.getString(id3));
}
test "sizeOf builtins" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
try std.testing.expectEqual(@as(u32, 0), table.sizeOf(.void));
try std.testing.expectEqual(@as(u32, 1), table.sizeOf(.bool));
try std.testing.expectEqual(@as(u32, 4), table.sizeOf(.s32));
try std.testing.expectEqual(@as(u32, 8), table.sizeOf(.s64));
try std.testing.expectEqual(@as(u32, 1), table.sizeOf(.u8));
try std.testing.expectEqual(@as(u32, 4), table.sizeOf(.f32));
try std.testing.expectEqual(@as(u32, 8), table.sizeOf(.f64));
try std.testing.expectEqual(@as(u32, 16), table.sizeOf(.string));
try std.testing.expectEqual(@as(u32, 8), table.sizeOf(table.ptrTo(.s32)));
try std.testing.expectEqual(@as(u32, 16), table.sizeOf(table.sliceOf(.s32)));
}
test "typeName for builtins" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
try std.testing.expectEqualStrings("s32", table.typeName(.s32));
try std.testing.expectEqualStrings("bool", table.typeName(.bool));
try std.testing.expectEqualStrings("string", table.typeName(.string));
try std.testing.expectEqualStrings("void", table.typeName(.void));
try std.testing.expectEqualStrings("Any", table.typeName(.any));
}
// ── Pack type (Feature 1, Step 2.1) ──────────────────────────────────
test "pack type: construct, element access, intern dedup (N=3)" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const elems = &[_]TypeId{ .bool, .s32, .string };
const p1 = table.packType(elems);
const p2 = table.packType(elems);
try std.testing.expectEqual(p1, p2); // structural dedup
const info = table.get(p1);
try std.testing.expect(info == .pack);
try std.testing.expectEqual(@as(usize, 3), info.pack.elements.len);
try std.testing.expectEqual(TypeId.bool, info.pack.elements[0]);
try std.testing.expectEqual(TypeId.s32, info.pack.elements[1]);
try std.testing.expectEqual(TypeId.string, info.pack.elements[2]);
}
test "pack type: empty pack (N=0)" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const empty1 = table.packType(&.{});
const empty2 = table.packType(&.{});
try std.testing.expectEqual(empty1, empty2);
const info = table.get(empty1);
try std.testing.expect(info == .pack);
try std.testing.expectEqual(@as(usize, 0), info.pack.elements.len);
}
test "pack type: single element (N=1)" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const p = table.packType(&[_]TypeId{.f64});
const info = table.get(p);
try std.testing.expectEqual(@as(usize, 1), info.pack.elements.len);
try std.testing.expectEqual(TypeId.f64, info.pack.elements[0]);
}
test "pack type: distinct element lists are distinct types" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
const a = table.packType(&[_]TypeId{ .bool, .s32 });
const b = table.packType(&[_]TypeId{ .s32, .bool }); // order matters
const c = table.packType(&[_]TypeId{.bool}); // arity matters
try std.testing.expect(a != b);
try std.testing.expect(a != c);
try std.testing.expect(b != c);
// A pack is distinct from the tuple of the same elements.
const tup = table.intern(.{ .tuple = .{ .fields = &[_]TypeId{ .bool, .s32 }, .names = null } });
try std.testing.expect(a != tup);
}
test "pack type: formatTypeName" {
const alloc = std.testing.allocator;
var table = TypeTable.init(alloc);
defer table.deinit();
var arena = std.heap.ArenaAllocator.init(alloc);
defer arena.deinit();
const p = table.packType(&[_]TypeId{ .bool, .s32, .string });
try std.testing.expectEqualStrings("pack(bool, s32, string)", table.formatTypeName(arena.allocator(), p));
const empty = table.packType(&.{});
try std.testing.expectEqualStrings("pack()", table.formatTypeName(arena.allocator(), empty));
}