Skip to content

Commit

Permalink
remove bad type inference behavior for enum identifiers (#23588)
Browse files Browse the repository at this point in the history
refs
#23586 (comment)

In #20091 a bad kind of type inference was mistakenly left in where if
an identifier `abc` had an expected type of an enum type `Enum`, and
`Enum` had a member called `abc`, the identifier would change to be that
enum member. This causes bugs where a local symbol can have the same
name as an enum member but have a different value. I had assumed this
behavior was removed since but it wasn't, and CI seems to pass having it
removed.

A separate PR needs to be made for the 2.0 branch because these lines
were moved around during a refactoring in #23123 which is not in 2.0.
  • Loading branch information
metagn committed May 10, 2024
1 parent 1eb9aac commit c101490
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
7 changes: 0 additions & 7 deletions compiler/semexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3028,13 +3028,6 @@ proc resolveIdentToSym(c: PContext, n: PNode, resultNode: var PNode,
flags: TExprFlags, expectedType: PType): PSym =
# result is nil on error or if a node that can't produce a sym is resolved
let ident = considerQuotedIdent(c, n)
if expectedType != nil and (
let expected = expectedType.skipTypes(abstractRange-{tyDistinct});
expected.kind == tyEnum):
let nameId = ident.id
for f in expected.n:
if f.kind == nkSym and f.sym.name.id == nameId:
return f.sym
var filter = {low(TSymKind)..high(TSymKind)}
if efNoEvaluateGeneric in flags:
# `a[...]` where `a` is a module or package is not possible
Expand Down
22 changes: 22 additions & 0 deletions tests/lookups/tenumlocalsym.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
block:
type Enum = enum a, b

block:
let a = b
let x: Enum = a
doAssert x == b

block:
type
Enum = enum
a = 2
b = 10

iterator items2(): Enum =
for a in [a, b]:
yield a

var s = newSeq[Enum]()
for i in items2():
s.add i
doAssert s == @[a, b]

0 comments on commit c101490

Please sign in to comment.