Destructors: more moves for tuples (#9808)

This commit is contained in:
cooldome
2018-11-27 17:59:21 +00:00
committed by Andreas Rumpf
parent cc0364e72f
commit 5b98afb8a8
4 changed files with 13 additions and 4 deletions

View File

@@ -458,6 +458,11 @@ proc moveOrCopy(dest, ri: PNode; c: var Con): PNode =
else:
result = genCopy(c, dest.typ, dest, ri)
result.add p(ri, c)
of nkStmtListExpr:
result = newNodeI(nkStmtList, ri.info)
for i in 0..ri.len-2:
result.add p(ri[i], c)
result.add moveOrCopy(dest, ri[^1], c)
of nkObjConstr:
result = genSink(c, dest.typ, dest, ri)
let ri2 = copyTree(ri)

View File

@@ -78,7 +78,7 @@ proc lowerTupleUnpackingForAsgn*(g: ModuleGraph; n: PNode; owner: PSym): PNode =
let value = n.lastSon
result = newNodeI(nkStmtList, n.info)
var temp = newSym(skLet, getIdent(g.cache, "_"), owner, value.info, owner.options)
var temp = newSym(skTemp, getIdent(g.cache, "_"), owner, value.info, owner.options)
var v = newNodeI(nkLetSection, value.info)
let tempAsNode = newSymNode(temp) #newIdentNode(getIdent(genPrefix & $temp.id), value.info)

View File

@@ -204,7 +204,7 @@ proc newSymG*(kind: TSymKind, n: PNode, c: PContext): PSym =
if n.kind == nkSym:
# and sfGenSym in n.sym.flags:
result = n.sym
if result.kind != kind:
if result.kind notin {kind, skTemp}:
localError(c.config, n.info, "cannot use symbol of kind '" &
$result.kind & "' as a '" & $kind & "'")
if sfGenSym in result.flags and result.kind notin {skTemplate, skMacro, skParam}:

View File

@@ -108,7 +108,8 @@ proc myfunc(x, y: int): (MySeqNonCopyable, MySeqNonCopyable) =
result = (newMySeq(x, 1.0), newMySeq(y, 5.0))
proc myfunc2(x, y: int): tuple[a: MySeqNonCopyable, b:int, c:MySeqNonCopyable] =
(a: newMySeq(x, 1.0), b:0, c:newMySeq(y, 5.0))
var cc = newMySeq(y, 5.0)
(a: newMySeq(x, 1.0), b:0, c: cc)
let (seq1, seq2) = myfunc(2, 3)
doAssert seq1.len == 2
@@ -118,4 +119,7 @@ doAssert seq2[0] == 5.0
var (seq3, i, _) = myfunc2(2, 3)
doAssert seq3.len == 2
doAssert seq3[0] == 1.0
doAssert seq3[0] == 1.0
var seq4, seq5: MySeqNonCopyable
(seq4, i, seq5) = myfunc2(2, 3)