From d43a5954c5e179c5ef270bb7b48bcab7288ddba5 Mon Sep 17 00:00:00 2001 From: metagn Date: Fri, 16 Aug 2024 07:33:43 +0300 Subject: [PATCH] remove nontoplevel type hack + consider symbol disamb in type hash (#23969) fixes #22571 Removes the hack added in #13589 which made non-top-level object type symbols `gensym` because they couldn't be mangled into different names for codegen vs. top-level types. Now we consider the new `disamb` field (added in #21667) of the type symbols in the type hash (which is used for the mangled name) to differentiate between the types. In other parts of the compiler, specifically the [proc mangling](https://github.com/nim-lang/Nim/blob/298ada3412c9cf5971abc2b3b891b9bb8612e170/compiler/mangleutils.nim#L59), `itemId.item` is used instead of the `disamb` field, but I didn't use it in case it's the outdated method. --- compiler/semstmts.nim | 4 ---- compiler/sighashes.nim | 4 ++++ tests/ccgbugs/tsamename3.nim | 9 +++++++++ 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 915f2dcb6d..5e7aabe017 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -1785,10 +1785,6 @@ proc typeSectionFinalPass(c: PContext, n: PNode) = checkForMetaFields(c, baseType.n, hasError) if not hasError: checkConstructedType(c.config, s.info, s.typ) - - # fix bug #5170, bug #17162, bug #15526: ensure locally scoped types get a unique name: - if s.typ.kind in {tyEnum, tyRef, tyObject} and not isTopLevel(c): - incl(s.flags, sfGenSym) #instAllTypeBoundOp(c, n.info) diff --git a/compiler/sighashes.nim b/compiler/sighashes.nim index f86f866794..d8dfe1828b 100644 --- a/compiler/sighashes.nim +++ b/compiler/sighashes.nim @@ -55,6 +55,8 @@ proc hashSym(c: var MD5Context, s: PSym) = c &= it.name.s c &= "." it = it.owner + c &= "#" + c &= s.disamb proc hashTypeSym(c: var MD5Context, s: PSym; conf: ConfigRef) = if sfAnon in s.flags or s.kind == skGenericParam: @@ -69,6 +71,8 @@ proc hashTypeSym(c: var MD5Context, s: PSym; conf: ConfigRef) = c &= it.name.s c &= "." it = it.owner + c &= "#" + c &= s.disamb proc hashTree(c: var MD5Context, n: PNode; flags: set[ConsiderFlag]; conf: ConfigRef) = if n == nil: diff --git a/tests/ccgbugs/tsamename3.nim b/tests/ccgbugs/tsamename3.nim index a69391e5c7..ded18e9f8f 100644 --- a/tests/ccgbugs/tsamename3.nim +++ b/tests/ccgbugs/tsamename3.nim @@ -109,3 +109,12 @@ block: # make sure `hashType` doesn't recurse infinitely a, b: PFoo c: int var a: PFoo + +block: # issue #22571 + macro foo(x: typed) = + result = x + + block: # or `proc main =` + foo: + type Foo = object + doAssert $Foo() == "()"