summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2023-07-03 03:04:48 +0800
committerGitHub <noreply@github.com>2023-07-02 21:04:48 +0200
commit57296a51397d65ce4f342d6cbc92a9de2629875c (patch)
treedf42f7ff238ca252347383312ae5b8ac2df0ec11
parent83a5865024cf391137fcbd4e5ed195cda969ed88 (diff)
downloadNim-57296a51397d65ce4f342d6cbc92a9de2629875c.tar.gz
fixes #22197; Distinct ref objects + destructor cause C++ codegen error (#22207)
-rw-r--r--compiler/semmagic.nim8
-rw-r--r--tests/destructor/tnonvardestructor.nim27
2 files changed, 34 insertions, 1 deletions
diff --git a/compiler/semmagic.nim b/compiler/semmagic.nim
index 22c2fb57e..e7057053b 100644
--- a/compiler/semmagic.nim
+++ b/compiler/semmagic.nim
@@ -609,6 +609,14 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
     let op = getAttachedOp(c.graph, t, attachedDestructor)
     if op != nil:
       result[0] = newSymNode(op)
+
+      if op.typ != nil and op.typ.len == 2 and op.typ[1].kind != tyVar and
+          skipAddr(n[1]).typ.kind == tyDistinct:
+        if n[1].kind == nkSym and n[1].sym.kind == skParam and
+            n[1].typ.kind == tyVar:
+          result[1] = genDeref(n[1])
+        else:
+          result[1] = skipAddr(n[1])
   of mTrace:
     result = n
     let t = n[1].typ.skipTypes(abstractVar)
diff --git a/tests/destructor/tnonvardestructor.nim b/tests/destructor/tnonvardestructor.nim
index e482ba137..1f59a3978 100644
--- a/tests/destructor/tnonvardestructor.nim
+++ b/tests/destructor/tnonvardestructor.nim
@@ -192,4 +192,29 @@ block:
     doAssert $(x[]) == "()"

 

   # bug #11705

-  foo()
\ No newline at end of file
+  foo()

+

+block: # bug #22197

+  type

+    H5IdObj = object

+    H5Id = ref H5IdObj

+

+    FileID = distinct H5Id

+

+    H5GroupObj = object

+      file_id: FileID

+    H5Group = ref H5GroupObj

+

+  ## This would make it work!

+  #proc `=destroy`*(x: FileID) = `=destroy`(cast[H5Id](x))

+  ## If this does not exist, it also works!

+  proc newFileID(): FileID = FileID(H5Id())

+

+  proc `=destroy`(grp: var H5GroupObj) =

+    ## Closes the group and resets all references to nil.

+    if cast[pointer](grp.fileId) != nil:

+      `=destroy`(grp.file_id)

+

+  var grp = H5Group()

+  reset(grp.file_id)

+  reset(grp)