fixes #3995
This commit is contained in:
parent
e2671aa401
commit
5757ad858c
3 changed files with 38 additions and 4 deletions
|
|
@ -1856,10 +1856,16 @@ proc genClosure(p: BProc, n: PNode, d: var TLoc) =
|
||||||
initLocExpr(p, n.sons[1], b)
|
initLocExpr(p, n.sons[1], b)
|
||||||
if n.sons[0].skipConv.kind == nkClosure:
|
if n.sons[0].skipConv.kind == nkClosure:
|
||||||
internalError(n.info, "closure to closure created")
|
internalError(n.info, "closure to closure created")
|
||||||
getTemp(p, n.typ, tmp)
|
# tasyncawait.nim breaks with this optimization:
|
||||||
linefmt(p, cpsStmts, "$1.ClPrc = $2; $1.ClEnv = $3;$n",
|
when false:
|
||||||
tmp.rdLoc, a.rdLoc, b.rdLoc)
|
if d.k != locNone:
|
||||||
putLocIntoDest(p, d, tmp)
|
linefmt(p, cpsStmts, "$1.ClPrc = $2; $1.ClEnv = $3;$n",
|
||||||
|
d.rdLoc, a.rdLoc, b.rdLoc)
|
||||||
|
else:
|
||||||
|
getTemp(p, n.typ, tmp)
|
||||||
|
linefmt(p, cpsStmts, "$1.ClPrc = $2; $1.ClEnv = $3;$n",
|
||||||
|
tmp.rdLoc, a.rdLoc, b.rdLoc)
|
||||||
|
putLocIntoDest(p, d, tmp)
|
||||||
|
|
||||||
proc genArrayConstr(p: BProc, n: PNode, d: var TLoc) =
|
proc genArrayConstr(p: BProc, n: PNode, d: var TLoc) =
|
||||||
var arr: TLoc
|
var arr: TLoc
|
||||||
|
|
|
||||||
|
|
@ -721,6 +721,10 @@ proc liftCapturedVars(n: PNode; owner: PSym; d: DetectionPass;
|
||||||
let m = newSymNode(n[namePos].sym)
|
let m = newSymNode(n[namePos].sym)
|
||||||
m.typ = n.typ
|
m.typ = n.typ
|
||||||
result = liftCapturedVars(m, owner, d, c)
|
result = liftCapturedVars(m, owner, d, c)
|
||||||
|
of nkHiddenStdConv:
|
||||||
|
if n.len == 2:
|
||||||
|
n.sons[1] = liftCapturedVars(n[1], owner, d, c)
|
||||||
|
if n[1].kind == nkClosure: result = n[1]
|
||||||
else:
|
else:
|
||||||
if owner.isIterator:
|
if owner.isIterator:
|
||||||
if n.kind == nkYieldStmt:
|
if n.kind == nkYieldStmt:
|
||||||
|
|
|
||||||
24
tests/closure/tflatmap.nim
Normal file
24
tests/closure/tflatmap.nim
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
|
||||||
|
# bug #3995
|
||||||
|
|
||||||
|
import future
|
||||||
|
|
||||||
|
type
|
||||||
|
RNG* = tuple[]
|
||||||
|
Rand*[A] = (RNG) -> (A, RNG)
|
||||||
|
|
||||||
|
proc nextInt*(r: RNG): (int, RNG) =
|
||||||
|
(1, ())
|
||||||
|
|
||||||
|
proc flatMap[A,B](f: Rand[A], g: A -> Rand[B]): Rand[B] =
|
||||||
|
(rng: RNG) => (
|
||||||
|
let (a, rng2) = f(rng);
|
||||||
|
let g1 = g(a);
|
||||||
|
g1(rng2)
|
||||||
|
)
|
||||||
|
|
||||||
|
proc map[A,B](s: Rand[A], f: A -> B): Rand[B] =
|
||||||
|
let g: A -> Rand[B] = (a: A) => ((rng: RNG) => (f(a), rng))
|
||||||
|
flatMap(s, g)
|
||||||
|
|
||||||
|
let f = nextInt.map(i => i - i mod 2)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue