Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* fix nim-lang#15609

* fix test
  • Loading branch information
cooldome authored and ardek66 committed Mar 26, 2021
1 parent a1b69f7 commit bb69512
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
19 changes: 9 additions & 10 deletions compiler/injectdestructors.nim
Original file line number Diff line number Diff line change
Expand Up @@ -824,14 +824,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:
Expand Down Expand Up @@ -954,8 +950,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))
Expand Down
33 changes: 33 additions & 0 deletions tests/arc/tmovebug.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ king
hi
try
bye
()
()
()
'''
"""

Expand Down Expand Up @@ -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[]

0 comments on commit bb69512

Please sign in to comment.