diff options
author | metagn <metagngn@gmail.com> | 2022-09-03 10:53:31 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-03 09:53:31 +0200 |
commit | a6189fbb988ae9c9e6760cb901e792e043b9086b (patch) | |
tree | b392aab7a3f79d5d4aa4230699d56d688a35f370 | |
parent | 86f7f4ffa5b8d84cbff12afbcd9b88d3ceb429c8 (diff) | |
download | Nim-a6189fbb988ae9c9e6760cb901e792e043b9086b.tar.gz |
only allow enums to overload enums + extra test (#20126)
mirror behavior without overloadableEnums
-rw-r--r-- | compiler/semexprs.nim | 4 | ||||
-rw-r--r-- | tests/enum/toverloadable_enums.nim | 34 |
2 files changed, 36 insertions, 2 deletions
diff --git a/compiler/semexprs.nim b/compiler/semexprs.nim index 84d45b7df..42f720c48 100644 --- a/compiler/semexprs.nim +++ b/compiler/semexprs.nim @@ -2798,7 +2798,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) @@ -2814,7 +2814,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 df255dd71..9bb551467 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) |