add is_tuple field to struct typeinfo

part of #4335
master
Vexu 2020-07-16 23:35:31 +03:00
parent cc3bceea3d
commit a1e78d0b06
No known key found for this signature in database
GPG Key ID: 59AEB8936E16A6AC
4 changed files with 16 additions and 5 deletions

View File

@ -246,6 +246,7 @@ pub const TypeInfo = union(enum) {
layout: ContainerLayout,
fields: []const StructField,
decls: []const Declaration,
is_tuple: bool,
};
/// This data structure is used by the Zig language code generation and

View File

@ -709,8 +709,7 @@ pub fn zeroInit(comptime T: type, init: anytype) T {
.Struct => |init_info| {
var value = std.mem.zeroes(T);
// typeInfo won't tell us if this is a tuple
if (comptime eql(u8, init_info.fields[0].name, "0")) {
if (init_info.is_tuple) {
inline for (init_info.fields) |field, i| {
@field(value, struct_info.fields[i].name) = @field(init, field.name);
}
@ -785,7 +784,7 @@ test "zeroInit" {
a: u8,
};
const c = zeroInit(Color, .{255, 255});
const c = zeroInit(Color, .{ 255, 255 });
testing.expectEqual(Color{
.r = 255,
.g = 255,

View File

@ -25546,7 +25546,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
result->special = ConstValSpecialStatic;
result->type = ir_type_info_get_type(ira, "Struct", nullptr);
ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 3);
ZigValue **fields = alloc_const_vals_ptrs(ira->codegen, 4);
result->data.x_struct.fields = fields;
// layout: ContainerLayout
@ -25627,6 +25627,12 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy
return err;
}
// is_tuple: bool
ensure_field_index(result->type, "is_tuple", 3);
fields[3]->special = ConstValSpecialStatic;
fields[3]->type = ira->codegen->builtin_types.entry_bool;
fields[3]->data.x_bool = is_tuple(type_entry);
break;
}
case ZigTypeIdFn:

View File

@ -280,7 +280,7 @@ fn testFunction() void {
expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct);
}
extern fn foo(a: usize, b: bool, args: ...) usize;
extern fn foo(a: usize, b: bool, ...) usize;
test "typeInfo with comptime parameter in struct fn def" {
const S = struct {
@ -425,3 +425,8 @@ test "Declarations are returned in declaration order" {
expect(std.mem.eql(u8, d[3].name, "d"));
expect(std.mem.eql(u8, d[4].name, "e"));
}
test "Struct.is_tuple" {
expect(@typeInfo(@TypeOf(.{0})).Struct.is_tuple);
expect(!@typeInfo(@TypeOf(.{ .a = 0 })).Struct.is_tuple);
}