authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-25 11:31:38-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-25 11:31:38-04:00
logc61e0a078cea2a6845ed7b03189409829d2cdf24
tree8a64733d2ecf5121dbdf0db998f4d69a4e5629e6
parent3021e5ca67acca6cf7420bc5e2400aa6965596f9
signature Commit is signed but in an unrecognized format.

fix union init with void payload

all std lib tests passing now

6 files changed, 44 insertions(+), 35 deletions(-)

src/analyze.cpp+3-11
......@@ -5001,12 +5001,9 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
50015001 field_val->type = wanted_type->data.structure.fields[i].type_entry;
50025002 assert(field_val->type);
50035003 init_const_undefined(g, field_val);
5004 ConstParent *parent = get_const_val_parent(g, field_val);
5005 if (parent != nullptr) {
5006 parent->id = ConstParentIdStruct;
5007 parent->data.p_struct.struct_val = const_val;
5008 parent->data.p_struct.field_index = i;
5009 }
5004 field_val->parent.id = ConstParentIdStruct;
5005 field_val->parent.data.p_struct.struct_val = const_val;
5006 field_val->parent.data.p_struct.field_index = i;
50105007 }
50115008 } else {
50125009 const_val->special = ConstValSpecialUndef;
......@@ -5842,11 +5839,6 @@ void expand_undef_array(CodeGen *g, ConstExprValue *const_val) {
58425839 zig_unreachable();
58435840}
58445841
5845// Deprecated. Reference the parent field directly.
5846ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value) {
5847 return &value->parent;
5848}
5849
58505842static const ZigTypeId all_type_ids[] = {
58515843 ZigTypeIdMetaType,
58525844 ZigTypeIdVoid,
src/analyze.hpp-1
......@@ -180,7 +180,6 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val);
180180ConstExprValue *create_const_vals(size_t count);
181181
182182ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);
183ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value);
184183void expand_undef_array(CodeGen *g, ConstExprValue *const_val);
185184void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);
186185
src/codegen.cpp+13-1
......@@ -3873,8 +3873,20 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
38733873
38743874 TypeUnionField *field = instruction->field;
38753875
3876 if (!type_has_bits(field->type_entry))
3876 if (!type_has_bits(field->type_entry)) {
3877 if (union_type->data.unionation.gen_tag_index == SIZE_MAX) {
3878 return nullptr;
3879 }
3880 if (instruction->initializing) {
3881 LLVMValueRef union_ptr = ir_llvm_value(g, instruction->union_ptr);
3882 LLVMValueRef tag_field_ptr = LLVMBuildStructGEP(g->builder, union_ptr,
3883 union_type->data.unionation.gen_tag_index, "");
3884 LLVMValueRef tag_value = bigint_to_llvm_const(get_llvm_type(g, union_type->data.unionation.tag_type),
3885 &field->enum_field->value);
3886 gen_store_untyped(g, tag_value, tag_field_ptr, 0, false);
3887 }
38773888 return nullptr;
3889 }
38783890
38793891 LLVMValueRef union_ptr = ir_llvm_value(g, instruction->union_ptr);
38803892 LLVMTypeRef field_type_ref = LLVMPointerType(get_llvm_type(g, field->type_entry), 0);
src/ir.cpp+7-11
......@@ -17425,11 +17425,9 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction
1742517425 ConstExprValue *field_val = &struct_val->data.x_struct.fields[i];
1742617426 field_val->special = ConstValSpecialUndef;
1742717427 field_val->type = struct_type->data.structure.fields[i].type_entry;
17428 ConstParent *parent = get_const_val_parent(ira->codegen, field_val);
17429 assert(parent != nullptr);
17430 parent->id = ConstParentIdStruct;
17431 parent->data.p_struct.struct_val = struct_val;
17432 parent->data.p_struct.field_index = i;
17428 field_val->parent.id = ConstParentIdStruct;
17429 field_val->parent.data.p_struct.struct_val = struct_val;
17430 field_val->parent.data.p_struct.field_index = i;
1743317431 }
1743417432 }
1743517433 IrInstruction *result;
......@@ -17507,11 +17505,8 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1750717505 ConstExprValue *payload_val = create_const_vals(1);
1750817506 payload_val->special = ConstValSpecialUndef;
1750917507 payload_val->type = field->type_entry;
17510 ConstParent *parent = get_const_val_parent(ira->codegen, payload_val);
17511 if (parent != nullptr) {
17512 parent->id = ConstParentIdUnion;
17513 parent->data.p_union.union_val = union_val;
17514 }
17508 payload_val->parent.id = ConstParentIdUnion;
17509 payload_val->parent.data.p_union.union_val = union_val;
1751517510
1751617511 union_val->special = ConstValSpecialStatic;
1751717512 bigint_init_bigint(&union_val->data.x_union.tag, &field->enum_field->value);
......@@ -25289,7 +25284,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2528925284 case IrInstructionIdReturnPtr:
2529025285 case IrInstructionIdTypeOf:
2529125286 case IrInstructionIdStructFieldPtr:
25292 case IrInstructionIdUnionFieldPtr:
2529325287 case IrInstructionIdArrayType:
2529425288 case IrInstructionIdPromiseType:
2529525289 case IrInstructionIdSliceType:
......@@ -25389,6 +25383,8 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2538925383 }
2539025384 case IrInstructionIdUnwrapErrCode:
2539125385 return reinterpret_cast<IrInstructionUnwrapErrCode *>(instruction)->initializing;
25386 case IrInstructionIdUnionFieldPtr:
25387 return reinterpret_cast<IrInstructionUnionFieldPtr *>(instruction)->initializing;
2539225388 case IrInstructionIdErrWrapPayload:
2539325389 return reinterpret_cast<IrInstructionErrWrapPayload *>(instruction)->result_loc != nullptr;
2539425390 case IrInstructionIdErrWrapCode:
std/zig/parser_test.zig+1-11
......@@ -1,4 +1,4 @@
1// TODO remove `use` keyword eventually
1// TODO remove `use` keyword eventually: https://github.com/ziglang/zig/issues/2591
22test "zig fmt: change use to usingnamespace" {
33 try testTransform(
44 \\use @import("std");
......@@ -105,7 +105,6 @@ test "zig fmt: linksection" {
105105}
106106
107107test "zig fmt: correctly move doc comments on struct fields" {
108 if (true) return error.SkipZigTest; // TODO
109108 try testTransform(
110109 \\pub const section_64 = extern struct {
111110 \\ sectname: [16]u8, /// name of this section
......@@ -917,7 +916,6 @@ test "zig fmt: statements with empty line between" {
917916}
918917
919918test "zig fmt: ptr deref operator and unwrap optional operator" {
920 if (true) return error.SkipZigTest; // TODO
921919 try testCanonical(
922920 \\const a = b.*;
923921 \\const a = b.?;
......@@ -1020,7 +1018,6 @@ test "zig fmt: same-line comment after a statement" {
10201018}
10211019
10221020test "zig fmt: same-line comment after var decl in struct" {
1023 if (true) return error.SkipZigTest; // TODO
10241021 try testCanonical(
10251022 \\pub const vfs_cap_data = extern struct {
10261023 \\ const Data = struct {}; // when on disk.
......@@ -1030,7 +1027,6 @@ test "zig fmt: same-line comment after var decl in struct" {
10301027}
10311028
10321029test "zig fmt: same-line comment after field decl" {
1033 if (true) return error.SkipZigTest; // TODO
10341030 try testCanonical(
10351031 \\pub const dirent = extern struct {
10361032 \\ d_name: u8,
......@@ -1106,7 +1102,6 @@ test "zig fmt: line comments in struct initializer" {
11061102}
11071103
11081104test "zig fmt: first line comment in struct initializer" {
1109 if (true) return error.SkipZigTest; // TODO
11101105 try testCanonical(
11111106 \\pub async fn acquire(self: *Self) HeldLock {
11121107 \\ return HeldLock{
......@@ -1120,7 +1115,6 @@ test "zig fmt: first line comment in struct initializer" {
11201115}
11211116
11221117test "zig fmt: doc comments before struct field" {
1123 if (true) return error.SkipZigTest; // TODO
11241118 try testCanonical(
11251119 \\pub const Allocator = struct {
11261120 \\ /// Allocate byte_count bytes and return them in a slice, with the
......@@ -1218,7 +1212,6 @@ test "zig fmt: comments before switch prong" {
12181212}
12191213
12201214test "zig fmt: comments before var decl in struct" {
1221 if (true) return error.SkipZigTest; // TODO
12221215 try testCanonical(
12231216 \\pub const vfs_cap_data = extern struct {
12241217 \\ // All of these are mandated as little endian
......@@ -1609,7 +1602,6 @@ test "zig fmt: indexing" {
16091602}
16101603
16111604test "zig fmt: struct declaration" {
1612 if (true) return error.SkipZigTest; // TODO
16131605 try testCanonical(
16141606 \\const S = struct {
16151607 \\ const Self = @This();
......@@ -1641,7 +1633,6 @@ test "zig fmt: struct declaration" {
16411633}
16421634
16431635test "zig fmt: enum declaration" {
1644 if (true) return error.SkipZigTest; // TODO
16451636 try testCanonical(
16461637 \\const E = enum {
16471638 \\ Ok,
......@@ -1670,7 +1661,6 @@ test "zig fmt: enum declaration" {
16701661}
16711662
16721663test "zig fmt: union declaration" {
1673 if (true) return error.SkipZigTest; // TODO
16741664 try testCanonical(
16751665 \\const U = union {
16761666 \\ Int: u8,
test/stage1/behavior/union.zig+20
......@@ -402,3 +402,23 @@ test "comptime union field value equality" {
402402 expect(a0 != a1);
403403 expect(b0 != b1);
404404}
405
406test "return union init with void payload" {
407 const S = struct {
408 fn entry() void {
409 expect(func().state == State.one);
410 }
411 const Outer = union(enum) {
412 state: State,
413 };
414 const State = union(enum) {
415 one: void,
416 two: u32,
417 };
418 fn func() Outer {
419 return Outer{ .state = State{ .one = {} }};
420 }
421 };
422 S.entry();
423 comptime S.entry();
424}