Skip to content

Commit cacba6f

Browse files
committed
fix crash on union-enums with only 1 field
closes #713
1 parent b52bffc commit cacba6f

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

src/analyze.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -1867,7 +1867,7 @@ static void resolve_union_type(CodeGen *g, TypeTableEntry *union_type) {
18671867
uint64_t padding_in_bits = biggest_size_in_bits - size_of_most_aligned_member_in_bits;
18681868

18691869
TypeTableEntry *tag_type = union_type->data.unionation.tag_type;
1870-
if (tag_type == nullptr) {
1870+
if (tag_type == nullptr || tag_type->zero_bits) {
18711871
assert(most_aligned_union_member != nullptr);
18721872

18731873
if (padding_in_bits > 0) {
@@ -2509,8 +2509,10 @@ static void resolve_union_zero_bits(CodeGen *g, TypeTableEntry *union_type) {
25092509

25102510
if (create_enum_type) {
25112511
ImportTableEntry *import = get_scope_import(scope);
2512-
uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, tag_type->type_ref);
2513-
uint64_t tag_debug_align_in_bits = 8*LLVMABIAlignmentOfType(g->target_data_ref, tag_type->type_ref);
2512+
uint64_t tag_debug_size_in_bits = tag_type->zero_bits ? 0 :
2513+
8*LLVMStoreSizeOfType(g->target_data_ref, tag_type->type_ref);
2514+
uint64_t tag_debug_align_in_bits = tag_type->zero_bits ? 0 :
2515+
8*LLVMABIAlignmentOfType(g->target_data_ref, tag_type->type_ref);
25142516
// TODO get a more accurate debug scope
25152517
ZigLLVMDIType *tag_di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
25162518
ZigLLVMFileToScope(import->di_file), buf_ptr(&tag_type->name),
@@ -3451,7 +3453,6 @@ TypeUnionField *find_union_type_field(TypeTableEntry *type_entry, Buf *name) {
34513453
TypeUnionField *find_union_field_by_tag(TypeTableEntry *type_entry, const BigInt *tag) {
34523454
assert(type_entry->id == TypeTableEntryIdUnion);
34533455
assert(type_entry->data.unionation.zero_bits_known);
3454-
assert(type_entry->data.unionation.gen_tag_index != SIZE_MAX);
34553456
for (uint32_t i = 0; i < type_entry->data.unionation.src_field_count; i += 1) {
34563457
TypeUnionField *field = &type_entry->data.unionation.fields[i];
34573458
if (bigint_cmp(&field->enum_field->value, tag) == CmpEQ) {

src/ir.cpp

+16-5
Original file line numberDiff line numberDiff line change
@@ -4769,7 +4769,8 @@ static IrInstruction *ir_gen_if_err_expr(IrBuilder *irb, Scope *scope, AstNode *
47694769
}
47704770

47714771
static bool ir_gen_switch_prong_expr(IrBuilder *irb, Scope *scope, AstNode *switch_node, AstNode *prong_node,
4772-
IrBasicBlock *end_block, IrInstruction *is_comptime, IrInstruction *target_value_ptr, IrInstruction *prong_value,
4772+
IrBasicBlock *end_block, IrInstruction *is_comptime, IrInstruction *var_is_comptime,
4773+
IrInstruction *target_value_ptr, IrInstruction *prong_value,
47734774
ZigList<IrBasicBlock *> *incoming_blocks, ZigList<IrInstruction *> *incoming_values)
47744775
{
47754776
assert(switch_node->type == NodeTypeSwitchExpr);
@@ -4786,7 +4787,7 @@ static bool ir_gen_switch_prong_expr(IrBuilder *irb, Scope *scope, AstNode *swit
47864787
bool is_shadowable = false;
47874788
bool is_const = true;
47884789
VariableTableEntry *var = ir_create_var(irb, var_symbol_node, scope,
4789-
var_name, is_const, is_const, is_shadowable, is_comptime);
4790+
var_name, is_const, is_const, is_shadowable, var_is_comptime);
47904791
child_scope = var->child_scope;
47914792
IrInstruction *var_value;
47924793
if (prong_value) {
@@ -4827,10 +4828,13 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
48274828
ZigList<IrInstructionSwitchBrCase> cases = {0};
48284829

48294830
IrInstruction *is_comptime;
4831+
IrInstruction *var_is_comptime;
48304832
if (ir_should_inline(irb->exec, scope)) {
48314833
is_comptime = ir_build_const_bool(irb, scope, node, true);
4834+
var_is_comptime = is_comptime;
48324835
} else {
48334836
is_comptime = ir_build_test_comptime(irb, scope, node, target_value);
4837+
var_is_comptime = ir_build_test_comptime(irb, scope, node, target_value_ptr);
48344838
}
48354839

48364840
ZigList<IrInstruction *> incoming_values = {0};
@@ -4856,7 +4860,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
48564860
IrBasicBlock *prev_block = irb->current_basic_block;
48574861
ir_set_cursor_at_end_and_append_block(irb, else_block);
48584862
if (!ir_gen_switch_prong_expr(irb, scope, node, prong_node, end_block,
4859-
is_comptime, target_value_ptr, nullptr, &incoming_blocks, &incoming_values))
4863+
is_comptime, var_is_comptime, target_value_ptr, nullptr, &incoming_blocks, &incoming_values))
48604864
{
48614865
return irb->codegen->invalid_instruction;
48624866
}
@@ -4923,7 +4927,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
49234927

49244928
ir_set_cursor_at_end_and_append_block(irb, range_block_yes);
49254929
if (!ir_gen_switch_prong_expr(irb, scope, node, prong_node, end_block,
4926-
is_comptime, target_value_ptr, nullptr, &incoming_blocks, &incoming_values))
4930+
is_comptime, var_is_comptime, target_value_ptr, nullptr, &incoming_blocks, &incoming_values))
49274931
{
49284932
return irb->codegen->invalid_instruction;
49294933
}
@@ -4967,7 +4971,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
49674971
IrBasicBlock *prev_block = irb->current_basic_block;
49684972
ir_set_cursor_at_end_and_append_block(irb, prong_block);
49694973
if (!ir_gen_switch_prong_expr(irb, scope, node, prong_node, end_block,
4970-
is_comptime, target_value_ptr, only_item_value, &incoming_blocks, &incoming_values))
4974+
is_comptime, var_is_comptime, target_value_ptr, only_item_value, &incoming_blocks, &incoming_values))
49714975
{
49724976
return irb->codegen->invalid_instruction;
49734977
}
@@ -12254,11 +12258,18 @@ static TypeTableEntry *ir_analyze_instruction_switch_target(IrAnalyze *ira,
1225412258
}
1225512259
TypeTableEntry *tag_type = target_type->data.unionation.tag_type;
1225612260
assert(tag_type != nullptr);
12261+
assert(tag_type->id == TypeTableEntryIdEnum);
1225712262
if (pointee_val) {
1225812263
ConstExprValue *out_val = ir_build_const_from(ira, &switch_target_instruction->base);
1225912264
bigint_init_bigint(&out_val->data.x_enum_tag, &pointee_val->data.x_union.tag);
1226012265
return tag_type;
1226112266
}
12267+
if (tag_type->data.enumeration.src_field_count == 1) {
12268+
ConstExprValue *out_val = ir_build_const_from(ira, &switch_target_instruction->base);
12269+
TypeEnumField *only_field = &tag_type->data.enumeration.fields[0];
12270+
bigint_init_bigint(&out_val->data.x_enum_tag, &only_field->value);
12271+
return tag_type;
12272+
}
1226212273

1226312274
IrInstruction *union_value = ir_build_load_ptr(&ira->new_irb, switch_target_instruction->base.scope,
1226412275
switch_target_instruction->base.source_node, target_value_ptr);

test/cases/union.zig

+27
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,30 @@ test "constant packed union" {
235235
fn testConstPackedUnion(expected_tokens: []const PackThis) {
236236
assert(expected_tokens[0].StringLiteral == 1);
237237
}
238+
239+
test "switch on union with only 1 field" {
240+
var r: PartialInst = undefined;
241+
r = PartialInst.Compiled;
242+
switch (r) {
243+
PartialInst.Compiled => {
244+
var z: PartialInstWithPayload = undefined;
245+
z = PartialInstWithPayload { .Compiled = 1234 };
246+
switch (z) {
247+
PartialInstWithPayload.Compiled => |x| {
248+
assert(x == 1234);
249+
return;
250+
},
251+
}
252+
},
253+
}
254+
unreachable;
255+
}
256+
257+
const PartialInst = union(enum) {
258+
Compiled,
259+
};
260+
261+
const PartialInstWithPayload = union(enum) {
262+
Compiled: i32,
263+
};
264+

0 commit comments

Comments
 (0)