Skip to content

Commit bb6b4f8

Browse files
committedDec 6, 2017
fix enum with 1 member causing segfault
closes #647
·
0.15.20.2.0
1 parent c49ee9f commit bb6b4f8

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed
 

‎src/ir.cpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9504,6 +9504,10 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
95049504
TypeTableEntry *resolved_type = ir_resolve_peer_types(ira, bin_op_instruction->base.source_node, instructions, 2);
95059505
if (type_is_invalid(resolved_type))
95069506
return resolved_type;
9507+
type_ensure_zero_bits_known(ira->codegen, resolved_type);
9508+
if (type_is_invalid(resolved_type))
9509+
return resolved_type;
9510+
95079511

95089512
AstNode *source_node = bin_op_instruction->base.source_node;
95099513
switch (resolved_type->id) {
@@ -9568,7 +9572,8 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
95689572

95699573
ConstExprValue *op1_val = &casted_op1->value;
95709574
ConstExprValue *op2_val = &casted_op2->value;
9571-
if ((value_is_comptime(op1_val) && value_is_comptime(op2_val)) || resolved_type->id == TypeTableEntryIdVoid) {
9575+
bool one_possible_value = !type_requires_comptime(resolved_type) && !type_has_bits(resolved_type);
9576+
if (one_possible_value || (value_is_comptime(op1_val) && value_is_comptime(op2_val))) {
95729577
bool answer;
95739578
if (resolved_type->id == TypeTableEntryIdNumLitFloat || resolved_type->id == TypeTableEntryIdFloat) {
95749579
Cmp cmp_result = float_cmp(op1_val, op2_val);
@@ -9577,7 +9582,7 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
95779582
Cmp cmp_result = bigint_cmp(&op1_val->data.x_bigint, &op2_val->data.x_bigint);
95789583
answer = resolve_cmp_op_id(op_id, cmp_result);
95799584
} else {
9580-
bool are_equal = resolved_type->id == TypeTableEntryIdVoid || const_values_equal(op1_val, op2_val);
9585+
bool are_equal = one_possible_value || const_values_equal(op1_val, op2_val);
95819586
if (op_id == IrBinOpCmpEq) {
95829587
answer = are_equal;
95839588
} else if (op_id == IrBinOpCmpNotEq) {

‎test/cases/enum.zig‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,20 @@ test "cast integer literal to enum" {
349349
assert(MultipleChoice2(0) == MultipleChoice2.Unspecified1);
350350
assert(MultipleChoice2(40) == MultipleChoice2.B);
351351
}
352+
353+
const EnumWithOneMember = enum {
354+
Eof,
355+
};
356+
357+
fn doALoopThing(id: EnumWithOneMember) {
358+
while (true) {
359+
if (id == EnumWithOneMember.Eof) {
360+
break;
361+
}
362+
@compileError("above if condition should be comptime");
363+
}
364+
}
365+
366+
test "comparison operator on enum with one member is comptime known" {
367+
doALoopThing(EnumWithOneMember.Eof);
368+
}

0 commit comments

Comments
 (0)
Please sign in to comment.