summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorringabout <43030857+ringabout@users.noreply.github.com>2022-10-06 13:51:42 +0800
committerGitHub <noreply@github.com>2022-10-06 13:51:42 +0800
commit114acff98d64781b1fdfe951c6f543e2fec60000 (patch)
treeff93b3884d70ecf2cd12bf1eea8d27caf17ff044
parentc273496d1852bef725092cbc44295221823edc49 (diff)
downloadNim-114acff98d64781b1fdfe951c6f543e2fec60000.tar.gz
closes #9401; add testcase (#20507)
-rw-r--r--tests/arc/tarcmisc.nim49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/arc/tarcmisc.nim b/tests/arc/tarcmisc.nim
index 45d1514cd..567daf982 100644
--- a/tests/arc/tarcmisc.nim
+++ b/tests/arc/tarcmisc.nim
@@ -1,5 +1,6 @@
 discard """
   output: '''
+=destroy called
 123xyzabc
 destroyed: false
 destroyed: false
@@ -38,6 +39,54 @@ destroying variable: 10
   cmd: "nim c --gc:arc --deepcopy:on -d:nimAllocPagesViaMalloc $file"
 """
 
+# bug #9401
+
+type
+  MyObj = object
+    len: int
+    data: ptr UncheckedArray[float]
+
+proc `=destroy`*(m: var MyObj) =
+
+  echo "=destroy called"
+
+  if m.data != nil:
+    deallocShared(m.data)
+    m.data = nil
+
+type
+  MyObjDistinct = distinct MyObj
+
+proc `=copy`*(m: var MyObj, m2: MyObj) =
+  if m.data == m2.data: return
+  if m.data != nil:
+    `=destroy`(m)
+  m.len = m2.len
+  if m.len > 0:
+    m.data = cast[ptr UncheckedArray[float]](allocShared(sizeof(float) * m.len))
+    copyMem(m.data, m2.data, sizeof(float) * m.len)
+
+
+proc `=sink`*(m: var MyObj, m2: MyObj) =
+  if m.data != m2.data:
+    if m.data != nil:
+      `=destroy`(m)
+    m.len = m2.len
+    m.data = m2.data
+
+proc newMyObj(len: int): MyObj =
+  result.len = len
+  result.data = cast[ptr UncheckedArray[float]](allocShared(sizeof(float) * len))
+
+proc newMyObjDistinct(len: int): MyObjDistinct =
+  MyObjDistinct(newMyObj(len))
+
+proc fooDistinct =
+  doAssert newMyObjDistinct(2).MyObj.len == 2
+
+fooDistinct()
+
+
 proc takeSink(x: sink string): bool = true
 
 proc b(x: sink string): string =