mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
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
59 lines
2.0 KiB
Zig
59 lines
2.0 KiB
Zig
const std = @import("std");
|
|
const builtin = @import("builtin");
|
|
const testing = std.testing;
|
|
const expect = testing.expect;
|
|
const expectEqualStrings = testing.expectEqualStrings;
|
|
|
|
test "tuple declaration type info" {
|
|
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
|
|
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
|
|
|
|
{
|
|
const T = struct { comptime u32 = 1, []const u8 };
|
|
const info = @typeInfo(T).@"struct";
|
|
|
|
try expect(info.layout == .auto);
|
|
try expect(info.backing_integer == null);
|
|
try expect(info.fields.len == 2);
|
|
try expect(info.decls.len == 0);
|
|
try expect(info.is_tuple);
|
|
|
|
try expectEqualStrings(info.fields[0].name, "0");
|
|
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 == @alignOf(u32));
|
|
|
|
try expectEqualStrings(info.fields[1].name, "1");
|
|
try expect(info.fields[1].type == []const u8);
|
|
try expect(info.fields[1].default_value == null);
|
|
try expect(!info.fields[1].is_comptime);
|
|
try expect(info.fields[1].alignment == @alignOf([]const u8));
|
|
}
|
|
}
|
|
|
|
test "tuple declaration usage" {
|
|
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
|
|
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
|
|
|
|
const T = struct { u32, []const u8 };
|
|
var t: T = .{ 1, "foo" };
|
|
_ = &t;
|
|
try expect(t[0] == 1);
|
|
try expectEqualStrings(t[1], "foo");
|
|
|
|
const mul = t ** 3;
|
|
try expect(@TypeOf(mul) != T);
|
|
try expect(mul.len == 6);
|
|
try expect(mul[2] == 1);
|
|
try expectEqualStrings(mul[3], "foo");
|
|
|
|
var t2: T = .{ 2, "bar" };
|
|
_ = &t2;
|
|
const cat = t ++ t2;
|
|
try expect(@TypeOf(cat) != T);
|
|
try expect(cat.len == 4);
|
|
try expect(cat[2] == 2);
|
|
try expectEqualStrings(cat[3], "bar");
|
|
}
|