Skip to content

Commit

Permalink
fixes a critical =trace generation bug (see test case) (nim-lang#14140)
Browse files Browse the repository at this point in the history
  • Loading branch information
Araq authored and EchoPouet committed Jun 13, 2020
1 parent 719d792 commit f0634aa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion compiler/liftdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,13 @@ proc newOpCall(op: PSym; x: PNode): PNode =
proc newDeepCopyCall(op: PSym; x, y: PNode): PNode =
result = newAsgnStmt(x, newOpCall(op, y))

proc usesBuiltinArc(t: PType): bool =
proc wrap(t: PType): bool {.nimcall.} = ast.isGCedMem(t)
result = types.searchTypeFor(t, wrap)

proc useNoGc(c: TLiftCtx; t: PType): bool {.inline.} =
result = optSeqDestructors in c.g.config.globalOptions and
({tfHasGCedMem, tfHasOwned} * t.flags != {} or t.isGCedMem)
({tfHasGCedMem, tfHasOwned} * t.flags != {} or usesBuiltinArc(t))

proc requiresDestructor(c: TLiftCtx; t: PType): bool {.inline.} =
result = optSeqDestructors in c.g.config.globalOptions and
Expand Down
2 changes: 1 addition & 1 deletion compiler/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ proc searchTypeForAux(t: PType, predicate: TTypePredicate,
else:
discard

proc searchTypeFor(t: PType, predicate: TTypePredicate): bool =
proc searchTypeFor*(t: PType, predicate: TTypePredicate): bool =
var marker = initIntSet()
result = searchTypeForAux(t, predicate, marker)

Expand Down
16 changes: 16 additions & 0 deletions tests/destructor/tcycle2.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,24 @@ proc main(x: int) =
let m = n
n.kids.add m

type
NodeA = ref object
s: char
a: array[3, NodeA]

proc m: NodeA =
result = NodeA(s: 'a')
result.a[0] = result
result.a[1] = result
result.a[2] = result

proc mainA =
for i in 0..10:
discard m()

let mem = getOccupiedMem()
main(90)
mainA()
GC_fullCollect()

echo "MEM ", getOccupiedMem() - mem

0 comments on commit f0634aa

Please sign in to comment.