Skip to content

Commit

Permalink
fix nim-lang#19795 iterator declaration zeros variable on ARC/ORC
Browse files Browse the repository at this point in the history
  • Loading branch information
bung87 committed Nov 8, 2022
1 parent 600b3a9 commit 1fed070
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/injectdestructors.nim
Expand Up @@ -61,6 +61,8 @@ template dbg(body) =
if shouldDebug:
body

template isGlobal(sym: PSym; s: Scope): bool = {sfGlobal, sfPure} <= sym.flags or sfGlobal in sym.flags and s.parent == nil

proc hasDestructor(c: Con; t: PType): bool {.inline.} =
result = ast.hasDestructor(t)
when toDebug.len > 0:
Expand Down Expand Up @@ -162,6 +164,8 @@ proc isLastRead(n: PNode; c: var Con; s: var Scope): bool =
if not hasDestructor(c, n.typ): return true

let m = skipConvDfa(n)
if m.kind == nkSym and isGlobal(m.sym, s):
return false
result = (m.kind == nkSym and sfSingleUsedTemp in m.sym.flags) or
isLastReadImpl(n, c, s)

Expand Down Expand Up @@ -490,7 +494,7 @@ proc pVarTopLevel(v: PNode; c: var Con; s: var Scope; res: PNode) =
res.add newTree(nkFastAsgn, v, genDefaultCall(v.typ, c, v.info))
elif sfThread notin v.sym.flags and sfCursor notin v.sym.flags:
# do not destroy thread vars for now at all for consistency.
if {sfGlobal, sfPure} <= v.sym.flags or sfGlobal in v.sym.flags and s.parent == nil:
if isGlobal(v.sym, s):
c.graph.globalDestructors.add c.genDestroy(v)
else:
s.final.add c.genDestroy(v)
Expand Down
11 changes: 11 additions & 0 deletions tests/arc/t19795.nim
@@ -0,0 +1,11 @@
type Node = ref object
kind: Node

var n1 = Node()
var n2 = Node()
n2.kind = n1

proc foo =
echo cast[int](n1)

foo()

0 comments on commit 1fed070

Please sign in to comment.