summary refs log tree commit diff stats
path: root/tests/destructor
diff options
context:
space:
mode:
authorcooldome <cdome@bk.ru>2019-04-04 20:46:02 +0100
committerGitHub <noreply@github.com>2019-04-04 20:46:02 +0100
commit795de374fba990a9ec7c919b8fda7896c66b1294 (patch)
tree75275e23e5064679e194981d7c04fd29453f9cce /tests/destructor
parentd5f2a5c204f2fe973a569a99731461820122358c (diff)
downloadNim-795de374fba990a9ec7c919b8fda7896c66b1294.tar.gz
fixes destructor tuple regression #10940 (#10941)
* fixes #10940

* bug fixes

* fix spacing
Diffstat (limited to 'tests/destructor')
-rw-r--r--tests/destructor/ttuple.nim48
1 files changed, 48 insertions, 0 deletions
diff --git a/tests/destructor/ttuple.nim b/tests/destructor/ttuple.nim
new file mode 100644
index 000000000..ec12dfc3a
--- /dev/null
+++ b/tests/destructor/ttuple.nim
@@ -0,0 +1,48 @@
+
+discard """
+   output: '''5.0 10.0'''
+"""
+
+type
+  MyOpt[T] = object
+    case has: bool:
+      of true: val: T
+      of false: nil
+
+  MyVal = object
+    f: ptr float
+
+proc `=destroy`(x: var MyVal) =
+  if x.f != nil:
+    dealloc(x.f)
+
+proc `=sink`(x1: var MyVal, x2: Myval) =
+  if x1.f != x2.f:
+    `=destroy`(x1)
+    x1.f = x2.f
+
+proc `=`(x1: var MyVal, x2: Myval) =
+  if x1.f != x2.f:
+    `=destroy`(x1)
+    x1.f = create(float)
+    x1.f[] = x2.f[]
+
+proc newVal(x: float): MyVal =
+  result.f = create(float)
+  result.f[] = x
+
+template getIt[T, R](self: MyOpt[T], body: untyped, default: R): R =
+  if self.has:
+    template it: untyped {.inject.} = self.val
+    body
+  else:
+    default
+
+proc myproc(h: MyOpt[float]) =
+  let (a, b) = h.getIt((newVal(it), newVal(it * 2)), (newVal(1.0), newVal(1.0)))
+  echo a.f[], " ", b.f[]
+
+let h = MyOpt[float](has: true, val: 5.0)
+myproc(h)
+
+