aboutsummaryrefslogtreecommitdiff
path: root/test/compile_errors.zig
diff options
context:
space:
mode:
authorVeikka Tuominen <git@vexu.eu>2020-09-14 16:43:49 +0300
committerGitHub <noreply@github.com>2020-09-14 16:43:49 +0300
commitd073836894df8b9fb56f24f577a51dd09a85a932 (patch)
tree920723ea7cbe577934f0fb9670332f40dc7fd94a /test/compile_errors.zig
parentc49435f76b07cbf3fdd6a10303a1a0ee4290e59a (diff)
parentacdf1f0bde9a07cae2fbe33739f5f4aee7989f7b (diff)
downloadzig-d073836894df8b9fb56f24f577a51dd09a85a932.tar.gz
zig-d073836894df8b9fb56f24f577a51dd09a85a932.zip
Merge pull request #6172 from tadeokondrak/@Type(.Union)
Implement @Type for Union
Diffstat (limited to 'test/compile_errors.zig')
-rw-r--r--test/compile_errors.zig131
1 files changed, 130 insertions, 1 deletions
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
index 31f2b57dc8..f457c74609 100644
--- a/test/compile_errors.zig
+++ b/test/compile_errors.zig
@@ -10,6 +10,135 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:2:37: error: expected type '[:1]const u8', found '*const [2:2]u8'",
});
+ cases.add("@Type for union with opaque field",
+ \\const TypeInfo = @import("builtin").TypeInfo;
+ \\const Untagged = @Type(.{
+ \\ .Union = .{
+ \\ .layout = .Auto,
+ \\ .tag_type = null,
+ \\ .fields = &[_]TypeInfo.UnionField{
+ \\ .{ .name = "foo", .field_type = @Type(.Opaque) },
+ \\ },
+ \\ .decls = &[_]TypeInfo.Declaration{},
+ \\ },
+ \\});
+ \\export fn entry() void {
+ \\ _ = Untagged{};
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:2:25: error: opaque types have unknown size and therefore cannot be directly embedded in unions",
+ "tmp.zig:13:17: note: referenced here",
+ });
+
+ cases.add("@Type for union with zero fields",
+ \\const TypeInfo = @import("builtin").TypeInfo;
+ \\const Untagged = @Type(.{
+ \\ .Union = .{
+ \\ .layout = .Auto,
+ \\ .tag_type = null,
+ \\ .fields = &[_]TypeInfo.UnionField{},
+ \\ .decls = &[_]TypeInfo.Declaration{},
+ \\ },
+ \\});
+ \\export fn entry() void {
+ \\ _ = Untagged{};
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:2:25: error: unions must have 1 or more fields",
+ "tmp.zig:11:17: note: referenced here",
+ });
+
+ cases.add("@Type for exhaustive enum with zero fields",
+ \\const TypeInfo = @import("builtin").TypeInfo;
+ \\const Tag = @Type(.{
+ \\ .Enum = .{
+ \\ .layout = .Auto,
+ \\ .tag_type = u1,
+ \\ .fields = &[_]TypeInfo.EnumField{},
+ \\ .decls = &[_]TypeInfo.Declaration{},
+ \\ .is_exhaustive = true,
+ \\ },
+ \\});
+ \\export fn entry() void {
+ \\ _ = @intToEnum(Tag, 0);
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:2:20: error: enums must have 1 or more fields",
+ "tmp.zig:12:9: note: referenced here",
+ });
+
+ cases.add("@Type for tagged union with extra union field",
+ \\const TypeInfo = @import("builtin").TypeInfo;
+ \\const Tag = @Type(.{
+ \\ .Enum = .{
+ \\ .layout = .Auto,
+ \\ .tag_type = u1,
+ \\ .fields = &[_]TypeInfo.EnumField{
+ \\ .{ .name = "signed", .value = 0 },
+ \\ .{ .name = "unsigned", .value = 1 },
+ \\ },
+ \\ .decls = &[_]TypeInfo.Declaration{},
+ \\ .is_exhaustive = true,
+ \\ },
+ \\});
+ \\const Tagged = @Type(.{
+ \\ .Union = .{
+ \\ .layout = .Auto,
+ \\ .tag_type = Tag,
+ \\ .fields = &[_]TypeInfo.UnionField{
+ \\ .{ .name = "signed", .field_type = i32 },
+ \\ .{ .name = "unsigned", .field_type = u32 },
+ \\ .{ .name = "arst", .field_type = f32 },
+ \\ },
+ \\ .decls = &[_]TypeInfo.Declaration{},
+ \\ },
+ \\});
+ \\export fn entry() void {
+ \\ var tagged = Tagged{ .signed = -1 };
+ \\ tagged = .{ .unsigned = 1 };
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:14:23: error: enum field not found: 'arst'",
+ "tmp.zig:2:20: note: enum declared here",
+ "tmp.zig:27:24: note: referenced here",
+ });
+
+ cases.add("@Type for tagged union with extra enum field",
+ \\const TypeInfo = @import("builtin").TypeInfo;
+ \\const Tag = @Type(.{
+ \\ .Enum = .{
+ \\ .layout = .Auto,
+ \\ .tag_type = u2,
+ \\ .fields = &[_]TypeInfo.EnumField{
+ \\ .{ .name = "signed", .value = 0 },
+ \\ .{ .name = "unsigned", .value = 1 },
+ \\ .{ .name = "arst", .field_type = 2 },
+ \\ },
+ \\ .decls = &[_]TypeInfo.Declaration{},
+ \\ .is_exhaustive = true,
+ \\ },
+ \\});
+ \\const Tagged = @Type(.{
+ \\ .Union = .{
+ \\ .layout = .Auto,
+ \\ .tag_type = Tag,
+ \\ .fields = &[_]TypeInfo.UnionField{
+ \\ .{ .name = "signed", .field_type = i32 },
+ \\ .{ .name = "unsigned", .field_type = u32 },
+ \\ },
+ \\ .decls = &[_]TypeInfo.Declaration{},
+ \\ },
+ \\});
+ \\export fn entry() void {
+ \\ var tagged = Tagged{ .signed = -1 };
+ \\ tagged = .{ .unsigned = 1 };
+ \\}
+ , &[_][]const u8{
+ "tmp.zig:9:32: error: no member named 'field_type' in struct 'std.builtin.EnumField'",
+ "tmp.zig:18:21: note: referenced here",
+ "tmp.zig:27:18: note: referenced here",
+ });
+
cases.add("@Type with undefined",
\\comptime {
\\ _ = @Type(.{ .Array = .{ .len = 0, .child = u8, .sentinel = undefined } });
@@ -7419,7 +7548,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
});
cases.add( // fixed bug #2032
- "compile diagnostic string for top level decl type",
+ "compile diagnostic string for top level decl type",
\\export fn entry() void {
\\ var foo: u32 = @This(){};
\\}