From cc6376058784aa7a910e93c31ac8bd819de4e187 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 9 Sep 2019 18:51:13 +0200 Subject: Allow comparison between union tag and enum literal Closes #2810 --- src/ir.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index 6b71fa8d17..bbea993162 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -13228,6 +13228,31 @@ static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp * ir_add_error_node(ira, source_node, buf_sprintf("comparison of '%s' with null", buf_ptr(&non_null_type->name))); return ira->codegen->invalid_instruction; + } else if (is_equality_cmp && ( + (op1->value.type->id == ZigTypeIdEnumLiteral && op2->value.type->id == ZigTypeIdUnion) || + (op2->value.type->id == ZigTypeIdEnumLiteral && op1->value.type->id == ZigTypeIdUnion))) + { + // Support equality comparison between a union's tag value and a enum literal + IrInstruction *union_val = op1->value.type->id == ZigTypeIdUnion ? op1 : op2; + IrInstruction *enum_val = op1->value.type->id == ZigTypeIdUnion ? op2 : op1; + + ZigType *tag_type = union_val->value.type->data.unionation.tag_type; + assert(tag_type != nullptr); + + IrInstruction *casted_union = ir_implicit_cast(ira, union_val, tag_type); + if (type_is_invalid(casted_union->value.type)) + return ira->codegen->invalid_instruction; + + IrInstruction *casted_val = ir_implicit_cast(ira, enum_val, tag_type); + if (type_is_invalid(casted_val->value.type)) + return ira->codegen->invalid_instruction; + + IrInstruction *result = ir_build_bin_op(&ira->new_irb, + bin_op_instruction->base.scope, bin_op_instruction->base.source_node, + op_id, casted_union, casted_val, bin_op_instruction->safety_check_on); + result->value.type = ira->codegen->builtin_types.entry_bool; + + return result; } if (op1->value.type->id == ZigTypeIdErrorSet && op2->value.type->id == ZigTypeIdErrorSet) { -- cgit v1.2.3 From 4b1cd45472cab5569438fef018990fbe8043c6c3 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Mon, 9 Sep 2019 19:09:56 +0200 Subject: Comptime folding of enum/union comparisons --- src/ir.cpp | 15 +++++++++++++++ test/stage1/behavior/union.zig | 7 ++++++- 2 files changed, 21 insertions(+), 1 deletion(-) (limited to 'src/ir.cpp') diff --git a/src/ir.cpp b/src/ir.cpp index bbea993162..196e84eead 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -13247,6 +13247,21 @@ static IrInstruction *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp * if (type_is_invalid(casted_val->value.type)) return ira->codegen->invalid_instruction; + if (instr_is_comptime(casted_union)) { + ConstExprValue *const_union_val = ir_resolve_const(ira, casted_union, UndefBad); + if (!const_union_val) + return ira->codegen->invalid_instruction; + + ConstExprValue *const_enum_val = ir_resolve_const(ira, casted_val, UndefBad); + if (!const_enum_val) + return ira->codegen->invalid_instruction; + + Cmp cmp_result = bigint_cmp(&const_union_val->data.x_union.tag, &const_enum_val->data.x_enum_tag); + bool bool_result = (op_id == IrBinOpCmpEq) ? cmp_result == CmpEQ : cmp_result != CmpEQ; + + return ir_const_bool(ira, &bin_op_instruction->base, bool_result); + } + IrInstruction *result = ir_build_bin_op(&ira->new_irb, bin_op_instruction->base.scope, bin_op_instruction->base.source_node, op_id, casted_union, casted_val, bin_op_instruction->safety_check_on); diff --git a/test/stage1/behavior/union.zig b/test/stage1/behavior/union.zig index 1f8ca82958..7c5c653275 100644 --- a/test/stage1/behavior/union.zig +++ b/test/stage1/behavior/union.zig @@ -468,7 +468,7 @@ test "union no tag with struct member" { u.foo(); } -test "comparison between union and enum literal" { +fn testComparison() void { var x = Payload{.A = 42}; expect(x == .A); expect(x != .B); @@ -477,3 +477,8 @@ test "comparison between union and enum literal" { expect((x == .C) == false); expect((x != .A) == false); } + +test "comparison between union and enum literal" { + testComparison(); + comptime testComparison(); +} -- cgit v1.2.3