Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only allow enums to overload enums + extra test #20126

Merged
merged 1 commit into from
Sep 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
34 changes: 34 additions & 0 deletions tests/enum/toverloadable_enums.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)