Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
cooldome committed Feb 12, 2020
1 parent c446c0f commit 9433d0b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
4 changes: 3 additions & 1 deletion compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const
stringCaseThreshold = 8
# above X strings a hash-switch for strings is generated

proc genVarStmt(p: BProc, n: PNode)

proc getTraverseProc(p: BProc, v: PSym): Rope =
if p.config.selectedGC in {gcMarkAndSweep, gcHooks, gcV2, gcRefc} and
optOwnedRefs notin p.config.globalOptions and
Expand Down Expand Up @@ -66,7 +68,7 @@ proc genVarTuple(p: BProc, n: PNode) =
# if we have a something that's been captured, use the lowering instead:
for i in 0..<n.len-2:
if n[i].kind != nkSym:
genStmts(p, lowerTupleUnpacking(p.module.g.graph, n, p.prc))
genVarStmt(p, lowerTupleUnpacking(p.module.g.graph, n, p.prc))
return

# check only the first son
Expand Down
15 changes: 8 additions & 7 deletions compiler/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ proc canBeMoved(c: Con; t: PType): bool {.inline.} =
result = t.attachedOps[attachedSink] != nil

proc genSink(c: var Con; dest, ri: PNode): PNode =
if isFirstWrite(dest, c): # optimize sink call into a bitwise memcopy
if isUnpackedTuple(dest) or isFirstWrite(dest, c):
# optimize sink call into a bitwise memcopy
result = newTree(nkFastAsgn, dest, ri)
else:
let t = dest.typ.skipTypes({tyGenericInst, tyAlias, tySink})
Expand Down Expand Up @@ -592,6 +593,9 @@ proc p(n: PNode; c: var Con; mode: ProcessMode): PNode =
# make sure it's destroyed at the end of the proc:
if not isUnpackedTuple(v):
c.destroys.add genDestroy(c, v)
elif c.inLoop > 0:
# unpacked tuple needs reset at every loop iteration
result.add newTree(nkFastAsgn, v, genDefaultCall(v.typ, c, v.info))
if ri.kind == nkEmpty and c.inLoop > 0:
ri = genDefaultCall(v.typ, c, v.info)
if ri.kind != nkEmpty:
Expand Down Expand Up @@ -660,14 +664,11 @@ proc p(n: PNode; c: var Con; mode: ProcessMode): PNode =
proc moveOrCopy(dest, ri: PNode; c: var Con): PNode =
case ri.kind
of nkCallKinds:
if isUnpackedTuple(dest):
result = newTree(nkFastAsgn, dest, p(ri, c, consumed))
else:
result = genSink(c, dest, p(ri, c, consumed))
result = genSink(c, dest, p(ri, c, consumed))
of nkBracketExpr:
if isUnpackedTuple(ri[0]):
# unpacking of tuple: take over elements
result = newTree(nkFastAsgn, dest, p(ri, c, consumed))
# unpacking of tuple: take over the elements
result = genSink(c, dest, p(ri, c, consumed))
elif isAnalysableFieldAccess(ri, c.owner) and isLastRead(ri, c) and
not aliases(dest, ri):
# Rule 3: `=sink`(x, z); wasMoved(z)
Expand Down
3 changes: 1 addition & 2 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1724,8 +1724,7 @@ proc genVarStmt(p: PProc, n: PNode) =
var a = n[i]
if a.kind != nkCommentStmt:
if a.kind == nkVarTuple:
let unpacked = lowerTupleUnpacking(p.module.graph, a, p.prc)
genStmt(p, unpacked)
genVarStmt(p, lowerTupleUnpacking(p.module.graph, a, p.prc))
else:
assert(a.kind == nkIdentDefs)
assert(a[0].kind == nkSym)
Expand Down
10 changes: 4 additions & 6 deletions compiler/lowerings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,18 @@ proc newFastAsgnStmt*(le, ri: PNode): PNode =
proc lowerTupleUnpacking*(g: ModuleGraph; n: PNode; owner: PSym): PNode =
assert n.kind == nkVarTuple
let value = n.lastSon
result = newNodeI(nkStmtList, n.info)

var temp = newSym(skTemp, getIdent(g.cache, genPrefix), owner, value.info, g.config.options)
temp.typ = skipTypes(value.typ, abstractInst)
incl(temp.flags, sfFromGeneric)

var v = newNodeI(nkVarSection, value.info)
result = newNodeI(nkVarSection, value.info)
let tempAsNode = newSymNode(temp)
v.addVar(tempAsNode, value)
result.add(v)
result.addVar(tempAsNode, value)

for i in 0..<n.len-2:
if n[i].kind == nkSym: v.addVar(n[i])
result.add newAsgnStmt(n[i], newTupleAccess(g, tempAsNode, i))
if n[i].kind == nkSym:
result.addVar(n[i], newTupleAccess(g, tempAsNode, i))

proc evalOnce*(g: ModuleGraph; value: PNode; owner: PSym): PNode =
## Turns (value) into (let tmp = value; tmp) so that 'value' can be re-used
Expand Down
11 changes: 11 additions & 0 deletions tests/arc/tmovebug.nim
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,14 @@ proc tbug13314 =
execute()

tbug13314()

#-------------------------------------------------------------------------
# bug #13368

import strutils
proc procStat() =
for line in @["a b", "c d", "e f"]:
let cols = line.splitWhitespace(maxSplit=1)
echo cols[0]
let (nm, rest) = (cols[0], cols[1])
procStat()

0 comments on commit 9433d0b

Please sign in to comment.