fixes #19291; implements wasMoved hook (#21303)

* fixes #19291; implements `wasMoved` hook

* basics

* checkpoint

* finish `wasMoved`

* add a test for #19291

* add documentation and changelog

* work `attachedWasMoved` with generics

* fixes optimizer

* register `=wasMoved`

* handle wasMoved magcis

* check another round

* some patches

* try `op == nil`

* nicer

* generate `wasMoved` before `destroy`

* try again

* fixes tests

* default wasMoved

* Update tests/destructor/tv2_cast.nim

* Update tests/destructor/tv2_cast.nim

* Update tests/arc/topt_refcursors.nim
This commit is contained in:
ringabout 2023-03-02 12:29:40 +08:00 • committed by GitHub
commit a137e50150
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 131 additions and 39 deletions

View file

@ -1,7 +1,8 @@
discard """
nimoutFull: true
cmd: '''nim c -r --warnings:off --hints:off --gc:arc --expandArc:newTarget --expandArc:delete --expandArc:p1 --expandArc:tt --hint:Performance:off --assertions:off --expandArc:extractConfig --expandArc:mergeShadowScope --expandArc:check $file'''
nimout: '''--expandArc: newTarget
nimout: '''
--expandArc: newTarget
var
splat
@ -11,9 +12,9 @@ splat = splitDrive do:
let blitTmp = path
blitTmp
:tmp = splat.drive
wasMoved(splat.drive)
`=wasMoved`(splat.drive)
:tmp_1 = splat.path_1
wasMoved(splat.path_1)
`=wasMoved`(splat.path_1)
result = (
let blitTmp_1 = :tmp
blitTmp_1,
@ -60,10 +61,10 @@ var
try:
it_cursor = x
a = (
wasMoved(:tmpD)
`=wasMoved`(:tmpD)
`=copy`(:tmpD, it_cursor.key)
:tmpD,
wasMoved(:tmpD_1)
`=wasMoved`(:tmpD_1)
`=copy`(:tmpD_1, it_cursor.val)
:tmpD_1)
echo [
@ -112,7 +113,7 @@ block :tmp:
var :tmpD
sym = shadowScope.symbols[i]
addInterfaceDecl(c):
wasMoved(:tmpD)
`=wasMoved`(:tmpD)
`=copy_1`(:tmpD, sym)
:tmpD
inc(i, 1)
@ -125,7 +126,7 @@ this.isValid = fileExists(this.value)
if dirExists(this.value):
var :tmpD
par = (dir:
wasMoved(:tmpD)
`=wasMoved`(:tmpD)
`=copy`(:tmpD, this.value)
:tmpD, front: "") else:
var
@ -133,10 +134,10 @@ if dirExists(this.value):
:tmpD_2
:tmpD_3
par = (dir_1: parentDir(this.value), front_1:
wasMoved(:tmpD_1)
`=wasMoved`(:tmpD_1)
`=copy`(:tmpD_1,
:tmpD_3 = splitDrive do:
wasMoved(:tmpD_2)
`=wasMoved`(:tmpD_2)
`=copy`(:tmpD_2, this.value)
:tmpD_2
:tmpD_3.path)

View file

@ -1,7 +1,8 @@
discard """
output: ''''''
cmd: '''nim c --gc:arc --expandArc:traverse --hint:Performance:off $file'''
nimout: '''--expandArc: traverse
nimout: '''
--expandArc: traverse
var
it_cursor
@ -22,12 +23,13 @@ try:
`=copy`(ri_1, jt.ri)
echo [jt.s]
`=sink`(jt, ri_1)
wasMoved(ri_1)
`=wasMoved`(ri_1)
finally:
`=destroy`(ri_1)
finally:
`=destroy`(jt)
-- end of expandArc ------------------------'''
-- end of expandArc ------------------------
'''
"""
type

View file

@ -1,7 +1,8 @@
discard """
output: ''''''
cmd: '''nim c --gc:arc --expandArc:main --expandArc:tfor --hint:Performance:off $file'''
nimout: '''--expandArc: main
nimout: '''
--expandArc: main
var
a
@ -29,6 +30,7 @@ try:
x = f()
block :tmp:
var i_cursor
mixin inc
var i_1 = 0
block :tmp_1:
while i_1 < 4:
@ -37,25 +39,26 @@ try:
if i_cursor == 2:
return
add(a):
wasMoved(:tmpD)
`=wasMoved`(:tmpD)
`=copy`(:tmpD, x)
:tmpD
inc i_1, 1
if cond:
add(a):
let blitTmp = x
wasMoved(x)
`=wasMoved`(x)
blitTmp
else:
add(b):
let blitTmp_1 = x
wasMoved(x)
`=wasMoved`(x)
blitTmp_1
finally:
`=destroy`(x)
`=destroy_1`(b)
`=destroy_1`(a)
-- end of expandArc ------------------------'''
-- end of expandArc ------------------------
'''
"""
proc f(): seq[int] =

View file

@ -4,7 +4,8 @@ discard """
@[1953719668, 875770417]
destroying O1'''
cmd: '''nim c --gc:arc --expandArc:main --expandArc:main1 --expandArc:main2 --expandArc:main3 --hints:off --assertions:off $file'''
nimout: '''--expandArc: main
nimout: '''
--expandArc: main
var
data
@ -12,7 +13,7 @@ var
:tmpD_1
:tmpD_2
data =
wasMoved(:tmpD)
`=wasMoved`(:tmpD)
`=copy`(:tmpD, cast[string](
:tmpD_2 = encode(cast[seq[byte]](
:tmpD_1 = newString(100)
@ -32,7 +33,7 @@ var
:tmpD_1
s = newString(100)
data =
wasMoved(:tmpD)
`=wasMoved`(:tmpD)
`=copy`(:tmpD, cast[string](
:tmpD_1 = encode(toOpenArrayByte(s, 0, len(s) - 1))
:tmpD_1))
@ -50,7 +51,7 @@ var
:tmpD_1
s = newSeq(100)
data =
wasMoved(:tmpD)
`=wasMoved`(:tmpD)
`=copy`(:tmpD, cast[string](
:tmpD_1 = encode(s)
:tmpD_1))
@ -67,7 +68,7 @@ var
:tmpD_1
:tmpD_2
data =
wasMoved(:tmpD)
`=wasMoved`(:tmpD)
`=copy`(:tmpD, cast[string](
:tmpD_2 = encode do:
:tmpD_1 = newSeq(100)
@ -77,7 +78,8 @@ data =
`=destroy`(:tmpD_2)
`=destroy`(:tmpD_1)
`=destroy_1`(data)
-- end of expandArc ------------------------'''
-- end of expandArc ------------------------
'''
"""
func encode*(src: openArray[byte]): seq[byte] =

View file

@ -0,0 +1,37 @@
discard """
cmd: '''nim c --mm:arc $file'''
errormsg: "'=wasMoved' is not available for type <Game>; routine: main"
"""
# bug #19291
const
screenWidth = 800
screenHeight = 450
var
ready = false
type
Game = object
proc `=destroy`(x: var Game) =
assert ready, "Window is already opened"
ready = false
proc `=sink`(x: var Game; y: Game) {.error.}
proc `=copy`(x: var Game; y: Game) {.error.}
proc `=wasMoved`(x: var Game) {.error.}
proc initGame(width, height: int32, title: string): Game =
assert not ready, "Window is already closed"
ready = true
proc update(x: Game) = discard
proc main =
var g = initGame(screenWidth, screenHeight, "Tetris raylib")
g.update()
var g2 = g
echo "hello"
main()