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

always lookup pure enum symbols if expected type is enum #23976

Merged
merged 1 commit into from
Aug 17, 2024
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
5 changes: 3 additions & 2 deletions compiler/lookups.nim
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,10 @@ type

const allExceptModule = {low(TSymKind)..high(TSymKind)} - {skModule, skPackage}

proc lookUpCandidates*(c: PContext, ident: PIdent, filter: set[TSymKind]): seq[PSym] =
proc lookUpCandidates*(c: PContext, ident: PIdent, filter: set[TSymKind],
includePureEnum = false): seq[PSym] =
result = searchInScopesFilterBy(c, ident, filter)
if result.len == 0:
if skEnumField in filter and (result.len == 0 or includePureEnum):
result.add allPureEnumFields(c, ident)

proc qualifiedLookUp*(c: PContext, n: PNode, flags: set[TLookupFlag]): PSym =
Expand Down
5 changes: 4 additions & 1 deletion compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3107,7 +3107,10 @@ proc resolveIdentToSym(c: PContext, n: PNode, resultNode: var PNode,
if efNoEvaluateGeneric in flags or expectedType != nil:
# `a[...]` where `a` is a module or package is not possible
filter.excl {skModule, skPackage}
let candidates = lookUpCandidates(c, ident, filter)
let includePureEnum = expectedType != nil and
expectedType.skipTypes(abstractRange-{tyDistinct}).kind == tyEnum
let candidates = lookUpCandidates(c, ident, filter,
includePureEnum = includePureEnum)
if candidates.len == 0:
result = errorUndeclaredIdentifierHint(c, ident, n.info)
elif candidates.len == 1 or {efNoEvaluateGeneric, efInCall} * flags != {}:
Expand Down
13 changes: 13 additions & 0 deletions tests/enum/ttypenameconflict.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# issue #23689

type
MyEnum {.pure.} = enum
A, B, C, D

B = object
field: int

let x: MyEnum = B
doAssert $x == "B"
doAssert typeof(x) is MyEnum
doAssert x in {A, B}