ARC: closure bugfixes (#12677)
* ARC: closure bugfixes * progress * ARC closures: create =hooks for captured parameters * ARC: always destroy constructions like tuples, arrays properly, even in edge cases * fixes a regression
This commit is contained in:
parent
bab5351d43
commit
5278cf80eb
4 changed files with 72 additions and 16 deletions
|
|
@ -271,7 +271,7 @@ proc sinkParamIsLastReadCheck(c: var Con, s: PNode) =
|
|||
localError(c.graph.config, c.otherRead.info, "sink parameter `" & $s.sym.name.s &
|
||||
"` is already consumed at " & toFileLineCol(c. graph.config, s.info))
|
||||
|
||||
proc p(n: PNode; c: var Con): PNode
|
||||
proc p(n: PNode; c: var Con; consumed = false): PNode
|
||||
proc pArg(arg: PNode; c: var Con; isSink: bool): PNode
|
||||
proc moveOrCopy(dest, ri: PNode; c: var Con): PNode
|
||||
|
||||
|
|
@ -366,6 +366,22 @@ template handleNested(n: untyped, processCall: untyped) =
|
|||
result.add branch
|
||||
else: assert(false)
|
||||
|
||||
proc ensureDestruction(arg: PNode; c: var Con): PNode =
|
||||
# it can happen that we need to destroy expression contructors
|
||||
# like [], (), closures explicitly in order to not leak them.
|
||||
if arg.typ != nil and hasDestructor(arg.typ):
|
||||
# produce temp creation for (fn, env). But we need to move 'env'?
|
||||
# This was already done in the sink parameter handling logic.
|
||||
result = newNodeIT(nkStmtListExpr, arg.info, arg.typ)
|
||||
let tmp = getTemp(c, arg.typ, arg.info)
|
||||
var sinkExpr = genSink(c, tmp, arg)
|
||||
sinkExpr.add arg
|
||||
result.add sinkExpr
|
||||
result.add tmp
|
||||
c.destroys.add genDestroy(c, tmp)
|
||||
else:
|
||||
result = arg
|
||||
|
||||
proc pArg(arg: PNode; c: var Con; isSink: bool): PNode =
|
||||
if isSink:
|
||||
if arg.kind in nkCallKinds:
|
||||
|
|
@ -374,7 +390,7 @@ proc pArg(arg: PNode; c: var Con; isSink: bool): PNode =
|
|||
result = copyNode(arg)
|
||||
let parameters = arg[0].typ
|
||||
let L = if parameters != nil: parameters.len else: 0
|
||||
result.add arg[0]
|
||||
result.add pArg(arg[0], c, isSink = false)
|
||||
for i in 1..<arg.len:
|
||||
result.add pArg(arg[i], c, i < L and isSinkTypeForParam(parameters[i]))
|
||||
elif arg.containsConstSeq:
|
||||
|
|
@ -392,6 +408,9 @@ proc pArg(arg: PNode; c: var Con; isSink: bool): PNode =
|
|||
result[i][1] = pArg(arg[i][1], c, isSink = true)
|
||||
else:
|
||||
result[i] = pArg(arg[i], c, isSink = true)
|
||||
#if arg.kind == nkClosure:
|
||||
# result = handleClosureCall(result, c)
|
||||
# Not required here because the nkClosure will be consumed!
|
||||
elif arg.kind == nkSym and isSinkParam(arg.sym):
|
||||
# Sinked params can be consumed only once. We need to reset the memory
|
||||
# to disable the destructor which we have not elided
|
||||
|
|
@ -442,7 +461,7 @@ proc isCursor(n: PNode): bool =
|
|||
else:
|
||||
result = false
|
||||
|
||||
proc p(n: PNode; c: var Con): PNode =
|
||||
proc p(n: PNode; c: var Con; consumed = false): PNode =
|
||||
case n.kind
|
||||
of nkCallKinds:
|
||||
let parameters = n[0].typ
|
||||
|
|
@ -454,6 +473,8 @@ proc p(n: PNode; c: var Con): PNode =
|
|||
if c.graph.config.selectedGC in {gcHooks, gcDestructors}:
|
||||
let destroyOld = genDestroy(c, result[1])
|
||||
result = newTree(nkStmtList, destroyOld, result)
|
||||
else:
|
||||
result[0] = pArg(result[0], c, isSink = false)
|
||||
of nkDiscardStmt: # Small optimization
|
||||
if n[0].kind != nkEmpty:
|
||||
n[0] = pArg(n[0], c, false)
|
||||
|
|
@ -464,12 +485,16 @@ proc p(n: PNode; c: var Con): PNode =
|
|||
# everything that is passed to an array constructor is consumed,
|
||||
# so these all act like 'sink' parameters:
|
||||
result[i] = pArg(n[i], c, isSink = true)
|
||||
if not consumed:
|
||||
result = ensureDestruction(result, c)
|
||||
of nkObjConstr:
|
||||
result = copyTree(n)
|
||||
for i in 1..<n.len:
|
||||
# everything that is passed to an object constructor is consumed,
|
||||
# so these all act like 'sink' parameters:
|
||||
result[i][1] = pArg(n[i][1], c, isSink = true)
|
||||
if not consumed:
|
||||
result = ensureDestruction(result, c)
|
||||
of nkTupleConstr, nkClosure:
|
||||
result = copyTree(n)
|
||||
for i in ord(n.kind == nkClosure)..<n.len:
|
||||
|
|
@ -479,6 +504,8 @@ proc p(n: PNode; c: var Con): PNode =
|
|||
result[i][1] = pArg(n[i][1], c, isSink = true)
|
||||
else:
|
||||
result[i] = pArg(n[i], c, isSink = true)
|
||||
if not consumed:
|
||||
result = ensureDestruction(result, c)
|
||||
of nkVarSection, nkLetSection:
|
||||
# transform; var x = y to var x; x op y where op is a move or copy
|
||||
result = newNodeI(nkStmtList, n.info)
|
||||
|
|
@ -556,11 +583,11 @@ proc p(n: PNode; c: var Con): PNode =
|
|||
result = copyTree(n)
|
||||
result[1][0] = p(result[1][0], c)
|
||||
of nkStmtList, nkStmtListExpr, nkBlockStmt, nkBlockExpr, nkIfStmt, nkIfExpr, nkCaseStmt:
|
||||
handleNested(n): p(node, c)
|
||||
handleNested(n): p(node, c, consumed)
|
||||
else:
|
||||
result = shallowCopy(n)
|
||||
for i in 0..<n.len:
|
||||
result[i] = p(n[i], c)
|
||||
result[i] = p(n[i], c, consumed)
|
||||
|
||||
proc moveOrCopy(dest, ri: PNode; c: var Con): PNode =
|
||||
# unfortunately, this needs to be kept consistent with the cases
|
||||
|
|
@ -571,12 +598,12 @@ proc moveOrCopy(dest, ri: PNode; c: var Con): PNode =
|
|||
case ri.kind
|
||||
of nkCallKinds:
|
||||
result = genSink(c, dest, ri)
|
||||
result.add p(ri, c)
|
||||
result.add p(ri, c, consumed = true)
|
||||
of nkBracketExpr:
|
||||
if ri[0].kind == nkSym and isUnpackedTuple(ri[0].sym):
|
||||
# unpacking of tuple: move out the elements
|
||||
result = genSink(c, dest, ri)
|
||||
result.add p(ri, c)
|
||||
result.add p(ri, c, consumed = true)
|
||||
elif isAnalysableFieldAccess(ri, c.owner) and isLastRead(ri, c):
|
||||
# Rule 3: `=sink`(x, z); wasMoved(z)
|
||||
var snk = genSink(c, dest, ri)
|
||||
|
|
@ -584,17 +611,17 @@ proc moveOrCopy(dest, ri: PNode; c: var Con): PNode =
|
|||
result = newTree(nkStmtList, snk, genWasMoved(ri, c))
|
||||
else:
|
||||
result = genCopy(c, dest, ri)
|
||||
result.add p(ri, c)
|
||||
result.add p(ri, c, consumed = true)
|
||||
of nkBracket:
|
||||
# array constructor
|
||||
if ri.len > 0 and isDangerousSeq(ri.typ):
|
||||
result = genCopy(c, dest, ri)
|
||||
else:
|
||||
result = genSink(c, dest, ri)
|
||||
result.add p(ri, c)
|
||||
result.add p(ri, c, consumed = true)
|
||||
of nkObjConstr, nkTupleConstr, nkClosure, nkCharLit..nkNilLit:
|
||||
result = genSink(c, dest, ri)
|
||||
result.add p(ri, c)
|
||||
result.add p(ri, c, consumed = true)
|
||||
of nkSym:
|
||||
if isSinkParam(ri.sym):
|
||||
# Rule 3: `=sink`(x, z); wasMoved(z)
|
||||
|
|
@ -610,7 +637,7 @@ proc moveOrCopy(dest, ri: PNode; c: var Con): PNode =
|
|||
result = newTree(nkStmtList, snk, genWasMoved(ri, c))
|
||||
else:
|
||||
result = genCopy(c, dest, ri)
|
||||
result.add p(ri, c)
|
||||
result.add p(ri, c, consumed = true)
|
||||
of nkHiddenSubConv, nkHiddenStdConv, nkConv:
|
||||
result = moveOrCopy(dest, ri[1], c)
|
||||
if not sameType(ri.typ, ri[1].typ):
|
||||
|
|
@ -633,7 +660,7 @@ proc moveOrCopy(dest, ri: PNode; c: var Con): PNode =
|
|||
result = newTree(nkStmtList, snk, genWasMoved(ri, c))
|
||||
else:
|
||||
result = genCopy(c, dest, ri)
|
||||
result.add p(ri, c)
|
||||
result.add p(ri, c, consumed = true)
|
||||
|
||||
proc computeUninit(c: var Con) =
|
||||
if not c.uninitComputed:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue