From 5dca695bcfaad7212679c80b3219e0f69afdca7d Mon Sep 17 00:00:00 2001 From: Andreas Rumpf Date: Fri, 13 Oct 2017 07:00:52 +0200 Subject: [PATCH] new destroyer pass works with procs too --- compiler/destroyer.nim | 5 ++++- compiler/semstmts.nim | 2 +- compiler/transf.nim | 8 ++++---- tests/destructor/tcustomstrings.nim | 5 ++--- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/compiler/destroyer.nim b/compiler/destroyer.nim index e8db4bc2b6..78df1bf065 100644 --- a/compiler/destroyer.nim +++ b/compiler/destroyer.nim @@ -290,4 +290,7 @@ proc injectDestructorCalls*(owner: PSym; n: PNode): PNode = else: result.add body - #echo "transformed into: ", result + when false: + echo "------------------------------------" + echo owner.name.s, " transformed from: ", n + echo result diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index b65443b2fc..3e6e918f19 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -400,7 +400,7 @@ proc addToVarSection(c: PContext; result: var PNode; orig, identDefs: PNode) = # in order for this transformation to be correct. let L = identDefs.len let value = identDefs[L-1] - if value.typ != nil and tfHasAsgn in value.typ.flags and c.p.owner.kind != skFunc: + if value.typ != nil and tfHasAsgn in value.typ.flags and not newDestructors: # the spec says we need to rewrite 'var x = T()' to 'var x: T; x = T()': identDefs.sons[L-1] = emptyNode if result.kind != nkStmtList: diff --git a/compiler/transf.nim b/compiler/transf.nim index fa468d4933..c3d12dafe2 100644 --- a/compiler/transf.nim +++ b/compiler/transf.nim @@ -971,11 +971,11 @@ proc transformBody*(module: PSym, n: PNode, prc: PSym): PNode = result = processTransf(c, result, prc) liftDefer(c, result) #result = liftLambdas(prc, result) - incl(result.flags, nfTransf) when useEffectSystem: trackProc(prc, result) if c.needsDestroyPass and newDestructors: result = injectDestructorCalls(prc, result) - #if prc.name.s == "testbody": + incl(result.flags, nfTransf) + #if prc.name.s == "testbody": # echo renderTree(result) proc transformStmt*(module: PSym, n: PNode): PNode = @@ -986,12 +986,12 @@ proc transformStmt*(module: PSym, n: PNode): PNode = result = processTransf(c, n, module) liftDefer(c, result) #result = liftLambdasForTopLevel(module, result) - incl(result.flags, nfTransf) when useEffectSystem: trackTopLevelStmt(module, result) #if n.info ?? "temp.nim": # echo renderTree(result, {renderIds}) if c.needsDestroyPass and newDestructors: result = injectDestructorCalls(module, result) + incl(result.flags, nfTransf) proc transformExpr*(module: PSym, n: PNode): PNode = if nfTransf in n.flags: @@ -1000,6 +1000,6 @@ proc transformExpr*(module: PSym, n: PNode): PNode = var c = openTransf(module, "") result = processTransf(c, n, module) liftDefer(c, result) - incl(result.flags, nfTransf) if c.needsDestroyPass and newDestructors: result = injectDestructorCalls(module, result) + incl(result.flags, nfTransf) diff --git a/tests/destructor/tcustomstrings.nim b/tests/destructor/tcustomstrings.nim index afd2fa8388..2250d47728 100644 --- a/tests/destructor/tcustomstrings.nim +++ b/tests/destructor/tcustomstrings.nim @@ -78,11 +78,11 @@ proc create*(lit: string): mystring = copyMem(addr result.data[result.len], unsafeAddr lit[0], newLen + 1) result.len = newLen -func `&`*(a, b: mystring): mystring = +proc `&`*(a, b: mystring): mystring = result = a result.add b -func main(n: int) = +proc main(n: int) = var a: mystring let b = create" to append" for i in 0..