From da9d8a6ecfb0428ce3d6575905f96fa1661c9b80 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 24 Mar 2019 18:47:36 -0400 Subject: [PATCH] implement peer type resolution for enum literals See #683 --- src/ir.cpp | 15 +++++++++++++++ test/stage1/behavior/enum.zig | 10 ++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/ir.cpp b/src/ir.cpp index 29b6eef27ab5..58db31566568 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -9573,6 +9573,21 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT continue; } + if (prev_type->id == ZigTypeIdEnum && cur_type->id == ZigTypeIdEnumLiteral) { + TypeEnumField *field = find_enum_type_field(prev_type, cur_inst->value.data.x_enum_literal); + if (field != nullptr) { + continue; + } + } + + if (cur_type->id == ZigTypeIdEnum && prev_type->id == ZigTypeIdEnumLiteral) { + TypeEnumField *field = find_enum_type_field(cur_type, prev_inst->value.data.x_enum_literal); + if (field != nullptr) { + prev_inst = cur_inst; + continue; + } + } + if (prev_type->id == ZigTypeIdPointer && prev_type->data.pointer.ptr_len == PtrLenC && (cur_type->id == ZigTypeIdComptimeInt || cur_type->id == ZigTypeIdInt)) { diff --git a/test/stage1/behavior/enum.zig b/test/stage1/behavior/enum.zig index 2be675ea340a..f584fc265a9e 100644 --- a/test/stage1/behavior/enum.zig +++ b/test/stage1/behavior/enum.zig @@ -913,3 +913,13 @@ test "enum literal cast to enum" { var color2 = Color.Auto; expect(color1 == color2); } + +test "peer type resolution with enum literal" { + const Items = enum { + one, + two, + }; + + expect(Items.two == .two); + expect(.two == Items.two); +}