From d11bbde5f9c64ef58405604601d55f88bb5d5f3a Mon Sep 17 00:00:00 2001 From: mlugg Date: Sat, 26 Oct 2024 23:13:58 +0100 Subject: 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 --- test/behavior/array.zig | 36 +----------------------------------- 1 file changed, 1 insertion(+), 35 deletions(-) (limited to 'test/behavior/array.zig') 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); -- cgit v1.2.3