authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-03 15:21:08-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-08-03 15:21:08-04:00
log9bd8b01650f9cf21e601117951711b21aa5fd216
treebb96a78a797d6e762a43cb6e2d1492fb336d04bf
parentc66c6304f90aa9ea9983134c857ba7ea014004a3

fix tagged union initialization with a runtime void

closes #1328

2 files changed, 24 insertions(+), 1 deletions(-)

src/ir.cpp+10-1
...@@ -9628,6 +9628,9 @@ static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, Un...@@ -9628,6 +9628,9 @@ static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, Un
9628 case ConstValSpecialStatic:9628 case ConstValSpecialStatic:
9629 return &value->value;9629 return &value->value;
9630 case ConstValSpecialRuntime:9630 case ConstValSpecialRuntime:
9631 if (!type_has_bits(value->value.type)) {
9632 return &value->value;
9633 }
9631 ir_add_error(ira, value, buf_sprintf("unable to evaluate constant expression"));9634 ir_add_error(ira, value, buf_sprintf("unable to evaluate constant expression"));
9632 return nullptr;9635 return nullptr;
9633 case ConstValSpecialUndef:9636 case ConstValSpecialUndef:
...@@ -16129,8 +16132,14 @@ static TypeTableEntry *ir_analyze_container_init_fields_union(IrAnalyze *ira, Ir...@@ -16129,8 +16132,14 @@ static TypeTableEntry *ir_analyze_container_init_fields_union(IrAnalyze *ira, Ir
16129 if (casted_field_value == ira->codegen->invalid_instruction)16132 if (casted_field_value == ira->codegen->invalid_instruction)
16130 return ira->codegen->builtin_types.entry_invalid;16133 return ira->codegen->builtin_types.entry_invalid;
1613116134
16135 type_ensure_zero_bits_known(ira->codegen, casted_field_value->value.type);
16136 if (type_is_invalid(casted_field_value->value.type))
16137 return ira->codegen->builtin_types.entry_invalid;
16138
16132 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->scope);16139 bool is_comptime = ir_should_inline(ira->new_irb.exec, instruction->scope);
16133 if (is_comptime || casted_field_value->value.special != ConstValSpecialRuntime) {16140 if (is_comptime || casted_field_value->value.special != ConstValSpecialRuntime ||
16141 !type_has_bits(casted_field_value->value.type))
16142 {
16134 ConstExprValue *field_val = ir_resolve_const(ira, casted_field_value, UndefOk);16143 ConstExprValue *field_val = ir_resolve_const(ira, casted_field_value, UndefOk);
16135 if (!field_val)16144 if (!field_val)
16136 return ira->codegen->builtin_types.entry_invalid;16145 return ira->codegen->builtin_types.entry_invalid;
test/cases/union.zig+14
...@@ -297,3 +297,17 @@ test "access a member of tagged union with conflicting enum tag name" {...@@ -297,3 +297,17 @@ test "access a member of tagged union with conflicting enum tag name" {
297297
298 comptime assert(Bar.A == u8);298 comptime assert(Bar.A == u8);
299}299}
300
301test "tagged union initialization with runtime void" {
302 assert(testTaggedUnionInit({}));
303}
304
305const TaggedUnionWithAVoid = union(enum) {
306 A,
307 B: i32,
308};
309
310fn testTaggedUnionInit(x: var) bool {
311 const y = TaggedUnionWithAVoid{ .A = x };
312 return @TagType(TaggedUnionWithAVoid)(y) == TaggedUnionWithAVoid.A;
313}