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:
ringabout
2023-11-02 18:14:50 +08:00
committed by narimiran
parent f4e80d51ff
commit 86e5545c25
3 changed files with 19 additions and 11 deletions

View File

@@ -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:

View File

@@ -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())

View File

@@ -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) =