authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-09-09 19:09:56+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-09-09 19:09:56+02:00
log4b1cd45472cab5569438fef018990fbe8043c6c3
treeae1f233ad992910d92b3882202cda30808491d4e
parentcc6376058784aa7a910e93c31ac8bd819de4e187

Comptime folding of enum/union comparisons


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

src/ir.cpp+15
......@@ -13247,6 +13247,21 @@ static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp *
1324713247 if (type_is_invalid(casted_val->value.type))
1324813248 return ira->codegen->invalid_instruction;
1324913249
13250 if (instr_is_comptime(casted_union)) {
13251 ConstExprValue *const_union_val = ir_resolve_const(ira, casted_union, UndefBad);
13252 if (!const_union_val)
13253 return ira->codegen->invalid_instruction;
13254
13255 ConstExprValue *const_enum_val = ir_resolve_const(ira, casted_val, UndefBad);
13256 if (!const_enum_val)
13257 return ira->codegen->invalid_instruction;
13258
13259 Cmp cmp_result = bigint_cmp(&const_union_val->data.x_union.tag, &const_enum_val->data.x_enum_tag);
13260 bool bool_result = (op_id == IrBinOpCmpEq) ? cmp_result == CmpEQ : cmp_result != CmpEQ;
13261
13262 return ir_const_bool(ira, &bin_op_instruction->base, bool_result);
13263 }
13264
1325013265 IrInstruction *result = ir_build_bin_op(&ira->new_irb,
1325113266 bin_op_instruction->base.scope, bin_op_instruction->base.source_node,
1325213267 op_id, casted_union, casted_val, bin_op_instruction->safety_check_on);
test/stage1/behavior/union.zig+6-1
......@@ -468,7 +468,7 @@ test "union no tag with struct member" {
468468 u.foo();
469469}
470470
471test "comparison between union and enum literal" {
471fn testComparison() void {
472472 var x = Payload{.A = 42};
473473 expect(x == .A);
474474 expect(x != .B);
......@@ -477,3 +477,8 @@ test "comparison between union and enum literal" {
477477 expect((x == .C) == false);
478478 expect((x != .A) == false);
479479}
480
481test "comparison between union and enum literal" {
482 testComparison();
483 comptime testComparison();
484}