mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-05 03:14:08 +00:00
fixes #22866; fixes #19998; ensure destruction for Object construction with custom destructors (#22901)
fixes #22866;
fixes #19998
(cherry picked from commit b68e0aab4c)
This commit is contained in:
@@ -852,7 +852,9 @@ proc p(n: PNode; c: var Con; s: var Scope; mode: ProcessMode; tmpFlags = {sfSing
|
||||
result[i][1] = p(n[i][1], c, s, m)
|
||||
else:
|
||||
result[i] = p(n[i], c, s, m)
|
||||
if mode == normal and isRefConstr:
|
||||
if mode == normal and (isRefConstr or (hasDestructor(c, t) and
|
||||
getAttachedOp(c.graph, t, attachedDestructor) != nil and
|
||||
sfOverridden in getAttachedOp(c.graph, t, attachedDestructor).flags)):
|
||||
result = ensureDestruction(result, n, c, s)
|
||||
of nkCallKinds:
|
||||
if n[0].kind == nkSym and n[0].sym.magic == mEnsureMove:
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
discard """
|
||||
cmd: '''nim c --gc:arc $file'''
|
||||
output: '''2
|
||||
2'''
|
||||
cmd: '''nim c --mm:arc $file'''
|
||||
output: '''
|
||||
2
|
||||
2
|
||||
destroyed
|
||||
'''
|
||||
"""
|
||||
|
||||
type
|
||||
ObjWithDestructor = object
|
||||
a: int
|
||||
proc `=destroy`(self: var ObjWithDestructor) =
|
||||
proc `=destroy`(self: ObjWithDestructor) =
|
||||
echo "destroyed"
|
||||
|
||||
proc `=`(self: var ObjWithDestructor, other: ObjWithDestructor) =
|
||||
proc `=copy`(self: var ObjWithDestructor, other: ObjWithDestructor) =
|
||||
echo "copied"
|
||||
|
||||
proc test(a: range[0..1], arg: ObjWithDestructor) =
|
||||
@@ -38,4 +41,4 @@ proc test(a: range[0..1], arg: ObjWithDestructor) =
|
||||
if iteration == 2:
|
||||
break
|
||||
|
||||
test(1, ObjWithDestructor())
|
||||
test(1, ObjWithDestructor())
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
discard """
|
||||
cmd: '''nim c --gc:arc $file'''
|
||||
output: '''2
|
||||
2'''
|
||||
cmd: '''nim c --mm:arc $file'''
|
||||
output: '''
|
||||
2
|
||||
2
|
||||
destroyed
|
||||
'''
|
||||
"""
|
||||
|
||||
type
|
||||
ObjWithDestructor = object
|
||||
a: int
|
||||
proc `=destroy`(self: var ObjWithDestructor) =
|
||||
proc `=destroy`(self: ObjWithDestructor) =
|
||||
echo "destroyed"
|
||||
|
||||
proc `=copy`(self: var ObjWithDestructor, other: ObjWithDestructor) =
|
||||
|
||||
Reference in New Issue
Block a user