From 77df5dae7f69f0d46c8e229df12f43f33487073f Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Sat, 26 Sep 2020 08:31:08 -0600 Subject: [PATCH 1/8] Make builtin.TypeInfo.Pointer.alignment u29 instead of comptime_int --- lib/std/builtin.zig | 2 +- src/stage1/ir.cpp | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index 52b8f641cd..cca47fbc01 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -214,7 +214,7 @@ pub const TypeInfo = union(enum) { size: Size, is_const: bool, is_volatile: bool, - alignment: comptime_int, + alignment: u29, child: type, is_allowzero: bool, diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index 7de4b923ba..51b9900b6e 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -25029,7 +25029,7 @@ static ZigValue *create_ptr_like_type_info(IrAnalyze *ira, IrInst *source_instr, fields[2]->data.x_bool = attrs_type->data.pointer.is_volatile; // alignment: u32 ensure_field_index(result->type, "alignment", 3); - fields[3]->type = ira->codegen->builtin_types.entry_num_lit_int; + fields[3]->type = ira->codegen->builtin_types.entry_u29; if (attrs_type->data.pointer.explicit_alignment != 0) { fields[3]->special = ConstValSpecialStatic; bigint_init_unsigned(&fields[3]->data.x_bigint, attrs_type->data.pointer.explicit_alignment); @@ -25740,6 +25740,17 @@ static Error get_const_field_bool(IrAnalyze *ira, AstNode *source_node, ZigValue return ErrorNone; } +static Error get_const_field_u29(IrAnalyze *ira, AstNode *source_node, ZigValue *struct_value, + const char *name, size_t field_index, uint32_t *out) +{ + ZigValue *value = get_const_field(ira, source_node, struct_value, name, field_index); + if (value == nullptr) + return ErrorSemanticAnalyzeFail; + assert(value->type == ira->codegen->builtin_types.entry_u29); + *out = bigint_as_u32(&value->data.x_bigint); + return ErrorNone; +} + static BigInt *get_const_field_lit_int(IrAnalyze *ira, AstNode *source_node, ZigValue *struct_value, const char *name, size_t field_index) { ZigValue *value = get_const_field(ira, source_node, struct_value, name, field_index); @@ -25868,8 +25879,9 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI buf_sprintf("sentinels are only allowed on slices and unknown-length pointers")); return ira->codegen->invalid_inst_gen->value->type; } - BigInt *bi = get_const_field_lit_int(ira, source_instr->source_node, payload, "alignment", 3); - if (bi == nullptr) + + uint32_t alignment; + if ((err = get_const_field_u29(ira, source_instr->source_node, payload, "alignment", 3, &alignment))) return ira->codegen->invalid_inst_gen->value->type; bool is_const; @@ -25896,7 +25908,7 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI is_const, is_volatile, ptr_len, - bigint_as_u32(bi), + alignment, 0, // bit_offset_in_host 0, // host_int_bytes is_allowzero, From d81648ce8ce93780c7eb8c93f05b6f99f160474c Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Sat, 26 Sep 2020 08:48:26 -0600 Subject: [PATCH 2/8] Add alignment field to TypeInfo.UnionField and TypeInfo.StructField Closes https://github.com/ziglang/zig/issues/6122 --- lib/std/builtin.zig | 2 ++ lib/std/meta/trailer_flags.zig | 1 + src/stage1/ir.cpp | 16 ++++++++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index cca47fbc01..857ecc391f 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -262,6 +262,7 @@ pub const TypeInfo = union(enum) { field_type: type, default_value: anytype, is_comptime: bool, + alignment: u29, }; /// This data structure is used by the Zig language code generation and @@ -318,6 +319,7 @@ pub const TypeInfo = union(enum) { pub const UnionField = struct { name: []const u8, field_type: type, + alignment: u29, }; /// This data structure is used by the Zig language code generation and diff --git a/lib/std/meta/trailer_flags.zig b/lib/std/meta/trailer_flags.zig index c8c1323686..6cd8dc9357 100644 --- a/lib/std/meta/trailer_flags.zig +++ b/lib/std/meta/trailer_flags.zig @@ -47,6 +47,7 @@ pub fn TrailerFlags(comptime Fields: type) type { @as(?struct_field.field_type, null), ), .is_comptime = false, + .alignment = @alignOf(?struct_field.field_type), }; } break :blk @Type(.{ diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index 51b9900b6e..5c58b0cb5a 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -25431,11 +25431,15 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy union_field_val->special = ConstValSpecialStatic; union_field_val->type = type_info_union_field_type; - ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 2); + ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 3); inner_fields[1]->special = ConstValSpecialStatic; inner_fields[1]->type = ira->codegen->builtin_types.entry_type; inner_fields[1]->data.x_type = union_field->type_entry; + inner_fields[2]->special = ConstValSpecialStatic; + inner_fields[2]->type = ira->codegen->builtin_types.entry_u29; + bigint_init_unsigned(&inner_fields[2]->data.x_bigint, union_field->align); + ZigValue *name = create_const_str_lit(ira->codegen, union_field->name)->data.x_ptr.data.ref.pointee; init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(union_field->name), true); @@ -25502,7 +25506,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy struct_field_val->special = ConstValSpecialStatic; struct_field_val->type = type_info_struct_field_type; - ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 4); + ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 5); inner_fields[1]->special = ConstValSpecialStatic; inner_fields[1]->type = ira->codegen->builtin_types.entry_type; @@ -25522,6 +25526,10 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy inner_fields[3]->type = ira->codegen->builtin_types.entry_bool; inner_fields[3]->data.x_bool = struct_field->is_comptime; + inner_fields[4]->special = ConstValSpecialStatic; + inner_fields[4]->type = ira->codegen->builtin_types.entry_u29; + bigint_init_unsigned(&inner_fields[4]->data.x_bigint, struct_field->align); + ZigValue *name = create_const_str_lit(ira->codegen, struct_field->name)->data.x_ptr.data.ref.pointee; init_const_slice(ira->codegen, inner_fields[0], name, 0, buf_len(struct_field->name), true); @@ -26145,6 +26153,8 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI } if ((err = get_const_field_bool(ira, source_instr->source_node, field_value, "is_comptime", 3, &field->is_comptime))) return ira->codegen->invalid_inst_gen->value->type; + if ((err = get_const_field_u29(ira, source_instr->source_node, field_value, "alignment", 4, &field->align))) + return ira->codegen->invalid_inst_gen->value->type; } return entry; @@ -26314,6 +26324,8 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI return ira->codegen->invalid_inst_gen->value->type; field->type_val = type_value; field->type_entry = type_value->data.x_type; + if ((err = get_const_field_u29(ira, source_instr->source_node, field_value, "alignment", 2, &field->align))) + return ira->codegen->invalid_inst_gen->value->type; } return entry; } From c2ee66108c399b0359ce996980bd8593c3eb22ef Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Sat, 26 Sep 2020 08:55:32 -0600 Subject: [PATCH 3/8] Add tests for alignment field in UnionField and StructFIeld --- test/stage1/behavior/type_info.zig | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/stage1/behavior/type_info.zig b/test/stage1/behavior/type_info.zig index 9e066d5f1a..f5b37fe3f0 100644 --- a/test/stage1/behavior/type_info.zig +++ b/test/stage1/behavior/type_info.zig @@ -211,7 +211,9 @@ fn testUnion() void { expect(notag_union_info.Union.tag_type == null); expect(notag_union_info.Union.layout == .Auto); expect(notag_union_info.Union.fields.len == 2); + expect(notag_union_info.Union.fields[0].alignment == @alignOf(void)); expect(notag_union_info.Union.fields[1].field_type == u32); + expect(notag_union_info.Union.fields[1].alignment == @alignOf(u32)); const TestExternUnion = extern union { foo: *c_void, @@ -229,13 +231,18 @@ test "type info: struct info" { } fn testStruct() void { + const unpacked_struct_info = @typeInfo(TestUnpackedStruct); + expect(unpacked_struct_info.Struct.fields[0].alignment == @alignOf(u32)); + const struct_info = @typeInfo(TestStruct); expect(struct_info == .Struct); expect(struct_info.Struct.layout == .Packed); expect(struct_info.Struct.fields.len == 4); + expect(struct_info.Struct.fields[0].alignment == 2 * @alignOf(usize)); expect(struct_info.Struct.fields[2].field_type == *TestStruct); expect(struct_info.Struct.fields[2].default_value == null); expect(struct_info.Struct.fields[3].default_value.? == 4); + expect(struct_info.Struct.fields[3].alignment == 1); expect(struct_info.Struct.decls.len == 2); expect(struct_info.Struct.decls[0].is_pub); expect(!struct_info.Struct.decls[0].data.Fn.is_extern); @@ -244,8 +251,12 @@ fn testStruct() void { expect(struct_info.Struct.decls[0].data.Fn.fn_type == fn (*const TestStruct) void); } +const TestUnpackedStruct = struct { + fieldA: u32 = 4, +}; + const TestStruct = packed struct { - fieldA: usize, + fieldA: usize align(2 * @alignOf(usize)), fieldB: void, fieldC: *Self, fieldD: u32 = 4, From 70c507911a738538b8ef4df2b52184512e7d86a8 Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Sat, 26 Sep 2020 09:55:38 -0600 Subject: [PATCH 4/8] Update @Type tests for alignment field in UnionField and StructFIeld --- test/stage1/behavior/type.zig | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/stage1/behavior/type.zig b/test/stage1/behavior/type.zig index 38d23175d0..1bb0823e74 100644 --- a/test/stage1/behavior/type.zig +++ b/test/stage1/behavior/type.zig @@ -320,8 +320,8 @@ test "Type.Union" { .layout = .Auto, .tag_type = null, .fields = &[_]TypeInfo.UnionField{ - .{ .name = "int", .field_type = i32 }, - .{ .name = "float", .field_type = f32 }, + .{ .name = "int", .field_type = i32, .alignment = @alignOf(f32) }, + .{ .name = "float", .field_type = f32, .alignment = @alignOf(f32) }, }, .decls = &[_]TypeInfo.Declaration{}, }, @@ -336,8 +336,8 @@ test "Type.Union" { .layout = .Packed, .tag_type = null, .fields = &[_]TypeInfo.UnionField{ - .{ .name = "signed", .field_type = i32 }, - .{ .name = "unsigned", .field_type = u32 }, + .{ .name = "signed", .field_type = i32, .alignment = @alignOf(i32) }, + .{ .name = "unsigned", .field_type = u32, .alignment = @alignOf(u32) }, }, .decls = &[_]TypeInfo.Declaration{}, }, @@ -363,8 +363,8 @@ test "Type.Union" { .layout = .Auto, .tag_type = Tag, .fields = &[_]TypeInfo.UnionField{ - .{ .name = "signed", .field_type = i32 }, - .{ .name = "unsigned", .field_type = u32 }, + .{ .name = "signed", .field_type = i32, .alignment = @alignOf(i32) }, + .{ .name = "unsigned", .field_type = u32, .alignment = @alignOf(u32) }, }, .decls = &[_]TypeInfo.Declaration{}, }, @@ -392,7 +392,7 @@ test "Type.Union from Type.Enum" { .layout = .Auto, .tag_type = Tag, .fields = &[_]TypeInfo.UnionField{ - .{ .name = "working_as_expected", .field_type = u32 }, + .{ .name = "working_as_expected", .field_type = u32, .alignment = @alignOf(u32) }, }, .decls = &[_]TypeInfo.Declaration{}, }, @@ -408,7 +408,7 @@ test "Type.Union from regular enum" { .layout = .Auto, .tag_type = E, .fields = &[_]TypeInfo.UnionField{ - .{ .name = "working_as_expected", .field_type = u32 }, + .{ .name = "working_as_expected", .field_type = u32, .alignment = @alignOf(u32) }, }, .decls = &[_]TypeInfo.Declaration{}, }, From a12203d2be2ff1021d8faa9b87c53af091f0bd01 Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Sat, 26 Sep 2020 14:42:43 -0600 Subject: [PATCH 5/8] Switch TypeInfo alignment fields from u29 to comptime_int --- lib/std/builtin.zig | 6 +++--- src/stage1/ir.cpp | 37 +++++++++++++++++-------------------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig index 857ecc391f..d80d0e88fe 100644 --- a/lib/std/builtin.zig +++ b/lib/std/builtin.zig @@ -214,7 +214,7 @@ pub const TypeInfo = union(enum) { size: Size, is_const: bool, is_volatile: bool, - alignment: u29, + alignment: comptime_int, child: type, is_allowzero: bool, @@ -262,7 +262,7 @@ pub const TypeInfo = union(enum) { field_type: type, default_value: anytype, is_comptime: bool, - alignment: u29, + alignment: comptime_int, }; /// This data structure is used by the Zig language code generation and @@ -319,7 +319,7 @@ pub const TypeInfo = union(enum) { pub const UnionField = struct { name: []const u8, field_type: type, - alignment: u29, + alignment: comptime_int, }; /// This data structure is used by the Zig language code generation and diff --git a/src/stage1/ir.cpp b/src/stage1/ir.cpp index 5c58b0cb5a..8da207cab7 100644 --- a/src/stage1/ir.cpp +++ b/src/stage1/ir.cpp @@ -25027,9 +25027,9 @@ static ZigValue *create_ptr_like_type_info(IrAnalyze *ira, IrInst *source_instr, fields[2]->special = ConstValSpecialStatic; fields[2]->type = ira->codegen->builtin_types.entry_bool; fields[2]->data.x_bool = attrs_type->data.pointer.is_volatile; - // alignment: u32 + // alignment: comptime_int ensure_field_index(result->type, "alignment", 3); - fields[3]->type = ira->codegen->builtin_types.entry_u29; + fields[3]->type = ira->codegen->builtin_types.entry_num_lit_int; if (attrs_type->data.pointer.explicit_alignment != 0) { fields[3]->special = ConstValSpecialStatic; bigint_init_unsigned(&fields[3]->data.x_bigint, attrs_type->data.pointer.explicit_alignment); @@ -25432,12 +25432,14 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy union_field_val->type = type_info_union_field_type; ZigValue **inner_fields = alloc_const_vals_ptrs(ira->codegen, 3); + // field_type: type inner_fields[1]->special = ConstValSpecialStatic; inner_fields[1]->type = ira->codegen->builtin_types.entry_type; inner_fields[1]->data.x_type = union_field->type_entry; + // alignment: comptime_int inner_fields[2]->special = ConstValSpecialStatic; - inner_fields[2]->type = ira->codegen->builtin_types.entry_u29; + inner_fields[2]->type = ira->codegen->builtin_types.entry_num_lit_int; bigint_init_unsigned(&inner_fields[2]->data.x_bigint, union_field->align); ZigValue *name = create_const_str_lit(ira->codegen, union_field->name)->data.x_ptr.data.ref.pointee; @@ -25522,12 +25524,14 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInst* source_instr, ZigTy } set_optional_payload(inner_fields[2], struct_field->init_val); + // is_comptime: bool inner_fields[3]->special = ConstValSpecialStatic; inner_fields[3]->type = ira->codegen->builtin_types.entry_bool; inner_fields[3]->data.x_bool = struct_field->is_comptime; + // alignment: comptime_int inner_fields[4]->special = ConstValSpecialStatic; - inner_fields[4]->type = ira->codegen->builtin_types.entry_u29; + inner_fields[4]->type = ira->codegen->builtin_types.entry_num_lit_int; bigint_init_unsigned(&inner_fields[4]->data.x_bigint, struct_field->align); ZigValue *name = create_const_str_lit(ira->codegen, struct_field->name)->data.x_ptr.data.ref.pointee; @@ -25748,17 +25752,6 @@ static Error get_const_field_bool(IrAnalyze *ira, AstNode *source_node, ZigValue return ErrorNone; } -static Error get_const_field_u29(IrAnalyze *ira, AstNode *source_node, ZigValue *struct_value, - const char *name, size_t field_index, uint32_t *out) -{ - ZigValue *value = get_const_field(ira, source_node, struct_value, name, field_index); - if (value == nullptr) - return ErrorSemanticAnalyzeFail; - assert(value->type == ira->codegen->builtin_types.entry_u29); - *out = bigint_as_u32(&value->data.x_bigint); - return ErrorNone; -} - static BigInt *get_const_field_lit_int(IrAnalyze *ira, AstNode *source_node, ZigValue *struct_value, const char *name, size_t field_index) { ZigValue *value = get_const_field(ira, source_node, struct_value, name, field_index); @@ -25888,8 +25881,8 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI return ira->codegen->invalid_inst_gen->value->type; } - uint32_t alignment; - if ((err = get_const_field_u29(ira, source_instr->source_node, payload, "alignment", 3, &alignment))) + BigInt *alignment = get_const_field_lit_int(ira, source_instr->source_node, payload, "alignment", 3); + if (alignment == nullptr) return ira->codegen->invalid_inst_gen->value->type; bool is_const; @@ -25916,7 +25909,7 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI is_const, is_volatile, ptr_len, - alignment, + bigint_as_u32(alignment), 0, // bit_offset_in_host 0, // host_int_bytes is_allowzero, @@ -26153,8 +26146,10 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI } if ((err = get_const_field_bool(ira, source_instr->source_node, field_value, "is_comptime", 3, &field->is_comptime))) return ira->codegen->invalid_inst_gen->value->type; - if ((err = get_const_field_u29(ira, source_instr->source_node, field_value, "alignment", 4, &field->align))) + BigInt *alignment = get_const_field_lit_int(ira, source_instr->source_node, field_value, "alignment", 4); + if (alignment == nullptr) return ira->codegen->invalid_inst_gen->value->type; + field->align = bigint_as_u32(alignment); } return entry; @@ -26324,8 +26319,10 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI return ira->codegen->invalid_inst_gen->value->type; field->type_val = type_value; field->type_entry = type_value->data.x_type; - if ((err = get_const_field_u29(ira, source_instr->source_node, field_value, "alignment", 2, &field->align))) + BigInt *alignment = get_const_field_lit_int(ira, source_instr->source_node, field_value, "alignment", 2); + if (alignment == nullptr) return ira->codegen->invalid_inst_gen->value->type; + field->align = bigint_as_u32(alignment); } return entry; } From e187ac09cbc23fedef40014f520b2e905033746b Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Sat, 26 Sep 2020 17:40:31 -0600 Subject: [PATCH 6/8] Update compile error tests for alignment in StructField/UnionField --- test/compile_errors.zig | 119 ++++++++++++++++++++-------------------- 1 file changed, 59 insertions(+), 60 deletions(-) diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 7c3fa544b6..fef0a64762 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -38,6 +38,61 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:2:20: error: TypeInfo.Enum.tag_type must be an integer type, not 'bool'", }); + cases.add("@Type for tagged union with extra enum field", + \\const TypeInfo = @import("builtin").TypeInfo; + \\const Tag = @Type(.{ + \\ .Enum = .{ + \\ .layout = .Auto, + \\ .tag_type = u2, + \\ .fields = &[_]TypeInfo.EnumField{ + \\ .{ .name = "signed", .value = 0 }, + \\ .{ .name = "unsigned", .value = 1 }, + \\ .{ .name = "arst", .value = 2 }, + \\ }, + \\ .decls = &[_]TypeInfo.Declaration{}, + \\ .is_exhaustive = true, + \\ }, + \\}); + \\const Tagged = @Type(.{ + \\ .Union = .{ + \\ .layout = .Auto, + \\ .tag_type = Tag, + \\ .fields = &[_]TypeInfo.UnionField{ + \\ .{ .name = "signed", .field_type = i32, .alignment = @alignOf(i32) }, + \\ .{ .name = "unsigned", .field_type = u32, .alignment = @alignOf(u32) }, + \\ }, + \\ .decls = &[_]TypeInfo.Declaration{}, + \\ }, + \\}); + \\export fn entry() void { + \\ var tagged = Tagged{ .signed = -1 }; + \\ tagged = .{ .unsigned = 1 }; + \\} + , &[_][]const u8{ + "tmp.zig:15:23: error: enum field missing: 'arst'", + "tmp.zig:27:24: note: referenced here", + }); + + cases.add("@Type for union with opaque field", + \\const TypeInfo = @import("builtin").TypeInfo; + \\const Untagged = @Type(.{ + \\ .Union = .{ + \\ .layout = .Auto, + \\ .tag_type = null, + \\ .fields = &[_]TypeInfo.UnionField{ + \\ .{ .name = "foo", .field_type = @Type(.Opaque), .alignment = 1 }, + \\ }, + \\ .decls = &[_]TypeInfo.Declaration{}, + \\ }, + \\}); + \\export fn entry() void { + \\ _ = Untagged{}; + \\} + , &[_][]const u8{ + "tmp.zig:2:25: error: opaque types have unknown size and therefore cannot be directly embedded in unions", + "tmp.zig:13:17: note: referenced here", + }); + cases.add("slice sentinel mismatch", \\export fn entry() void { \\ const x = @import("std").meta.Vector(3, f32){ 25, 75, 5, 0 }; @@ -54,26 +109,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:2:37: error: expected type '[:1]const u8', found '*const [2:2]u8'", }); - cases.add("@Type for union with opaque field", - \\const TypeInfo = @import("builtin").TypeInfo; - \\const Untagged = @Type(.{ - \\ .Union = .{ - \\ .layout = .Auto, - \\ .tag_type = null, - \\ .fields = &[_]TypeInfo.UnionField{ - \\ .{ .name = "foo", .field_type = @Type(.Opaque) }, - \\ }, - \\ .decls = &[_]TypeInfo.Declaration{}, - \\ }, - \\}); - \\export fn entry() void { - \\ _ = Untagged{}; - \\} - , &[_][]const u8{ - "tmp.zig:2:25: error: opaque types have unknown size and therefore cannot be directly embedded in unions", - "tmp.zig:13:17: note: referenced here", - }); - cases.add("@Type for union with zero fields", \\const TypeInfo = @import("builtin").TypeInfo; \\const Untagged = @Type(.{ @@ -130,9 +165,9 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { \\ .layout = .Auto, \\ .tag_type = Tag, \\ .fields = &[_]TypeInfo.UnionField{ - \\ .{ .name = "signed", .field_type = i32 }, - \\ .{ .name = "unsigned", .field_type = u32 }, - \\ .{ .name = "arst", .field_type = f32 }, + \\ .{ .name = "signed", .field_type = i32, .alignment = @alignOf(i32) }, + \\ .{ .name = "unsigned", .field_type = u32, .alignment = @alignOf(u32) }, + \\ .{ .name = "arst", .field_type = f32, .alignment = @alignOf(f32) }, \\ }, \\ .decls = &[_]TypeInfo.Declaration{}, \\ }, @@ -147,42 +182,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:27:24: note: referenced here", }); - cases.add("@Type for tagged union with extra enum field", - \\const TypeInfo = @import("builtin").TypeInfo; - \\const Tag = @Type(.{ - \\ .Enum = .{ - \\ .layout = .Auto, - \\ .tag_type = u2, - \\ .fields = &[_]TypeInfo.EnumField{ - \\ .{ .name = "signed", .value = 0 }, - \\ .{ .name = "unsigned", .value = 1 }, - \\ .{ .name = "arst", .field_type = 2 }, - \\ }, - \\ .decls = &[_]TypeInfo.Declaration{}, - \\ .is_exhaustive = true, - \\ }, - \\}); - \\const Tagged = @Type(.{ - \\ .Union = .{ - \\ .layout = .Auto, - \\ .tag_type = Tag, - \\ .fields = &[_]TypeInfo.UnionField{ - \\ .{ .name = "signed", .field_type = i32 }, - \\ .{ .name = "unsigned", .field_type = u32 }, - \\ }, - \\ .decls = &[_]TypeInfo.Declaration{}, - \\ }, - \\}); - \\export fn entry() void { - \\ var tagged = Tagged{ .signed = -1 }; - \\ tagged = .{ .unsigned = 1 }; - \\} - , &[_][]const u8{ - "tmp.zig:9:32: error: no member named 'field_type' in struct 'std.builtin.EnumField'", - "tmp.zig:18:21: note: referenced here", - "tmp.zig:27:18: note: referenced here", - }); - cases.add("@Type with undefined", \\comptime { \\ _ = @Type(.{ .Array = .{ .len = 0, .child = u8, .sentinel = undefined } }); @@ -7592,7 +7591,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { }); cases.add( // fixed bug #2032 - "compile diagnostic string for top level decl type", + "compile diagnostic string for top level decl type", \\export fn entry() void { \\ var foo: u32 = @This(){}; \\} From ec8f0777f23e4585b927c8d95994947b2b263051 Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Tue, 29 Sep 2020 10:37:24 -0600 Subject: [PATCH 7/8] Update std.meta.Tuple for alignment in StructField/UnionField --- lib/std/meta.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 492e497ff4..8f40d70fed 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -884,6 +884,7 @@ pub fn Tuple(comptime types: []const type) type { .field_type = T, .default_value = @as(?T, null), .is_comptime = false, + .alignment = @alignOf(T), }; } From 362c87f1aab1db7f0019130115f7cadfef782a56 Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Tue, 29 Sep 2020 13:50:43 -0600 Subject: [PATCH 8/8] Update std.meta.ArgsTuple for alignment in StructField/UnionField --- lib/std/meta.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 8f40d70fed..79b0424e96 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -854,6 +854,7 @@ pub fn ArgsTuple(comptime Function: type) type { .field_type = arg.arg_type.?, .default_value = @as(?(arg.arg_type.?), null), .is_comptime = false, + .alignment = @alignOf(arg.arg_type.?), }; }