Skip to content

Commit 9bd8b01

Browse files
committed
fix tagged union initialization with a runtime void
closes #1328
1 parent c66c630 commit 9bd8b01

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/ir.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9628,6 +9628,9 @@ static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, Un
96289628
case ConstValSpecialStatic:
96299629
return &value->value;
96309630
case ConstValSpecialRuntime:
9631+
if (!type_has_bits(value->value.type)) {
9632+
return &value->value;
9633+
}
96319634
ir_add_error(ira, value, buf_sprintf("unable to evaluate constant expression"));
96329635
return nullptr;
96339636
case ConstValSpecialUndef:
@@ -16129,8 +16132,14 @@ static TypeTableEntry *ir_analyze_container_init_fields_union(IrAnalyze *ira, Ir
1612916132
if (casted_field_value == ira->codegen->invalid_instruction)
1613016133
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+
1613216139
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+
{
1613416143
ConstExprValue *field_val = ir_resolve_const(ira, casted_field_value, UndefOk);
1613516144
if (!field_val)
1613616145
return ira->codegen->builtin_types.entry_invalid;

test/cases/union.zig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,17 @@ test "access a member of tagged union with conflicting enum tag name" {
297297

298298
comptime assert(Bar.A == u8);
299299
}
300+
301+
test "tagged union initialization with runtime void" {
302+
assert(testTaggedUnionInit({}));
303+
}
304+
305+
const TaggedUnionWithAVoid = union(enum) {
306+
A,
307+
B: i32,
308+
};
309+
310+
fn testTaggedUnionInit(x: var) bool {
311+
const y = TaggedUnionWithAVoid{ .A = x };
312+
return @TagType(TaggedUnionWithAVoid)(y) == TaggedUnionWithAVoid.A;
313+
}

0 commit comments

Comments
 (0)