summary refs log tree commit diff stats
path: root/compiler
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2012-09-23 00:52:34 +0200
committerAraq <rumpf_a@web.de>2012-09-23 00:52:34 +0200
commit603dc36008150d9a6d2b033fb863596e71cb8b04 (patch)
treed1e62fb10633718bfce58b9765f1e41ad6deee0f /compiler
parentea4435544e16b6fa32efaded59acc255aeae104a (diff)
downloadNim-603dc36008150d9a6d2b033fb863596e71cb8b04.tar.gz
bugfix: 'result' cannot be captured in a closure
Diffstat (limited to 'compiler')
-rw-r--r--compiler/lambdalifting.nim7
-rwxr-xr-xcompiler/semstmts.nim5
2 files changed, 7 insertions, 5 deletions
diff --git a/compiler/lambdalifting.nim b/compiler/lambdalifting.nim
index 0748b99b3..5896615c0 100644
--- a/compiler/lambdalifting.nim
+++ b/compiler/lambdalifting.nim
@@ -538,9 +538,6 @@ proc liftLambdas*(fn: PSym, body: PNode): PNode =
   if body.kind == nkEmpty or gCmd == cmdCompileToEcmaScript:
     # ignore forward declaration:
     result = body
-  elif not containsNode(body, procDefs) and false:
-    # fast path: no inner procs, so no closure needed:
-    result = body
   else:
     var o = newOuterContext(fn)
     let ex = closureCreationPoint(body)
@@ -552,6 +549,10 @@ proc liftLambdas*(fn: PSym, body: PNode): PNode =
         InternalError(params.info, "liftLambdas: strange params")
       let param = params.sons[i].sym
       IdTablePut(o.localsToEnv, param, o.currentEnv)
+    # put the 'result' into the environment so it can be captured:
+    let ast = fn.ast
+    if resultPos < sonsLen(ast) and ast.sons[resultPos].kind == nkSym:
+      IdTablePut(o.localsToEnv, ast.sons[resultPos].sym, o.currentEnv)
     searchForInnerProcs(o, body)
     discard transformOuterProc(o, body)
     result = ex
diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim
index 8a82e629a..2bd5898a7 100755
--- a/compiler/semstmts.nim
+++ b/compiler/semstmts.nim
@@ -661,7 +661,7 @@ proc semLambda(c: PContext, n: PNode): PNode =
       LocalError(n.sons[bodyPos].info, errImplOfXNotAllowed, s.name.s)
     pushProcCon(c, s)
     addResult(c, s.typ.sons[0], n.info, skProc)
-    let semBody = hloBody(c, semStmtScope(c, n.sons[bodyPos]))
+    let semBody = hloBody(c, semProcBody(c, n.sons[bodyPos]))
     n.sons[bodyPos] = transformBody(c.module, semBody, s)
     addResultNode(c, n)
     popProcCon(c)
@@ -765,13 +765,14 @@ proc semProcAux(c: PContext, n: PNode, kind: TSymKind,
       pushProcCon(c, s)
       if s.typ.sons[0] != nil and kind != skIterator: 
         addResult(c, s.typ.sons[0], n.info, kind)
+        addResultNode(c, n)
       if sfImportc notin s.flags:
         # no semantic checking for importc:
         let semBody = hloBody(c, semProcBody(c, n.sons[bodyPos]))
         # unfortunately we cannot skip this step when in 'system.compiles'
         # context as it may even be evaluated in 'system.compiles':
         n.sons[bodyPos] = transformBody(c.module, semBody, s)
-      if s.typ.sons[0] != nil and kind != skIterator: addResultNode(c, n)
+      #if s.typ.sons[0] != nil and kind != skIterator: addResultNode(c, n)
       popProcCon(c)
     else: 
       if s.typ.sons[0] != nil and kind != skIterator: