diff --git a/src/ir.cpp b/src/ir.cpp index 9225968ce9a1c9b5e0576cce29e48f2e31be7a79..507b3e445989d899d03ebf9a5cbfa31e93d402a8 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -18263,7 +18263,8 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_ if (!ptr_val) return ira->codegen->invalid_instruction; - if (ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) { + if (ptr_val->data.x_ptr.mut != ConstPtrMutRuntimeVar && + ptr_val->data.x_ptr.special != ConstPtrSpecialHardCodedAddr) { ConstExprValue *union_val = const_ptr_pointee(ira, ira->codegen, ptr_val, source_instr->source_node); if (union_val == nullptr) return ira->codegen->invalid_instruction; @@ -18295,7 +18296,6 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_ ConstExprValue *payload_val = union_val->data.x_union.payload; - IrInstruction *result; if (ptr_val->data.x_ptr.mut == ConstPtrMutInfer) { result = ir_build_union_field_ptr(&ira->new_irb, source_instr->scope, diff --git a/src/ir_print.cpp b/src/ir_print.cpp index 85b58faefc177a0dc531e99ee7dc530e49218e82..89cfab22c2927cd6eec4e1132d5f6bcd6c717e4e 100644 --- a/src/ir_print.cpp +++ b/src/ir_print.cpp @@ -2549,3 +2549,18 @@ void ir_print_instruction(CodeGen *codegen, FILE *f, IrInstruction *instruction, ir_print_instruction(irp, instruction, false); } + +void ir_print_const_expr(CodeGen *codegen, FILE *f, ConstExprValue *value, int indent_size, IrPass pass) { + IrPrint ir_print = {}; + IrPrint *irp = &ir_print; + irp->pass = pass; + irp->codegen = codegen; + irp->f = f; + irp->indent = indent_size; + irp->indent_size = indent_size; + irp->printed = {}; + irp->printed.init(4); + irp->pending = {}; + + ir_print_const_value(irp, value); +} diff --git a/src/ir_print.hpp b/src/ir_print.hpp index e3947077c8f018a98da79922ef00559237c2308f..64af959f53f548022be9830c67174bc892f25f19 100644 --- a/src/ir_print.hpp +++ b/src/ir_print.hpp @@ -14,6 +14,7 @@ void ir_print(CodeGen *codegen, FILE *f, IrExecutable *executable, int indent_size, IrPass pass); void ir_print_instruction(CodeGen *codegen, FILE *f, IrInstruction *instruction, int indent_size, IrPass pass); +void ir_print_const_expr(CodeGen *codegen, FILE *f, ConstExprValue *value, int indent_size, IrPass pass); const char* ir_instruction_type_str(IrInstructionId id); diff --git a/test/stage1/behavior/union.zig b/test/stage1/behavior/union.zig index 40af5c4fd451b774941d16cf22434037cca3c99a..497aa7e5740fc04494067b7fae9c672d1d15c882 100644 --- a/test/stage1/behavior/union.zig +++ b/test/stage1/behavior/union.zig @@ -535,3 +535,17 @@ test "global union with single field is correctly initialized" { }; expect(glbl.f.x == 123); } + +pub const FooUnion = union(enum) { + U0: usize, + U1: u8, +}; + +var glbl_array: [2]FooUnion = undefined; + +test "initialize global array of union" { + glbl_array[1] = FooUnion{ .U1 = 2 }; + glbl_array[0] = FooUnion{ .U0 = 1 }; + expect(glbl_array[0].U0 == 1); + expect(glbl_array[1].U1 == 2); +}