summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2023-06-26 21:10:06 +0800
committerGitHub <noreply@github.com>2023-06-26 15:10:06 +0200
commit4546f5dfe539ff4f37a5100bdfedaf4867293f3a (patch)
tree4d9f921600ddbc75d9c0964e87b8d121dcd9ede4
parent43a3de9077955031e55691ebb84ee958f6aa260e (diff)
downloadNim-4546f5dfe539ff4f37a5100bdfedaf4867293f3a.tar.gz
adds T destructor for refs (#22147)
* adds T destructor for refs

* add `newRefdestructor`

* adds ref overload for destructors

* fixes config
-rw-r--r--compiler/liftdestructors.nim7
-rw-r--r--lib/system.nim4
-rw-r--r--tests/config.nims1
3 files changed, 11 insertions, 1 deletions
diff --git a/compiler/liftdestructors.nim b/compiler/liftdestructors.nim
index e40193481..c25f089fd 100644
--- a/compiler/liftdestructors.nim
+++ b/compiler/liftdestructors.nim
@@ -1068,7 +1068,12 @@ proc symPrototype(g: ModuleGraph; typ: PType; owner: PSym; kind: TTypeAttachedOp
   let dest = newSym(skParam, getIdent(g.cache, "dest"), idgen, result, info)
   let src = newSym(skParam, getIdent(g.cache, if kind == attachedTrace: "env" else: "src"),
                    idgen, result, info)
-  dest.typ = makeVarType(typ.owner, typ, idgen)
+
+  if kind == attachedDestructor and typ.kind == tyRef:
+    dest.typ = typ
+  else:
+    dest.typ = makeVarType(typ.owner, typ, idgen)
+
   if kind == attachedTrace:
     src.typ = getSysType(g, info, tyPointer)
   else:
diff --git a/lib/system.nim b/lib/system.nim
index 2290ff6f6..d4835b20b 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -365,6 +365,10 @@ proc arrGet[I: Ordinal;T](a: T; i: I): T {.
 proc arrPut[I: Ordinal;T,S](a: T; i: I;
   x: S) {.noSideEffect, magic: "ArrPut".}
 
+when defined(nimAllowNonVarDestructor):
+  proc `=destroy`*[T](x: ref T) {.inline, magic: "Destroy".} =
+    discard
+
 proc `=destroy`*[T](x: var T) {.inline, magic: "Destroy".} =
   ## Generic `destructor`:idx: implementation that can be overridden.
   discard
diff --git a/tests/config.nims b/tests/config.nims
index 842459c3a..b288bec2a 100644
--- a/tests/config.nims
+++ b/tests/config.nims
@@ -43,3 +43,4 @@ switch("define", "nimPreviewRangeDefault")
 
 switch("warningAserror", "UnnamedBreak")
 switch("legacy", "verboseTypeMismatch")
+