fixes #22286; enforce Non-var T destructors by nimPreviewNonVarDestructor (#22975)

fixes #22286
ref https://forum.nim-lang.org/t/10642

For backwards compatibilities, we might need to keep the changes under a
preview compiler flag. Let's see how many packags it break.

**TODO** in the following PRs

- [ ] Turn the `var T` destructors warning into an error with
`nimPreviewNonVarDestructor`

---------

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
ringabout
2023-11-26 01:27:27 +08:00
committed by GitHub
parent 502a4486ae
commit 379299a5ac
13 changed files with 84 additions and 41 deletions

View File

@@ -1,9 +1,11 @@
discard """
disabled: "arm64"
cmd: "nim c --gc:arc $file"
cmd: "nim c --mm:arc -u:nimPreviewNonVarDestructor $file"
output: "y"
"""
# TODO: fixme: investigate why it failed with non-var destructors
{.passC: "-march=native".}
proc isAlignedCheck(p: pointer, alignment: int) =

View File

@@ -97,7 +97,7 @@ try:
finally:
`=destroy`(splitted)
finally:
`=destroy`(lan_ip)
`=destroy_1`(lan_ip)
-- end of expandArc ------------------------
--expandArc: mergeShadowScope

View File

@@ -1,6 +1,5 @@
discard """
ccodecheck: "\\i !@('struct tyObject_MyRefObject'[0-z]+' {')"
ccodecheck: "\\i !@('mymoduleInit')"
ccodecheck: "\\i !@('struct tyObject_MyRefObject'[0-z]+' _')"
output: "hello"
"""

View File

@@ -40,6 +40,7 @@ switch("define", "nimPreviewFloatRoundtrip")
switch("define", "nimPreviewJsonutilsHoleyEnum")
switch("define", "nimPreviewHashRef")
switch("define", "nimPreviewRangeDefault")
switch("define", "nimPreviewNonVarDestructor")
switch("warningAserror", "UnnamedBreak")
switch("legacy", "verboseTypeMismatch")

View File

@@ -22,12 +22,11 @@ proc `$`(x: MyLen): string {.borrow.}
proc `==`(x1, x2: MyLen): bool {.borrow.}
proc `=destroy`*(m: var MySeq) {.inline.} =
proc `=destroy`*(m: MySeq) {.inline.} =
if m.data != nil:
deallocShared(m.data)
m.data = nil
proc `=`*(m: var MySeq, m2: MySeq) =
proc `=copy`*(m: var MySeq, m2: MySeq) =
if m.data == m2.data: return
if m.data != nil:
`=destroy`(m)
@@ -77,13 +76,12 @@ converter literalToLen*(x: int{lit}): MyLen =
# Unique pointer implementation
#-------------------------------------------------------------
proc `=destroy`*[T](p: var UniquePtr[T]) =
proc `=destroy`*[T](p: UniquePtr[T]) =
if p.val != nil:
`=destroy`(p.val[])
dealloc(p.val)
p.val = nil
proc `=`*[T](dest: var UniquePtr[T], src: UniquePtr[T]) {.error.}
proc `=copy`*[T](dest: var UniquePtr[T], src: UniquePtr[T]) {.error.}
proc `=sink`*[T](dest: var UniquePtr[T], src: UniquePtr[T]) {.inline.} =
if dest.val != nil and dest.val != src.val:
@@ -118,13 +116,12 @@ type
## as it returns only `lent T`
val: ptr T
proc `=destroy`*[T](p: var ConstPtr[T]) =
proc `=destroy`*[T](p: ConstPtr[T]) =
if p.val != nil:
`=destroy`(p.val[])
dealloc(p.val)
p.val = nil
proc `=`*[T](dest: var ConstPtr[T], src: ConstPtr[T]) {.error.}
proc `=copy`*[T](dest: var ConstPtr[T], src: ConstPtr[T]) {.error.}
proc `=sink`*[T](dest: var ConstPtr[T], src: ConstPtr[T]) {.inline.} =
if dest.val != nil and dest.val != src.val:

View File

