diff options
| author | Matthew Lugg <mlugg@mlugg.co.uk> | 2024-11-01 01:43:08 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-01 01:43:08 +0000 |
| commit | 3f7fac5fff9beed535a7674679a5e2c1f3cd74d2 (patch) | |
| tree | c5f7affd52d47632874da1f533c888ef648e8304 /test/behavior | |
| parent | a916bc7fdd3975a9e2ef13c44f814c71ce017193 (diff) | |
| parent | 24babde746621492c5111ffcd8edf575cb176d65 (diff) | |
| download | zig-3f7fac5fff9beed535a7674679a5e2c1f3cd74d2.tar.gz zig-3f7fac5fff9beed535a7674679a5e2c1f3cd74d2.zip | |
Merge pull request #21817 from mlugg/no-anon-structs
compiler: remove anonymous struct types, unify all tuples
Diffstat (limited to 'test/behavior')
| -rw-r--r-- | test/behavior/array.zig | 36 | ||||
| -rw-r--r-- | test/behavior/cast.zig | 26 | ||||
| -rw-r--r-- | test/behavior/empty_file_level_struct.zig | 1 | ||||
| -rw-r--r-- | test/behavior/empty_file_level_union.zig | 1 | ||||
| -rw-r--r-- | test/behavior/empty_tuple_fields.zig | 28 | ||||
| -rw-r--r-- | test/behavior/struct.zig | 95 | ||||
| -rw-r--r-- | test/behavior/tuple.zig | 34 | ||||
| -rw-r--r-- | test/behavior/tuple_declarations.zig | 6 | ||||
| -rw-r--r-- | test/behavior/union.zig | 70 |
9 files changed, 44 insertions, 253 deletions
diff --git a/test/behavior/array.zig b/test/behavior/array.zig index 17b8667238..adcbe49eeb 100644 --- a/test/behavior/array.zig +++ b/test/behavior/array.zig @@ -596,7 +596,7 @@ test "type coercion of anon struct literal to array" { var x2: U = .{ .a = 42 }; _ = &x2; - const t2 = .{ x2, .{ .b = true }, .{ .c = "hello" } }; + const t2 = .{ x2, U{ .b = true }, U{ .c = "hello" } }; const arr2: [3]U = t2; try expect(arr2[0].a == 42); try expect(arr2[1].b == true); @@ -607,40 +607,6 @@ test "type coercion of anon struct literal to array" { try comptime S.doTheTest(); } -test "type coercion of pointer to anon struct literal to pointer to array" { - if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - - const S = struct { - const U = union { - a: u32, - b: bool, - c: []const u8, - }; - - fn doTheTest() !void { - var x1: u8 = 42; - _ = &x1; - const t1 = &.{ x1, 56, 54 }; - const arr1: *const [3]u8 = t1; - try expect(arr1[0] == 42); - try expect(arr1[1] == 56); - try expect(arr1[2] == 54); - - var x2: U = .{ .a = 42 }; - _ = &x2; - const t2 = &.{ x2, .{ .b = true }, .{ .c = "hello" } }; - const arr2: *const [3]U = t2; - try expect(arr2[0].a == 42); - try expect(arr2[1].b == true); - try expect(mem.eql(u8, arr2[2].c, "hello")); - } - }; - try S.doTheTest(); - try comptime S.doTheTest(); -} - test "array with comptime-only element type" { const a = [_]type{ u32, i32 }; try testing.expect(a[0] == u32); diff --git a/test/behavior/cast.zig b/test/behavior/cast.zig index 5d88564083..efc7d4237f 100644 --- a/test/behavior/cast.zig +++ b/test/behavior/cast.zig @@ -2600,32 +2600,6 @@ test "result type is preserved into comptime block" { try expect(x == 123); } -test "implicit cast from ptr to tuple to ptr to struct" { - if (builtin.zig_backend == .stage2_x86) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - - const ComptimeReason = union(enum) { - c_import: struct { - a: u32, - }, - }; - - const Block = struct { - reason: ?*const ComptimeReason, - }; - - var a: u32 = 16; - _ = &a; - var reason = .{ .c_import = .{ .a = a } }; - var block = Block{ - .reason = &reason, - }; - _ = █ - try expect(block.reason.?.c_import.a == 16); -} - test "bitcast vector" { if (builtin.zig_backend == .stage2_x86) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO diff --git a/test/behavior/empty_file_level_struct.zig b/test/behavior/empty_file_level_struct.zig deleted file mode 100644 index 86f0f2b3c7..0000000000 --- a/test/behavior/empty_file_level_struct.zig +++ /dev/null @@ -1 +0,0 @@ -struct {} diff --git a/test/behavior/empty_file_level_union.zig b/test/behavior/empty_file_level_union.zig deleted file mode 100644 index 0d24797ffb..0000000000 --- a/test/behavior/empty_file_level_union.zig +++ /dev/null @@ -1 +0,0 @@ -union {} diff --git a/test/behavior/empty_tuple_fields.zig b/test/behavior/empty_tuple_fields.zig deleted file mode 100644 index dc809b0355..0000000000 --- a/test/behavior/empty_tuple_fields.zig +++ /dev/null @@ -1,28 +0,0 @@ -const std = @import("std"); -const builtin = @import("builtin"); - -test "empty file level struct" { - if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - - const T = @import("empty_file_level_struct.zig"); - const info = @typeInfo(T); - try std.testing.expectEqual(@as(usize, 1), info.@"struct".fields.len); - try std.testing.expectEqualStrings("0", info.@"struct".fields[0].name); - try std.testing.expect(@typeInfo(info.@"struct".fields[0].type) == .@"struct"); -} - -test "empty file level union" { - if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; - if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - - const T = @import("empty_file_level_union.zig"); - const info = @typeInfo(T); - try std.testing.expectEqual(@as(usize, 1), info.@"struct".fields.len); - try std.testing.expectEqualStrings("0", info.@"struct".fields[0].name); - try std.testing.expect(@typeInfo(info.@"struct".fields[0].type) == .@"union"); -} diff --git a/test/behavior/struct.zig b/test/behavior/struct.zig index cc373cd8b1..283c8dbc4a 100644 --- a/test/behavior/struct.zig +++ b/test/behavior/struct.zig @@ -1013,84 +1013,6 @@ test "struct with 0-length union array field" { try expectEqual(@as(usize, 0), s.zero_length.len); } -test "type coercion of anon struct literal to struct" { - if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; - - const S = struct { - const S2 = struct { - A: u32, - B: []const u8, - C: void, - D: Foo = .{}, - }; - - const Foo = struct { - field: i32 = 1234, - }; - - fn doTheTest() !void { - var y: u32 = 42; - _ = &y; - const t0 = .{ .A = 123, .B = "foo", .C = {} }; - const t1 = .{ .A = y, .B = "foo", .C = {} }; - const y0: S2 = t0; - const y1: S2 = t1; - try expect(y0.A == 123); - try expect(std.mem.eql(u8, y0.B, "foo")); - try expect(y0.C == {}); - try expect(y0.D.field == 1234); - try expect(y1.A == y); - try expect(std.mem.eql(u8, y1.B, "foo")); - try expect(y1.C == {}); - try expect(y1.D.field == 1234); - } - }; - try S.doTheTest(); - try comptime S.doTheTest(); -} - -test "type coercion of pointer to anon struct literal to pointer to struct" { - if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; - - const S = struct { - const S2 = struct { - A: u32, - B: []const u8, - C: void, - D: Foo = .{}, - }; - - const Foo = struct { - field: i32 = 1234, - }; - - fn doTheTest() !void { - var y: u32 = 42; - _ = &y; - const t0 = &.{ .A = 123, .B = "foo", .C = {} }; - const t1 = &.{ .A = y, .B = "foo", .C = {} }; - const y0: *const S2 = t0; - const y1: *const S2 = t1; - try expect(y0.A == 123); - try expect(std.mem.eql(u8, y0.B, "foo")); - try expect(y0.C == {}); - try expect(y0.D.field == 1234); - try expect(y1.A == y); - try expect(std.mem.eql(u8, y1.B, "foo")); - try expect(y1.C == {}); - try expect(y1.D.field == 1234); - } - }; - try S.doTheTest(); - try comptime S.doTheTest(); -} - test "packed struct with undefined initializers" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO @@ -2183,3 +2105,20 @@ test "extern struct @FieldType" { comptime assert(@FieldType(S, "b") == f64); comptime assert(@FieldType(S, "c") == *S); } + +test "anonymous struct equivalence" { + const S = struct { + fn anonStructType(comptime x: anytype) type { + const val = .{ .a = "hello", .b = x }; + return @TypeOf(val); + } + }; + + const A = S.anonStructType(123); + const B = S.anonStructType(123); + const C = S.anonStructType(456); + + comptime assert(A == B); + comptime assert(A != C); + comptime assert(B != C); +} diff --git a/test/behavior/tuple.zig b/test/behavior/tuple.zig index a511b1c1b3..becfcce028 100644 --- a/test/behavior/tuple.zig +++ b/test/behavior/tuple.zig @@ -150,7 +150,7 @@ test "array-like initializer for tuple types" { .type = u8, .default_value = null, .is_comptime = false, - .alignment = @alignOf(i32), + .alignment = @alignOf(u8), }, }, }, @@ -566,16 +566,28 @@ test "comptime fields in tuple can be initialized" { _ = &a; } -test "tuple default values" { - const T = struct { - usize, - usize = 123, - usize = 456, - }; +test "empty struct in tuple" { + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - const t: T = .{1}; + const T = struct { struct {} }; + const info = @typeInfo(T); + try std.testing.expectEqual(@as(usize, 1), info.@"struct".fields.len); + try std.testing.expectEqualStrings("0", info.@"struct".fields[0].name); + try std.testing.expect(@typeInfo(info.@"struct".fields[0].type) == .@"struct"); +} + +test "empty union in tuple" { + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - try expectEqual(1, t[0]); - try expectEqual(123, t[1]); - try expectEqual(456, t[2]); + const T = struct { union {} }; + const info = @typeInfo(T); + try std.testing.expectEqual(@as(usize, 1), info.@"struct".fields.len); + try std.testing.expectEqualStrings("0", info.@"struct".fields[0].name); + try std.testing.expect(@typeInfo(info.@"struct".fields[0].type) == .@"union"); } diff --git a/test/behavior/tuple_declarations.zig b/test/behavior/tuple_declarations.zig index cf113c0201..907b114aeb 100644 --- a/test/behavior/tuple_declarations.zig +++ b/test/behavior/tuple_declarations.zig @@ -9,7 +9,7 @@ test "tuple declaration type info" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; { - const T = struct { comptime u32 align(2) = 1, []const u8 }; + const T = struct { comptime u32 = 1, []const u8 }; const info = @typeInfo(T).@"struct"; try expect(info.layout == .auto); @@ -22,7 +22,7 @@ test "tuple declaration type info" { try expect(info.fields[0].type == u32); try expect(@as(*const u32, @ptrCast(@alignCast(info.fields[0].default_value))).* == 1); try expect(info.fields[0].is_comptime); - try expect(info.fields[0].alignment == 2); + try expect(info.fields[0].alignment == @alignOf(u32)); try expectEqualStrings(info.fields[1].name, "1"); try expect(info.fields[1].type == []const u8); @@ -32,7 +32,7 @@ test "tuple declaration type info" { } } -test "Tuple declaration usage" { +test "tuple declaration usage" { if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; diff --git a/test/behavior/union.zig b/test/behavior/union.zig index 16ccfdd451..5acb1b5abc 100644 --- a/test/behavior/union.zig +++ b/test/behavior/union.zig @@ -986,76 +986,6 @@ test "function call result coerces from tagged union to the tag" { try comptime S.doTheTest(); } -test "cast from anonymous struct to union" { - if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - - const S = struct { - const U = union(enum) { - A: u32, - B: []const u8, - C: void, - }; - fn doTheTest() !void { - var y: u32 = 42; - _ = &y; - const t0 = .{ .A = 123 }; - const t1 = .{ .B = "foo" }; - const t2 = .{ .C = {} }; - const t3 = .{ .A = y }; - const x0: U = t0; - var x1: U = t1; - _ = &x1; - const x2: U = t2; - var x3: U = t3; - _ = &x3; - try expect(x0.A == 123); - try expect(std.mem.eql(u8, x1.B, "foo")); - try expect(x2 == .C); - try expect(x3.A == y); - } - }; - try S.doTheTest(); - try comptime S.doTheTest(); -} - -test "cast from pointer to anonymous struct to pointer to union" { - if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO - if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest; - - const S = struct { - const U = union(enum) { - A: u32, - B: []const u8, - C: void, - }; - fn doTheTest() !void { - var y: u32 = 42; - _ = &y; - const t0 = &.{ .A = 123 }; - const t1 = &.{ .B = "foo" }; - const t2 = &.{ .C = {} }; - const t3 = &.{ .A = y }; - const x0: *const U = t0; - var x1: *const U = t1; - _ = &x1; - const x2: *const U = t2; - var x3: *const U = t3; - _ = &x3; - try expect(x0.A == 123); - try expect(std.mem.eql(u8, x1.B, "foo")); - try expect(x2.* == .C); - try expect(x3.A == y); - } - }; - try S.doTheTest(); - try comptime S.doTheTest(); -} - test "switching on non exhaustive union" { if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO |
