summary refs log tree commit diff stats
path: root/lib/system/deepcopy.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-09-26 09:36:09 +0200
committerAraq <rumpf_a@web.de>2014-09-26 09:36:09 +0200
commit1088814e56811f3bca77f191d8c492405712fcdb (patch)
tree7899b77663824fa613551840c84a1e7b5f06727a /lib/system/deepcopy.nim
parentdfd73902772a0236e09cae534ba98f1a58c194ad (diff)
downloadNim-1088814e56811f3bca77f191d8c492405712fcdb.tar.gz
deepCopy is instantiated when its corresponding type is instantiated
Diffstat (limited to 'lib/system/deepcopy.nim')
-rw-r--r--lib/system/deepcopy.nim18
1 files changed, 9 insertions, 9 deletions
diff --git a/lib/system/deepcopy.nim b/lib/system/deepcopy.nim
index 902999970..e4356a25d 100644
--- a/lib/system/deepcopy.nim
+++ b/lib/system/deepcopy.nim
@@ -82,17 +82,16 @@ proc genericDeepCopyAux(dest, src: pointer, mt: PNimType) =
       genericDeepCopyAux(cast[pointer](d +% i*% mt.base.size),
                          cast[pointer](s +% i*% mt.base.size), mt.base)
   of tyRef:
-    if mt.base.deepcopy != nil:
-      let z = mt.base.deepcopy(cast[PPointer](src)[])
+    let s2 = cast[PPointer](src)[]
+    if s2 == nil:
+      unsureAsgnRef(cast[PPointer](dest), s2)
+    elif mt.base.deepcopy != nil:
+      let z = mt.base.deepcopy(s2)
       unsureAsgnRef(cast[PPointer](dest), z)
     else:
       # we modify the header of the cell temporarily; instead of the type
       # field we store a forwarding pointer. XXX This is bad when the cloning
       # fails due to OOM etc.
-      let s2 = cast[PPointer](src)[]
-      if s2 == nil:
-        unsureAsgnRef(cast[PPointer](dest), s2)
-        return
       when declared(usrToCell):
         # unfortunately we only have cycle detection for our native GCs.
         let x = usrToCell(s2)
@@ -116,10 +115,11 @@ proc genericDeepCopyAux(dest, src: pointer, mt: PNimType) =
         genericDeepCopyAux(z, s2, realType.base)        
   of tyPtr:
     # no cycle check here, but also not really required
-    if mt.base.deepcopy != nil:
-      cast[PPointer](dest)[] = mt.base.deepcopy(cast[PPointer](s)[])
+    let s2 = cast[PPointer](src)[]
+    if s2 != nil and mt.base.deepcopy != nil:
+      cast[PPointer](dest)[] = mt.base.deepcopy(s2)
     else:
-      cast[PPointer](dest)[] = cast[PPointer](s)[]
+      cast[PPointer](dest)[] = s2
   else:
     copyMem(dest, src, mt.size)