destructors: optimize more assignments into moves; also fixes #9294

This commit is contained in:
Andreas Rumpf 2018-10-12 20:36:42 +02:00
commit fcca59e415
5 changed files with 143 additions and 9 deletions

View file

@ -19,9 +19,12 @@ proc `=`(lhs: var T, rhs: T) =
proc `=destroy`(v: var T) =
echo "destroy"
proc use(x: T) = discard
proc usedToBeBlock =
var v1 : T
var v2 : T = v1
use v1
usedToBeBlock()

View file

@ -1,6 +1,7 @@
discard """
output: '''after 3 3
after 3 3
output: '''after 2 2
after 2 2
after 2 2
after 2 2'''
"""
# bug #9263
@ -107,6 +108,11 @@ proc test3 =
# a = a - b
b = -b + a
proc test4 =
# bug #9294
var a = matrix(5, 5, 1.0)
a = -a + a
test1()
info()
@ -115,3 +121,6 @@ info()
test3()
info()
test4()
info()

View file

@ -90,6 +90,8 @@ proc write(t: opt[Tree]) =
write stdout, it.data, "\n"
write(it.ri)
proc use(t: opt[Tree]) = discard
proc main =
var t: opt[Tree]
insert t, 60.0
@ -99,6 +101,7 @@ proc main =
write t
let copy = t
write copy
use t
main()
echo allocCount, " ", deallocCount

View file

@ -0,0 +1,48 @@
discard """
errormsg: "'=' is not available for type <Foo>; requires a copy because it's not the last read of 'otherTree'"
line: 44
"""
type
Foo = object
x: int
proc `=destroy`(f: var Foo) = f.x = 0
proc `=`(a: var Foo; b: Foo) {.error.} # = a.x = b.x
proc `=sink`(a: var Foo; b: Foo) = a.x = b.x
proc createTree(x: int): Foo =
Foo(x: x)
proc take2(a, b: sink Foo) =
echo a.x, " ", b.x
proc allowThis() =
var otherTree: Foo
for i in 0..3:
while true:
#if i == 0:
otherTree = createTree(44)
case i
of 0:
echo otherTree
take2(createTree(34), otherTree)
of 1:
take2(createTree(34), otherTree)
else:
discard
proc preventThis() =
var otherTree: Foo
for i in 0..3:
while true:
if i == 0:
otherTree = createTree(44)
case i
of 0:
echo otherTree
take2(createTree(34), otherTree)
of 1:
take2(createTree(34), otherTree)
else:
discard