From 8c17fd71fbfaf48f49a03bdf01930b72bbd69dc3 Mon Sep 17 00:00:00 2001 From: hlaaftana <10591326+metagn@users.noreply.github.com> Date: Mon, 1 Aug 2022 13:07:21 +0300 Subject: [PATCH] only allow enums to overload enums + extra test mirror behavior without overloadableEnums --- compiler/semexprs.nim | 4 ++-- tests/enum/toverloadable_enums.nim | 34 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index d3c62629d3c00..b63779185e022 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -2766,7 +2766,7 @@ proc enumFieldSymChoice(c: PContext, n: PNode, s: PSym): PNode = var i = 0 var a = initOverloadIter(o, c, n) while a != nil: - if a.kind in OverloadableSyms-{skModule}: + if a.kind == skEnumField: inc(i) if i > 1: break a = nextOverloadIter(o, c, n) @@ -2782,7 +2782,7 @@ proc enumFieldSymChoice(c: PContext, n: PNode, s: PSym): PNode = result = newNodeIT(nkClosedSymChoice, info, newTypeS(tyNone, c)) a = initOverloadIter(o, c, n) while a != nil: - if a.kind in OverloadableSyms-{skModule}: + if a.kind == skEnumField: incl(a.flags, sfUsed) markOwnerModuleAsUsed(c, a) result.add newSymNode(a, info) diff --git a/tests/enum/toverloadable_enums.nim b/tests/enum/toverloadable_enums.nim index df255dd71c94b..9bb5514674ca3 100644 --- a/tests/enum/toverloadable_enums.nim +++ b/tests/enum/toverloadable_enums.nim @@ -84,3 +84,37 @@ proc g3[T](x: T, e: E2): int = of value2: echo "E2-B" let v5 = g3(99, E2.value2) + +block: # only allow enums to overload enums + # mirrors behavior without overloadableEnums + proc foo() = discard + block: + type Foo = enum foo + doAssert foo is Foo + foo() + +import macros +block: # test with macros/templates + type + Enum1 = enum + value01, value02 + Enum2 = enum + value01, value10 + + macro isOneM(a: untyped): bool = + result = newCall(bindSym"==", a, ident"value01") + + macro isOneMS(a: untyped): bool = + result = newCall(bindSym"==", a, bindSym"value01") + + template isOneT(a: untyped): bool = + a == value01 + + let e1 = Enum1.value01 + let e2 = Enum2.value01 + doAssert isOneM(e1) + doAssert isOneM(e2) + doAssert isOneMS(e1) + doAssert isOneMS(e2) + doAssert isOneT(e1) + doAssert isOneT(e2)