type with same name in different scope now works (#17710)

* type with same name in different scope now works
* fix tests/enum/tenum.nim which was wrong because it was affected by this bug
This commit is contained in:
Timothee Cour 2021-04-13 23:50:15 -07:00 • committed by GitHub
commit 840e13deb7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 133 additions and 9 deletions

View file

@ -151,15 +151,24 @@ proc hashType(c: var MD5Context, t: PType; flags: set[ConsiderFlag]) =
c.hashTypeSym(t.sym)
else:
c.hashSym(t.sym)
if {sfAnon, sfGenSym} * t.sym.flags != {}:
var symWithFlags: PSym
template hasFlag(sym): bool =
let ret = {sfAnon, sfGenSym} * sym.flags != {}
if ret: symWithFlags = sym
ret
if hasFlag(t.sym) or (t.kind == tyObject and t.owner.kind == skType and t.owner.typ.kind == tyRef and hasFlag(t.owner)):
# for `PFoo:ObjectType`, arising from `type PFoo = ref object`
# Generated object names can be identical, so we need to
# disambiguate furthermore by hashing the field types and names.
if t.n.len > 0:
let oldFlags = t.sym.flags
# Mild hack to prevent endless recursion.
t.sym.flags.excl {sfAnon, sfGenSym}
let oldFlags = symWithFlags.flags
# Hack to prevent endless recursion
# xxx intead, use a hash table to indicate we've already visited a type, which
# would also be more efficient.
symWithFlags.flags.excl {sfAnon, sfGenSym}
hashTree(c, t.n, flags + {CoHashTypeInsideNode})
t.sym.flags = oldFlags
symWithFlags.flags = oldFlags
else:
# The object has no fields: we _must_ add something here in order to
# make the hash different from the one we produce by hashing only the