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) {...@@ -246,6 +246,7 @@ pub const TypeInfo = union(enum) {
246 layout: ContainerLayout,246 layout: ContainerLayout,
247 fields: []const StructField,247 fields: []const StructField,
248 decls: []const Declaration,248 decls: []const Declaration,
249 is_tuple: bool,
249 };250 };
250251
251 /// This data structure is used by the Zig language code generation and252 /// 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 {...@@ -709,8 +709,7 @@ pub fn zeroInit(comptime T: type, init: anytype) T {
709 .Struct => |init_info| {709 .Struct => |init_info| {
710 var value = std.mem.zeroes(T);710 var value = std.mem.zeroes(T);
711711
712 // typeInfo won't tell us if this is a tuple712 if (init_info.is_tuple) {
713 if (comptime eql(u8, init_info.fields[0].name, "0")) {
714 inline for (init_info.fields) |field, i| {713 inline for (init_info.fields) |field, i| {
715 @field(value, struct_info.fields[i].name) = @field(init, field.name);714 @field(value, struct_info.fields[i].name) = @field(init, field.name);
716 }715 }
...@@ -785,7 +784,7 @@ test "zeroInit" {...@@ -785,7 +784,7 @@ test "zeroInit" {
785 a: u8,784 a: u8,
786 };785 };
787786
788 const c = zeroInit(Color, .{255, 255});787 const c = zeroInit(Color, .{ 255, 255 });
789 testing.expectEqual(Color{788 testing.expectEqual(Color{
790 .r = 255,789 .r = 255,
791 .g = 255,790 .g = 255,
src/ir.cpp+7-1
...@@ -25546,7 +25546,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -25546,7 +25546,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
25546 result->special = ConstValSpecialStatic;25546 result->special = ConstValSpecialStatic;
25547 result->type = ir_type_info_get_type(ira, "Struct", nullptr);25547 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);
25550 result->data.x_struct.fields = fields;25550 result->data.x_struct.fields = fields;
2555125551
25552 // layout: ContainerLayout25552 // layout: ContainerLayout
...@@ -25627,6 +25627,12 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy...@@ -25627,6 +25627,12 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
25627 return err;25627 return err;
25628 }25628 }
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
25630 break;25636 break;
25631 }25637 }
25632 case ZigTypeIdFn:25638 case ZigTypeIdFn:
test/stage1/behavior/type_info.zig+6-1
...@@ -280,7 +280,7 @@ fn testFunction() void {...@@ -280,7 +280,7 @@ fn testFunction() void {
280 expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct);280 expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct);
281}281}
282282
283extern fn foo(a: usize, b: bool, args: ...) usize;283extern fn foo(a: usize, b: bool, ...) usize;
284284
285test "typeInfo with comptime parameter in struct fn def" {285test "typeInfo with comptime parameter in struct fn def" {
286 const S = struct {286 const S = struct {
...@@ -425,3 +425,8 @@ test "Declarations are returned in declaration order" {...@@ -425,3 +425,8 @@ test "Declarations are returned in declaration order" {
425 expect(std.mem.eql(u8, d[3].name, "d"));425 expect(std.mem.eql(u8, d[3].name, "d"));
426 expect(std.mem.eql(u8, d[4].name, "e"));426 expect(std.mem.eql(u8, d[4].name, "e"));
427}427}
428
429test "Struct.is_tuple" {
430 expect(@typeInfo(@TypeOf(.{0})).Struct.is_tuple);
431 expect(!@typeInfo(@TypeOf(.{ .a = 0 })).Struct.is_tuple);
432}