~alva/zig-bare

81525c72a5a8f3afb70cd14a82d6330c835c659d — Alva 2 months ago 0fccd09
Better compile error messages
2 files changed, 42 insertions(+), 19 deletions(-)

M src/bare.zig
M src/test.zig
M src/bare.zig => src/bare.zig +10 -9
@@ 37,7 37,7 @@ pub const Reader = struct {
            .Array => self.readArray(T, stream),
            .Union => self.readUnion(T, stream),
            .Pointer => self.readPointer(T, stream),
            else => @compileError("unsupported type"),
            else => @compileError("unsupported type " ++ @typeName(T)),
        };
    }



@@ 86,14 86,14 @@ pub const Reader = struct {
    fn readInt(self: *Self, comptime T: type, stream: var) !T {
        return switch (T.bit_count) {
            8, 16, 32, 64 => stream.readIntLittle(T),
            else => @compileError("unsupported type"),
            else => @compileError("unsupported integer type " ++ @typeName(T)),
        };
    }

    fn readFloat(self: *Self, comptime T: type, stream: var) !T {
        return switch (T.bit_count) {
            32, 64 => try stream.readIntLittle(T),
            else => @compileError("unsupported type"),
            else => @compileError("unsupported float type " ++ @typeName(T)),
        };
    }



@@ 184,7 184,7 @@ pub const Reader = struct {
        const V = HashMapValueType(T);

        if (comptime !isValidHashMapKeyType(K))
            @compileError("unsupported hashmap key type");
            @compileError("unsupported hashmap key type " ++ @typeName(K));

        var i = try self.readVarUint(stream);
        var map = T.init(&self.arena.allocator);


@@ 227,7 227,7 @@ pub const Writer = struct {
            .Array => self.writeArray(value, stream),
            .Union => self.writeUnion(value, stream),
            .Pointer => self.writePointer(value, stream),
            else => @compileError("unsupported type"),
            else => @compileError("unsupported type " ++ @typeName(T)),
        };
    }



@@ 261,14 261,14 @@ pub const Writer = struct {
    fn writeInt(self: *Self, comptime T: type, value: T, stream: var) !void {
        return switch (T.bit_count) {
            8, 16, 32, 64 => stream.writeIntLittle(T, value),
            else => @compileError("unsupported type"),
            else => @compileError("unsupported integer type " ++ @typeName(T)),
        };
    }

    fn writeFloat(self: *Self, comptime T: type, value: T, stream: var) !void {
        return switch (T.bit_count) {
            32, 64 => stream.writeIntLittle(T, value),
            else => @compileError("unsupported type"),
            else => @compileError("unsupported float type " ++ @typeName(T)),
        };
    }



@@ 322,9 322,10 @@ pub const Writer = struct {

    fn writeHashMap(self: *Self, value: var, stream: var) !void {
        const T = @TypeOf(value);
        const K = HashMapKeyType(T);

        if (comptime !isValidHashMapKeyType(HashMapKeyType(T)))
            @compileError("unsupported hashmap key type");
        if (comptime !isValidHashMapKeyType(K))
            @compileError("unsupported hashmap key type " ++ @typeName(K));

        try self.writeVarUint(value.size, stream);


M src/test.zig => src/test.zig +32 -10
@@ 134,7 134,7 @@ test "read struct with void members" {
        \\  const Foo = struct { a: u8, b: void };
        \\  _ = try Reader.init(testing.allocator).read(Foo, fbs.inStream());
        \\}
        , "unsupported type"
        , "unsupported type void"
    );
}



@@ 215,7 215,7 @@ test "read map[u8]void" {
        \\  const HM = std.AutoHashMap(u8, void);
        \\  _ = try Reader.init(testing.allocator).read(HM, fbs.inStream());
        \\}
        , "unsupported type"
        , "unsupported type void"
    );
}



@@ 242,17 242,27 @@ test "read void" {
        \\  var fbs = io.fixedBufferStream("lol");
        \\  _ = try Reader.init(testing.allocator).read(void, fbs.inStream());
        \\}
        , "unsupported type"
        , "unsupported type void"
    );
}

test "read unsupported type" {
test "read unsupported integer type" {
    try testCompileError(
        \\pub fn main() void {
        \\  var fbs = io.fixedBufferStream("lol");
        \\  _ = try Reader.init(testing.allocator).read(u7, fbs.inStream());
        \\}
        , "unsupported type"
        , "unsupported integer type u7"
    );
}

test "read unsupported float type" {
    try testCompileError(
        \\pub fn main() void {
        \\  var fbs = io.fixedBufferStream("lol");
        \\  _ = try Reader.init(testing.allocator).read(f16, fbs.inStream());
        \\}
        , "unsupported float type f16"
    );
}



@@ 413,7 423,7 @@ test "write slice of void" {
        \\  const foo: []const void = &[_]void{ {}, {} };
        \\  try Writer.init().write(foo, fbs.outStream());
        \\}
        , "unsupported type"
        , "unsupported type void"
    );
}



@@ 506,11 516,11 @@ test "write void" {
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  try Writer.init().write(void, fbs.outStream());
        \\}
        , "unsupported type"
        , "unsupported type" // TODO: I get `type` as the name here.
    );
}

test "write unsupported type" {
test "write unsupported integer type" {
    try testCompileError(
        \\pub fn main() void {
        \\  var buf: [0x100]u8 = undefined;


@@ 518,7 528,19 @@ test "write unsupported type" {
        \\  const foo: u7 = 7;
        \\  try Writer.init().write(foo, fbs.outStream());
        \\}
        , "unsupported type"
        , "unsupported integer type u7"
    );
}

test "write unsupported float type" {
    try testCompileError(
        \\pub fn main() void {
        \\  var buf: [0x100]u8 = undefined;
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  const foo: f16 = 4.2;
        \\  try Writer.init().write(foo, fbs.outStream());
        \\}
        , "unsupported float type f16"
    );
}



@@ 734,7 756,7 @@ test "invariant: hashmap keys must be of primitive type" {
        \\  var fbs = io.fixedBufferStream(&buf);
        \\  try Writer.init().write(hm, fbs.outStream());
        \\}
        , "unsupported hashmap key type"
        , "unsupported hashmap key type [64]u8"
    );
}