only allow enums to overload enums + extra test (#20126)

mirror behavior without overloadableEnums
This commit is contained in:
metagn
2022-09-03 10:53:31 +03:00
committed by GitHub
parent 86f7f4ffa5
commit a6189fbb98
2 changed files with 36 additions and 2 deletions

View File

@@ -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)

View File

@@ -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)