authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-11-08 23:17:26+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-09 12:20:34-05:00
logd18b5f8b53d24d5a27ebde5711d0d142a953adc0
tree28f752869bb64e68466c5453dc7345f8ab540c66
parent8c8078513e60df7e017f91193524384e4e9640ca

Fix initialization of union references

Fixes #3532

4 files changed, 32 insertions(+), 2 deletions(-)

src/ir.cpp+2-2
......@@ -18263,7 +18263,8 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1826318263 if (!ptr_val)
1826418264 return ira->codegen->invalid_instruction;
1826518265
18266 if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
18266 if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar &&
18267 ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) {
1826718268 ConstExprValue *union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node);
1826818269 if (union_val == nullptr)
1826918270 return ira->codegen->invalid_instruction;
......@@ -18295,7 +18296,6 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
1829518296
1829618297 ConstExprValue *payload_val = union_val->data.x_union.payload;
1829718298
18298
1829918299 IrInstruction *result;
1830018300 if (ptr_val->data.x_ptr.mut == ConstPtrMutInfer) {
1830118301 result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope,
src/ir_print.cpp+15
......@@ -2549,3 +2549,18 @@ void ir_print_instruction(CodeGen *codegen, FILE *f, IrInstruction *instruction,
25492549
25502550 ir_print_instruction(irp, instruction, false);
25512551}
2552
2553void ir_print_const_expr(CodeGen *codegen, FILE *f, ConstExprValue *value, int indent_size, IrPass pass) {
2554 IrPrint ir_print = {};
2555 IrPrint *irp = &ir_print;
2556 irp->pass = pass;
2557 irp->codegen = codegen;
2558 irp->f = f;
2559 irp->indent = indent_size;
2560 irp->indent_size = indent_size;
2561 irp->printed = {};
2562 irp->printed.init(4);
2563 irp->pending = {};
2564
2565 ir_print_const_value(irp, value);
2566}
src/ir_print.hpp+1
......@@ -14,6 +14,7 @@
1414
1515void ir_print(CodeGen *codegen, FILE *f, IrExecutable *executable, int indent_size, IrPass pass);
1616void ir_print_instruction(CodeGen *codegen, FILE *f, IrInstruction *instruction, int indent_size, IrPass pass);
17void ir_print_const_expr(CodeGen *codegen, FILE *f, ConstExprValue *value, int indent_size, IrPass pass);
1718
1819const char* ir_instruction_type_str(IrInstructionId id);
1920
test/stage1/behavior/union.zig+14
......@@ -535,3 +535,17 @@ test "global union with single field is correctly initialized" {
535535 };
536536 expect(glbl.f.x == 123);
537537}
538
539pub const FooUnion = union(enum) {
540 U0: usize,
541 U1: u8,
542};
543
544var glbl_array: [2]FooUnion = undefined;
545
546test "initialize global array of union" {
547 glbl_array[1] = FooUnion{ .U1 = 2 };
548 glbl_array[0] = FooUnion{ .U0 = 1 };
549 expect(glbl_array[0].U0 == 1);
550 expect(glbl_array[1].U1 == 2);
551}