aboutsummaryrefslogtreecommitdiff
path: root/test/behavior/struct.zig
diff options
context:
space:
mode:
authormlugg <mlugg@mlugg.co.uk>2024-10-26 23:13:58 +0100
committermlugg <mlugg@mlugg.co.uk>2024-10-31 20:42:53 +0000
commitd11bbde5f9c64ef58405604601d55f88bb5d5f3a (patch)
tree4e05b679cd791e90db68f3f2198294667a6fc57c /test/behavior/struct.zig
parenta916bc7fdd3975a9e2ef13c44f814c71ce017193 (diff)
downloadzig-d11bbde5f9c64ef58405604601d55f88bb5d5f3a.tar.gz
zig-d11bbde5f9c64ef58405604601d55f88bb5d5f3a.zip
compiler: remove anonymous struct types, unify all tuples
This commit reworks how anonymous struct literals and tuples work. Previously, an untyped anonymous struct literal (e.g. `const x = .{ .a = 123 }`) was given an "anonymous struct type", which is a special kind of struct which coerces using structural equivalence. This mechanism was a holdover from before we used RLS / result types as the primary mechanism of type inference. This commit changes the language so that the type assigned here is a "normal" struct type. It uses a form of equivalence based on the AST node and the type's structure, much like a reified (`@Type`) type. Additionally, tuples have been simplified. The distinction between "simple" and "complex" tuple types is eliminated. All tuples, even those explicitly declared using `struct { ... }` syntax, use structural equivalence, and do not undergo staged type resolution. Tuples are very restricted: they cannot have non-`auto` layouts, cannot have aligned fields, and cannot have default values with the exception of `comptime` fields. Tuples currently do not have optimized layout, but this can be changed in the future. This change simplifies the language, and fixes some problematic coercions through pointers which led to unintuitive behavior. Resolves: #16865
Diffstat (limited to 'test/behavior/struct.zig')
-rw-r--r--test/behavior/struct.zig95
1 files changed, 17 insertions, 78 deletions
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);
+}