mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-30 09:54:49 +00:00
destructors: some improvements for bug #4214: object constructors are moved too
This commit is contained in:
@@ -210,7 +210,7 @@ template recurse(n, dest) =
|
||||
dest.add p(n[i], c)
|
||||
|
||||
proc moveOrCopy(dest, ri: PNode; c: var Con): PNode =
|
||||
if ri.kind in nkCallKinds:
|
||||
if ri.kind in nkCallKinds+{nkObjConstr}:
|
||||
result = genSink(ri.typ, dest)
|
||||
# watch out and no not transform 'ri' twice if it's a call:
|
||||
let ri2 = copyNode(ri)
|
||||
@@ -312,7 +312,7 @@ proc injectDestructorCalls*(owner: PSym; n: PNode): PNode =
|
||||
result.add body
|
||||
|
||||
when defined(nimDebugDestroys):
|
||||
if owner.name.s == "createSeq":
|
||||
if owner.name.s == "main" or true:
|
||||
echo "------------------------------------"
|
||||
echo owner.name.s, " transformed to: "
|
||||
echo result
|
||||
|
||||
32
tests/destructor/tmove_objconstr.nim
Normal file
32
tests/destructor/tmove_objconstr.nim
Normal file
@@ -0,0 +1,32 @@
|
||||
|
||||
discard """
|
||||
output: '''test created
|
||||
test destroyed 0'''
|
||||
cmd: '''nim c --newruntime $file'''
|
||||
"""
|
||||
|
||||
# bug #4214
|
||||
type
|
||||
Data = object
|
||||
data: string
|
||||
rc: int
|
||||
|
||||
proc `=destroy`(d: var Data) =
|
||||
dec d.rc
|
||||
echo d.data, " destroyed ", d.rc
|
||||
|
||||
proc `=`(dst: var Data, src: Data) =
|
||||
echo src.data, " copied"
|
||||
dst.data = src.data & " (copy)"
|
||||
dec dst.rc
|
||||
inc dst.rc
|
||||
|
||||
proc initData(s: string): Data =
|
||||
result = Data(data: s, rc: 1)
|
||||
echo s, " created"
|
||||
|
||||
proc main =
|
||||
var x = initData"test"
|
||||
|
||||
when isMainModule:
|
||||
main()
|
||||
Reference in New Issue
Block a user