@@ -2,13 +2,12 @@ type
ConstPtr*[T] = object
val: ptr T
proc `=destroy`*[T](p: var ConstPtr[T]) =
proc `=destroy`*[T](p: ConstPtr[T]) =
if p.val != nil:
`=destroy`(p.val[])
dealloc(p.val)
p.val = nil
proc `=`*[T](dest: var ConstPtr[T], src: ConstPtr[T]) {.error.}
proc `=copy`*[T](dest: var ConstPtr[T], src: ConstPtr[T]) {.error.}
proc `=sink`*[T](dest: var ConstPtr[T], src: ConstPtr[T]) {.inline.} =
if dest.val != nil and dest.val != src.val:
@@ -31,12 +30,11 @@ type
len: int
data: ptr UncheckedArray[float]
proc `=destroy`*(m: var MySeqNonCopyable) {.inline.} =
proc `=destroy`*(m: MySeqNonCopyable) {.inline.} =
if m.data != nil:
deallocShared(m.data)
m.data = nil
proc `=`*(m: var MySeqNonCopyable, m2: MySeqNonCopyable) {.error.}
proc `=copy`*(m: var MySeqNonCopyable, m2: MySeqNonCopyable) {.error.}
proc `=sink`*(m: var MySeqNonCopyable, m2: MySeqNonCopyable) {.inline.} =
if m.data != m2.data:

View File

@@ -152,7 +152,7 @@ type
Graph = object
nodes: seq[Node]
proc `=destroy`(x: var NodeObj) =
proc `=destroy`(x: NodeObj) =
`=destroy`(x.neighbors)
`=destroy`(x.label)
@@ -210,7 +210,7 @@ block: # bug #22197
## If this does not exist, it also works!
proc newFileID(): FileID = FileID(H5Id())
proc `=destroy`(grp: var H5GroupObj) =
proc `=destroy`(grp: H5GroupObj) =
## Closes the group and resets all references to nil.
if cast[pointer](grp.fileId) != nil:
`=destroy`(grp.file_id)
@@ -218,3 +218,30 @@ block: # bug #22197
var grp = H5Group()
reset(grp.file_id)
reset(grp)
import std/tables
block: # bug #22286
type
A = object
B = object
a: A
C = object
b: B
proc `=destroy`(self: A) =
echo "destroyed"
proc `=destroy`(self: C) =
`=destroy`(self.b)
var c = C()
block: # https://forum.nim-lang.org/t/10642
type AObj = object
name: string
tableField: Table[string, string]
proc `=destroy`(x: AObj) =
`=destroy`(x.name)
`=destroy`(x.tableField)

View File

@@ -20,8 +20,8 @@ data =
:tmpD_2))
:tmpD
`=destroy`(:tmpD_2)
`=destroy`(:tmpD_1)
`=destroy`(data)
`=destroy_1`(:tmpD_1)
`=destroy_1`(data)
-- end of expandArc ------------------------
--expandArc: main1
@@ -37,8 +37,8 @@ data =
:tmpD_1))
:tmpD
`=destroy`(:tmpD_1)
`=destroy`(data)
`=destroy`(s)
`=destroy_1`(data)
`=destroy_1`(s)
-- end of expandArc ------------------------
--expandArc: main2
@@ -54,7 +54,7 @@ data =
:tmpD_1))
:tmpD
`=destroy`(:tmpD_1)
`=destroy`(data)
`=destroy_1`(data)
`=destroy`(s)
-- end of expandArc ------------------------
--expandArc: main3
@@ -73,7 +73,7 @@ data =
:tmpD
`=destroy`(:tmpD_2)
`=destroy`(:tmpD_1)
`=destroy`(data)
`=destroy_1`(data)
-- end of expandArc ------------------------
'''
"""

View File

@@ -11,9 +11,19 @@ var foo_counter = 0
var alive_foos = newseq[int](0)
when defined(gcDestructors):
proc `=destroy`(some: var TFoo) =
proc `=destroy`(some: TFoo) =
alive_foos.del alive_foos.find(some.id)
`=destroy`(some.fn)
# TODO: fixme: investigate why `=destroy` requires `some.fn` to be `gcsafe`
# the debugging info below came from `symPrototype` in the liftdestructors
# proc (){.closure, gcsafe.}, {tfThread, tfHasAsgn, tfCheckedForDestructor, tfExplicitCallConv}
# var proc (){.closure, gcsafe.}, {tfHasGCedMem}
# it worked by accident with var T destructors because in the sempass2
#
# let argtype = skipTypes(a.typ, abstractInst) # !!! it does't skip `tyVar`
# if argtype.kind == tyProc and notGcSafe(argtype) and not tracked.inEnforcedGcSafe:
# localError(tracked.config, n.info, $n & " is not GC safe")
{.cast(gcsafe).}:
`=destroy`(some.fn)
else:
proc free*(some: ref TFoo) =