injectdestructors fixes and refactor (#14964)

* injectdestructors fixes and refactor

* Tiny cleanup

* Refactor and expand testcase

* Closes #14902 by adding testcase

* Better naming

* Fix test failures

* Misc cleanup

* Add testcase for #14968

* Better approach; expand testcases

* Optimizations and fixes

* Add testcase

* typo

* Tiny cleanup
This commit is contained in:
Clyybber 2020-07-14 14:15:39 +02:00 • committed by GitHub
commit 2b0e336c97
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 317 additions and 198 deletions

View file

@ -3,6 +3,11 @@ discard """
123xyzabc
destroyed: false
destroyed: false
destroyed2: false
destroyed2: false
destroying variable: 2
destroying variable: 1
whiley ends :(
1
(x: "0")
(x: "1")
@ -17,7 +22,8 @@ destroyed: false
(x: "10")
0
closed
destroying variable
destroying variable: 20
destroying variable: 10
'''
cmd: "nim c --gc:arc $file"
"""
@ -40,11 +46,12 @@ type Variable = ref object
value: int
proc `=destroy`(self: var typeof(Variable()[])) =
echo "destroying variable"
echo "destroying variable: ",self.value
proc newVariable(value: int): Variable =
result = Variable()
result.value = value
#echo "creating variable: ",result.value
proc test(count: int) =
var v {.global.} = newVariable(10)
@ -57,6 +64,28 @@ proc test(count: int) =
test(3)
proc test2(count: int) =
#block: #XXX: Fails with block currently
var v {.global.} = newVariable(20)
var count = count - 1
if count == 0: return
test2(count)
echo "destroyed2: ", v.isNil
test2(3)
proc whiley =
var a = newVariable(1)
while true:
var b = newVariable(2)
if true: raise newException(CatchableError, "test")
try:
whiley()
except CatchableError:
echo "whiley ends :("
#------------------------------------------------------------------------------
# issue #13810
@ -209,3 +238,8 @@ proc setParent(self: SimpleLoopB, parent: SimpleLoopB) =
var l = SimpleLoopB()
l.setParent(l)
# bug #14968
import times
let currentTime = now().utc

View file

@ -1,12 +1,12 @@
discard """
output: '''begin A
elif
destroyed
end A
destroyed
begin false
if
destroyed
end false
destroyed
begin true
if
end true

View file

@ -40,6 +40,33 @@ sink me (not sink)
sinked and not optimized to a bitcopy
sinked and not optimized to a bitcopy
sinked and not optimized to a bitcopy
(data: @[0, 0])
(data: @[0, 0])
(data: @[0, 0])
(data: @[0, 0])
(data: @[0, 0])
(data: @[0, 0])
(data: @[0, 0])
100
hey
hey
(a: "a", b: 2)
ho
(a: "b", b: 3)
(b: "b", a: 2)
ho
(b: "a", a: 3)
hey
break
break
hey
ho
hey
ho
ho
king
live long; long live
king
'''
"""
@ -325,3 +352,145 @@ proc update() =
for i in 1..3:
update()
# bug #14961
type
Foo = object
data: seq[int]
proc initFoo(len: int): Foo =
result = (let s = newSeq[int](len); Foo(data: s) )
var f = initFoo(2)
echo initFoo(2)
proc initFoo2(len: int) =
echo if true:
let s = newSeq[int](len); Foo(data: s)
else:
let s = newSeq[int](len); Foo(data: s)
initFoo2(2)
proc initFoo3(len: int) =
echo (block:
let s = newSeq[int](len); Foo(data: s))
initFoo3(2)
proc initFoo4(len: int) =
echo (let s = newSeq[int](len); Foo(data: s))
initFoo4(2)
proc initFoo5(len: int) =
echo (case true
of true:
let s = newSeq[int](len); Foo(data: s)
of false:
let s = newSeq[int](len); Foo(data: s))
initFoo5(2)
proc initFoo6(len: int) =
echo (block:
try:
let s = newSeq[int](len); Foo(data: s)
finally: discard)
initFoo6(2)
proc initFoo7(len: int) =
echo (block:
try:
raise newException(CatchableError, "sup")
let s = newSeq[int](len); Foo(data: s)
except CatchableError:
let s = newSeq[int](len); Foo(data: s) )
initFoo7(2)
# bug #14902
iterator zip[T](s: openarray[T]): (T, T) =
var i = 0
while i < 10:
yield (s[i mod 2], s[i mod 2 + 1])
inc i
var lastMem = int.high
proc leak =
const len = 10
var x = @[newString(len), newString(len), newString(len)]
var c = 0
for (a, b) in zip(x):
let newMem = getOccupiedMem()
assert newMem <= lastMem
lastMem = newMem
c += a.len
echo c
leak()
proc consume(a: sink string) = echo a
proc weirdScopes =
if (let a = "hey"; a.len > 0):
echo a
while (let a = "hey"; a.len > 0):
echo a
break
var a = block: (a: "a", b: 2)
echo a
(discard; a) = (echo "ho"; (a: "b", b: 3))
echo a
var b = try: (b: "b", a: 2)
except: raise
echo b
(discard; b) = (echo "ho"; (b: "a", a: 3))
echo b
var s = "break"
consume((echo "hey"; s))
echo s
echo (block:
var a = "hey"
(echo "hey"; "ho"))
var b2 = "ho"
echo (block:
var a = "hey"
(echo "hey"; b2))
echo b2
type status = enum
alive
var king = "king"
echo (block:
var a = "a"
when true:
var b = "b"
case alive
of alive:
try:
var c = "c"
if true:
king
else:
"the abyss"
except:
echo "he ded"
"dead king")
echo "live long; long live"
echo king
weirdScopes()

View file

@ -2,7 +2,6 @@
discard """
output: '''test created
test destroyed 0
Pony is dying!
1
2
3