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
signaturelock-open 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) {...@@ -5001,12 +5001,9 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val) {
5001 field_val->type = wanted_type->data.structure.fields[i].type_entry;5001 field_val->type = wanted_type->data.structure.fields[i].type_entry;
5002 assert(field_val->type);5002 assert(field_val->type);
5003 init_const_undefined(g, field_val);5003 init_const_undefined(g, field_val);
5004 ConstParent *parent = get_const_val_parent(g, field_val);5004 field_val->parent.id = ConstParentIdStruct;
5005 if (parent != nullptr) {5005 field_val->parent.data.p_struct.struct_val = const_val;
5006 parent->id = ConstParentIdStruct;5006 field_val->parent.data.p_struct.field_index = i;
5007 parent->data.p_struct.struct_val = const_val;
5008 parent->data.p_struct.field_index = i;
5009 }
5010 }5007 }
5011 } else {5008 } else {
5012 const_val->special = ConstValSpecialUndef;5009 const_val->special = ConstValSpecialUndef;
...@@ -5842,11 +5839,6 @@ void expand_undef_array(CodeGen *g, ConstExprValue *const_val) {...@@ -5842,11 +5839,6 @@ void expand_undef_array(CodeGen *g, ConstExprValue *const_val) {
5842 zig_unreachable();5839 zig_unreachable();
5843}5840}
58445841
5845// Deprecated. Reference the parent field directly.
5846ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value) {
5847 return &value->parent;
5848}
5849
5850static const ZigTypeId all_type_ids[] = {5842static const ZigTypeId all_type_ids[] = {
5851 ZigTypeIdMetaType,5843 ZigTypeIdMetaType,
5852 ZigTypeIdVoid,5844 ZigTypeIdVoid,
src/analyze.hpp-1
...@@ -180,7 +180,6 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val);...@@ -180,7 +180,6 @@ void init_const_undefined(CodeGen *g, ConstExprValue *const_val);
180ConstExprValue *create_const_vals(size_t count);180ConstExprValue *create_const_vals(size_t count);
181181
182ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);182ZigType *make_int_type(CodeGen *g, bool is_signed, uint32_t size_in_bits);
183ConstParent *get_const_val_parent(CodeGen *g, ConstExprValue *value);
184void expand_undef_array(CodeGen *g, ConstExprValue *const_val);183void expand_undef_array(CodeGen *g, ConstExprValue *const_val);
185void update_compile_var(CodeGen *g, Buf *name, ConstExprValue *value);184void 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...@@ -3873,8 +3873,20 @@ static LLVMValueRef ir_render_union_field_ptr(CodeGen *g, IrExecutable *executab
38733873
3874 TypeUnionField *field = instruction->field;3874 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 }
3877 return nullptr;3888 return nullptr;
3889 }
38783890
3879 LLVMValueRef union_ptr = ir_llvm_value(g, instruction->union_ptr);3891 LLVMValueRef union_ptr = ir_llvm_value(g, instruction->union_ptr);
3880 LLVMTypeRef field_type_ref = LLVMPointerType(get_llvm_type(g, field->type_entry), 0);3892 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...@@ -17425,11 +17425,9 @@ static IrInstruction *ir_analyze_struct_field_ptr(IrAnalyze *ira, IrInstruction
17425 ConstExprValue *field_val = &struct_val->data.x_struct.fields[i];17425 ConstExprValue *field_val = &struct_val->data.x_struct.fields[i];
17426 field_val->special = ConstValSpecialUndef;17426 field_val->special = ConstValSpecialUndef;
17427 field_val->type = struct_type->data.structure.fields[i].type_entry;17427 field_val->type = struct_type->data.structure.fields[i].type_entry;
17428 ConstParent *parent = get_const_val_parent(ira->codegen, field_val);17428 field_val->parent.id = ConstParentIdStruct;
17429 assert(parent != nullptr);17429 field_val->parent.data.p_struct.struct_val = struct_val;
17430 parent->id = ConstParentIdStruct;17430 field_val->parent.data.p_struct.field_index = i;
17431 parent->data.p_struct.struct_val = struct_val;
17432 parent->data.p_struct.field_index = i;
17433 }17431 }
17434 }17432 }
17435 IrInstruction *result;17433 IrInstruction *result;
...@@ -17507,11 +17505,8 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_...@@ -17507,11 +17505,8 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
17507 ConstExprValue *payload_val = create_const_vals(1);17505 ConstExprValue *payload_val = create_const_vals(1);
17508 payload_val->special = ConstValSpecialUndef;17506 payload_val->special = ConstValSpecialUndef;
17509 payload_val->type = field->type_entry;17507 payload_val->type = field->type_entry;
17510 ConstParent *parent = get_const_val_parent(ira->codegen, payload_val);17508 payload_val->parent.id = ConstParentIdUnion;
17511 if (parent != nullptr) {17509 payload_val->parent.data.p_union.union_val = union_val;
17512 parent->id = ConstParentIdUnion;
17513 parent->data.p_union.union_val = union_val;
17514 }
1751517510
17516 union_val->special = ConstValSpecialStatic;17511 union_val->special = ConstValSpecialStatic;
17517 bigint_init_bigint(&union_val->data.x_union.tag, &field->enum_field->value);17512 bigint_init_bigint(&union_val->data.x_union.tag, &field->enum_field->value);
...@@ -25289,7 +25284,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -25289,7 +25284,6 @@ bool ir_has_side_effects(IrInstruction *instruction) {
25289 case IrInstructionIdReturnPtr:25284 case IrInstructionIdReturnPtr:
25290 case IrInstructionIdTypeOf:25285 case IrInstructionIdTypeOf:
25291 case IrInstructionIdStructFieldPtr:25286 case IrInstructionIdStructFieldPtr:
25292 case IrInstructionIdUnionFieldPtr:
25293 case IrInstructionIdArrayType:25287 case IrInstructionIdArrayType:
25294 case IrInstructionIdPromiseType:25288 case IrInstructionIdPromiseType:
25295 case IrInstructionIdSliceType:25289 case IrInstructionIdSliceType:
...@@ -25389,6 +25383,8 @@ bool ir_has_side_effects(IrInstruction *instruction) {...@@ -25389,6 +25383,8 @@ bool ir_has_side_effects(IrInstruction *instruction) {
25389 }25383 }
25390 case IrInstructionIdUnwrapErrCode:25384 case IrInstructionIdUnwrapErrCode:
25391 return reinterpret_cast<IrInstructionUnwrapErrCode *>(instruction)->initializing;25385 return reinterpret_cast<IrInstructionUnwrapErrCode *>(instruction)->initializing;
25386 case IrInstructionIdUnionFieldPtr:
25387 return reinterpret_cast<IrInstructionUnionFieldPtr *>(instruction)->initializing;
25392 case IrInstructionIdErrWrapPayload:25388 case IrInstructionIdErrWrapPayload:
25393 return reinterpret_cast<IrInstructionErrWrapPayload *>(instruction)->result_loc != nullptr;25389 return reinterpret_cast<IrInstructionErrWrapPayload *>(instruction)->result_loc != nullptr;
25394 case IrInstructionIdErrWrapCode:25390 case IrInstructionIdErrWrapCode:
std/zig/parser_test.zig+1-11
...@@ -1,4 +1,4 @@...@@ -1,4 +1,4 @@
1// TODO remove `use` keyword eventually1// TODO remove `use` keyword eventually: https://github.com/ziglang/zig/issues/2591
2test "zig fmt: change use to usingnamespace" {2test "zig fmt: change use to usingnamespace" {
3 try testTransform(3 try testTransform(
4 \\use @import("std");4 \\use @import("std");
...@@ -105,7 +105,6 @@ test "zig fmt: linksection" {...@@ -105,7 +105,6 @@ test "zig fmt: linksection" {
105}105}
106106
107test "zig fmt: correctly move doc comments on struct fields" {107test "zig fmt: correctly move doc comments on struct fields" {
108 if (true) return error.SkipZigTest; // TODO
109 try testTransform(108 try testTransform(
110 \\pub const section_64 = extern struct {109 \\pub const section_64 = extern struct {
111 \\ sectname: [16]u8, /// name of this section110 \\ sectname: [16]u8, /// name of this section
...@@ -917,7 +916,6 @@ test "zig fmt: statements with empty line between" {...@@ -917,7 +916,6 @@ test "zig fmt: statements with empty line between" {
917}916}
918917
919test "zig fmt: ptr deref operator and unwrap optional operator" {918test "zig fmt: ptr deref operator and unwrap optional operator" {
920 if (true) return error.SkipZigTest; // TODO
921 try testCanonical(919 try testCanonical(
922 \\const a = b.*;920 \\const a = b.*;
923 \\const a = b.?;921 \\const a = b.?;
...@@ -1020,7 +1018,6 @@ test "zig fmt: same-line comment after a statement" {...@@ -1020,7 +1018,6 @@ test "zig fmt: same-line comment after a statement" {
1020}1018}
10211019
1022test "zig fmt: same-line comment after var decl in struct" {1020test "zig fmt: same-line comment after var decl in struct" {
1023 if (true) return error.SkipZigTest; // TODO
1024 try testCanonical(1021 try testCanonical(
1025 \\pub const vfs_cap_data = extern struct {1022 \\pub const vfs_cap_data = extern struct {
1026 \\ const Data = struct {}; // when on disk.1023 \\ const Data = struct {}; // when on disk.
...@@ -1030,7 +1027,6 @@ test "zig fmt: same-line comment after var decl in struct" {...@@ -1030,7 +1027,6 @@ test "zig fmt: same-line comment after var decl in struct" {
1030}1027}
10311028
1032test "zig fmt: same-line comment after field decl" {1029test "zig fmt: same-line comment after field decl" {
1033 if (true) return error.SkipZigTest; // TODO
1034 try testCanonical(1030 try testCanonical(
1035 \\pub const dirent = extern struct {1031 \\pub const dirent = extern struct {
1036 \\ d_name: u8,1032 \\ d_name: u8,
...@@ -1106,7 +1102,6 @@ test "zig fmt: line comments in struct initializer" {...@@ -1106,7 +1102,6 @@ test "zig fmt: line comments in struct initializer" {
1106}1102}
11071103
1108test "zig fmt: first line comment in struct initializer" {1104test "zig fmt: first line comment in struct initializer" {
1109 if (true) return error.SkipZigTest; // TODO
1110 try testCanonical(1105 try testCanonical(
1111 \\pub async fn acquire(self: *Self) HeldLock {1106 \\pub async fn acquire(self: *Self) HeldLock {
1112 \\ return HeldLock{1107 \\ return HeldLock{
...@@ -1120,7 +1115,6 @@ test "zig fmt: first line comment in struct initializer" {...@@ -1120,7 +1115,6 @@ test "zig fmt: first line comment in struct initializer" {
1120}1115}
11211116
1122test "zig fmt: doc comments before struct field" {1117test "zig fmt: doc comments before struct field" {
1123 if (true) return error.SkipZigTest; // TODO
1124 try testCanonical(1118 try testCanonical(
1125 \\pub const Allocator = struct {1119 \\pub const Allocator = struct {
1126 \\ /// Allocate byte_count bytes and return them in a slice, with the1120 \\ /// Allocate byte_count bytes and return them in a slice, with the
...@@ -1218,7 +1212,6 @@ test "zig fmt: comments before switch prong" {...@@ -1218,7 +1212,6 @@ test "zig fmt: comments before switch prong" {
1218}1212}
12191213
1220test "zig fmt: comments before var decl in struct" {1214test "zig fmt: comments before var decl in struct" {
1221 if (true) return error.SkipZigTest; // TODO
1222 try testCanonical(1215 try testCanonical(
1223 \\pub const vfs_cap_data = extern struct {1216 \\pub const vfs_cap_data = extern struct {
1224 \\ // All of these are mandated as little endian1217 \\ // All of these are mandated as little endian
...@@ -1609,7 +1602,6 @@ test "zig fmt: indexing" {...@@ -1609,7 +1602,6 @@ test "zig fmt: indexing" {
1609}1602}
16101603
1611test "zig fmt: struct declaration" {1604test "zig fmt: struct declaration" {
1612 if (true) return error.SkipZigTest; // TODO
1613 try testCanonical(1605 try testCanonical(
1614 \\const S = struct {1606 \\const S = struct {
1615 \\ const Self = @This();1607 \\ const Self = @This();
...@@ -1641,7 +1633,6 @@ test "zig fmt: struct declaration" {...@@ -1641,7 +1633,6 @@ test "zig fmt: struct declaration" {
1641}1633}
16421634
1643test "zig fmt: enum declaration" {1635test "zig fmt: enum declaration" {
1644 if (true) return error.SkipZigTest; // TODO
1645 try testCanonical(1636 try testCanonical(
1646 \\const E = enum {1637 \\const E = enum {
1647 \\ Ok,1638 \\ Ok,
...@@ -1670,7 +1661,6 @@ test "zig fmt: enum declaration" {...@@ -1670,7 +1661,6 @@ test "zig fmt: enum declaration" {
1670}1661}
16711662
1672test "zig fmt: union declaration" {1663test "zig fmt: union declaration" {
1673 if (true) return error.SkipZigTest; // TODO
1674 try testCanonical(1664 try testCanonical(
1675 \\const U = union {1665 \\const U = union {
1676 \\ Int: u8,1666 \\ Int: u8,
test/stage1/behavior/union.zig+20
...@@ -402,3 +402,23 @@ test "comptime union field value equality" {...@@ -402,3 +402,23 @@ test "comptime union field value equality" {
402 expect(a0 != a1);402 expect(a0 != a1);
403 expect(b0 != b1);403 expect(b0 != b1);
404}404}
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}