From fc8791c133c14fe969b9056d8f0e337094ce1461 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sun, 6 Jun 2021 12:26:15 +0200 Subject: [PATCH] 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. --- src/stage1/ir.cpp | 2 +- test/behavior/tuple.zig | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index 3604f98ff55d4eb0b58609da28d13fb1a59e43f1..19242a5c42170472b8ec0e6f534cd673f1f636ab 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -16694,7 +16694,7 @@ static IrInstGen *ir_analyze_instruction_container_init_list(IrAnalyze *ira, { // We're now done inferring the type. container_type->data.structure.resolve_status = ResolveStatusUnstarted; - } else if (container_type->id == ZigTypeIdVector) { + } else if (container_type->id == ZigTypeIdVector || is_tuple(container_type)) { // OK } else { ir_add_error(ira, &instruction->base.base, diff --git a/test/behavior/tuple.zig b/test/behavior/tuple.zig index 0a32c664ddeabbd84826abf4b06c361b0f07f06b..29d44d582f160d28a17828f82be1f0cee231bf62 100644 --- a/test/behavior/tuple.zig +++ b/test/behavior/tuple.zig @@ -111,3 +111,39 @@ test "tuple initializer for var" { S.doTheTest(); comptime S.doTheTest(); } + +test "array-like initializer for tuple types" { + const T = @Type(std.builtin.TypeInfo{ + .Struct = std.builtin.TypeInfo.Struct{ + .is_tuple = true, + .layout = .Auto, + .decls = &[_]std.builtin.TypeInfo.Declaration{}, + .fields = &[_]std.builtin.TypeInfo.StructField{ + .{ + .name = "0", + .field_type = i32, + .default_value = @as(?i32, null), + .is_comptime = false, + .alignment = @alignOf(i32), + }, + .{ + .name = "1", + .field_type = u8, + .default_value = @as(?i32, null), + .is_comptime = false, + .alignment = @alignOf(i32), + }, + }, + }, + }); + const S = struct { + fn doTheTest() !void { + var obj: T = .{ -1234, 128 }; + try testing.expectEqual(@as(i32, -1234), obj[0]); + try testing.expectEqual(@as(u8, 128), obj[1]); + } + }; + + try S.doTheTest(); + comptime try S.doTheTest(); +} -- 2.54.0