authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2021-06-06 12:26:15+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-06 21:21:42-04:00
logfc8791c133c14fe969b9056d8f0e337094ce1461
tree2748fac999f47ea06ba9a892ae3734ca69ace042
parentb87c3d5371a277a60e63dc521a44d488f013dbc8

stage1: Allow array-like initialization for tuple types

This small change makes working with tuple types much easier, allowing the use of anonymous (eg. obtained with meta.ArgsTuple) tuples in more places without the need for specifying each (quoted!) field name in the initializer.

2 files changed, 37 insertions(+), 1 deletions(-)

src/stage1/ir.cpp+1-1
......@@ -16694,7 +16694,7 @@ static IrInstGen *ir_analyze_instruction_container_init_list(IrAnalyze *ira,
1669416694 {
1669516695 // We're now done inferring the type.
1669616696 container_type->data.structure.resolve_status = ResolveStatusUnstarted;
16697 } else if (container_type->id == ZigTypeIdVector) {
16697 } else if (container_type->id == ZigTypeIdVector || is_tuple(container_type)) {
1669816698 // OK
1669916699 } else {
1670016700 ir_add_error(ira, &instruction->base.base,
test/behavior/tuple.zig+36
......@@ -111,3 +111,39 @@ test "tuple initializer for var" {
111111 S.doTheTest();
112112 comptime S.doTheTest();
113113}
114
115test "array-like initializer for tuple types" {
116 const T = @Type(std.builtin.TypeInfo{
117 .Struct = std.builtin.TypeInfo.Struct{
118 .is_tuple = true,
119 .layout = .Auto,
120 .decls = &[_]std.builtin.TypeInfo.Declaration{},
121 .fields = &[_]std.builtin.TypeInfo.StructField{
122 .{
123 .name = "0",
124 .field_type = i32,
125 .default_value = @as(?i32, null),
126 .is_comptime = false,
127 .alignment = @alignOf(i32),
128 },
129 .{
130 .name = "1",
131 .field_type = u8,
132 .default_value = @as(?i32, null),
133 .is_comptime = false,
134 .alignment = @alignOf(i32),
135 },
136 },
137 },
138 });
139 const S = struct {
140 fn doTheTest() !void {
141 var obj: T = .{ -1234, 128 };
142 try testing.expectEqual(@as(i32, -1234), obj[0]);
143 try testing.expectEqual(@as(u8, 128), obj[1]);
144 }
145 };
146
147 try S.doTheTest();
148 comptime try S.doTheTest();
149}