* fix #15609

* fix test

(cherry picked from commit fa5f225efc)
This commit is contained in:
cooldome
2020-11-05 22:29:05 +00:00
committed by narimiran
parent 554a9ad156
commit ca53629c42
2 changed files with 42 additions and 10 deletions

View File

@@ -823,14 +823,10 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode): PNode =
of nkAsgn, nkFastAsgn:
if hasDestructor(c, n[0].typ) and n[1].kind notin {nkProcDef, nkDo, nkLambda} and
not isCursor(n[0], c):
# rule (self-assignment-removal):
if n[1].kind == nkSym and n[0].kind == nkSym and n[0].sym == n[1].sym:
result = newNodeI(nkEmpty, n.info)
else:
if n[0].kind in {nkDotExpr, nkCheckedFieldExpr}:
cycleCheck(n, c)
assert n[1].kind notin {nkAsgn, nkFastAsgn}
result = moveOrCopy(p(n[0], c, s, mode), n[1], c, s)
if n[0].kind in {nkDotExpr, nkCheckedFieldExpr}:
cycleCheck(n, c)
assert n[1].kind notin {nkAsgn, nkFastAsgn}
result = moveOrCopy(p(n[0], c, s, mode), n[1], c, s)
elif isDiscriminantField(n[0]):
result = c.genDiscriminantAsgn(s, n)
else:
@@ -953,8 +949,11 @@ proc moveOrCopy(dest, ri: PNode; c: var Con; s: var Scope, isDecl = false): PNod
result = c.genSink(dest, p(ri, c, s, consumed), isDecl)
of nkObjConstr, nkTupleConstr, nkClosure, nkCharLit..nkNilLit:
result = c.genSink(dest, p(ri, c, s, consumed), isDecl)
of nkSym:
if isSinkParam(ri.sym) and isLastRead(ri, c):
of nkSym:
if dest.kind == nkSym and dest.sym == ri.sym:
# rule (self-assignment-removal):
result = newNodeI(nkEmpty, dest.info)
elif isSinkParam(ri.sym) and isLastRead(ri, c):
# Rule 3: `=sink`(x, z); wasMoved(z)
let snk = c.genSink(dest, ri, isDecl)
result = newTree(nkStmtList, snk, c.genWasMoved(ri))

View File

@@ -70,6 +70,9 @@ king
hi
try
bye
()
()
()
'''
"""
@@ -524,3 +527,33 @@ proc getScope2(): string =
"else"
echo getScope2()
#--------------------------------------------------------------------
#bug #15609
type
Wrapper = object
discard
proc newWrapper(): ref Wrapper =
new(result)
result
proc newWrapper2(a: int): ref Wrapper =
new(result)
if a > 0:
result
else:
new(Wrapper)
let w1 = newWrapper()
echo $w1[]
let w2 = newWrapper2(1)
echo $w2[]
let w3 = newWrapper2(-1)
echo $w3[]