authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-07-16 23:35:31+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-07-17 00:15:34+03:00
loga1e78d0b0631885b95dfa80ab617818d1fd61277
treea73ef9c8e04c3a16dc690277ac21581999c2f669
parentcc3bceea3de67f24e6c17d4c04a34a2301e097f8
signaturelock-open Commit is signed but in an unrecognized format.

add is_tuple field to struct typeinfo

part of #4335

4 files changed, 16 insertions(+), 5 deletions(-)

lib/std/builtin.zig+1
......@@ -246,6 +246,7 @@ pub const TypeInfo = union(enum) {
246246 layout: ContainerLayout,
247247 fields: []const StructField,
248248 decls: []const Declaration,
249 is_tuple: bool,
249250 };
250251
251252 /// This data structure is used by the Zig language code generation and
lib/std/mem.zig+2-3
......@@ -709,8 +709,7 @@ pub fn zeroInit(comptime T: type, init: anytype) T {
709709 .Struct => |init_info| {
710710 var value = std.mem.zeroes(T);
711711
712 // typeInfo won't tell us if this is a tuple
713 if (comptime eql(u8, init_info.fields[0].name, "0")) {
712 if (init_info.is_tuple) {
714713 inline for (init_info.fields) |field, i| {
715714 @field(value, struct_info.fields[i].name) = @field(init, field.name);
716715 }
......@@ -785,7 +784,7 @@ test "zeroInit" {
785784 a: u8,
786785 };
787786
788 const c = zeroInit(Color, .{255, 255});
787 const c = zeroInit(Color, .{ 255, 255 });
789788 testing.expectEqual(Color{
790789 .r = 255,
791790 .g = 255,
src/ir.cpp+7-1
......@@ -25546,7 +25546,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
2554625546 result->special = ConstValSpecialStatic;
2554725547 result->type = ir_type_info_get_type(ira, "Struct", nullptr);
2554825548
25549 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 3);
25549 ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 4);
2555025550 result->data.x_struct.fields = fields;
2555125551
2555225552 // layout: ContainerLayout
......@@ -25627,6 +25627,12 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
2562725627 return err;
2562825628 }
2562925629
25630 // is_tuple: bool
25631 ensure_field_index(result->type, "is_tuple", 3);
25632 fields[3]->special = ConstValSpecialStatic;
25633 fields[3]->type = ira->codegen->builtin_types.entry_bool;
25634 fields[3]->data.x_bool = is_tuple(type_entry);
25635
2563025636 break;
2563125637 }
2563225638 case ZigTypeIdFn:
test/stage1/behavior/type_info.zig+6-1
......@@ -280,7 +280,7 @@ fn testFunction() void {
280280 expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct);
281281}
282282
283extern fn foo(a: usize, b: bool, args: ...) usize;
283extern fn foo(a: usize, b: bool, ...) usize;
284284
285285test "typeInfo with comptime parameter in struct fn def" {
286286 const S = struct {
......@@ -425,3 +425,8 @@ test "Declarations are returned in declaration order" {
425425 expect(std.mem.eql(u8, d[3].name, "d"));
426426 expect(std.mem.eql(u8, d[4].name, "e"));
427427}
428
429test "Struct.is_tuple" {
430 expect(@typeInfo(@TypeOf(.{0})).Struct.is_tuple);
431 expect(!@typeInfo(@TypeOf(.{ .a = 0 })).Struct.is_tuple);
432}