Skip to content

Commit

Permalink
Make a copy of env for closure [C backend]
Browse files Browse the repository at this point in the history
This fixes part of the issue nim-lang#12625. vm & other backends also need
update.
  • Loading branch information
foldl committed Nov 20, 2019
1 parent 675189c commit 93c814f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2354,6 +2354,14 @@ proc genClosure(p: BProc, n: PNode, d: var TLoc) =
linefmt(p, cpsStmts, "$1.ClP_0 = $2; $1.ClE_0 = $3;$n",
[d.rdLoc, a.rdLoc, b.rdLoc])
else:
# make a copy of env (b)
if b.t.kind != tyNil:
var copy: TLoc
getTemp(p, b.t, copy)
rawGenNew(p, copy, copy.r)
genDeepCopy(p, copy, b)
b = copy

getTemp(p, n.typ, tmp)
linefmt(p, cpsStmts, "$1.ClP_0 = $2; $1.ClE_0 = $3;$n",
[tmp.rdLoc, a.rdLoc, b.rdLoc])
Expand Down
24 changes: 24 additions & 0 deletions tests/closure/tclosureenv.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
discard """
target: "c"
output: '''
2302
'''
joinable: false
"""

import sequtils

type
ft = proc (x: int): int {.noSideEffect.}

proc foo() =
let m = @[func (x: int): int = x + 100, func (x: int): int = x + 200]
var l: seq[ft] = @[]
for it in m:
l.add(func (x: int): int = it(x) + 1000)
let r = l.map(func (f: ft): int = f(1))
echo r[0] + r[1]

if isMainModule:
foo()

0 comments on commit 93c814f

Please sign in to comment.