summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--compiler/cgen.nim10
-rw-r--r--tests/destructor/tcomplexobjconstr.nim25
2 files changed, 30 insertions, 5 deletions
diff --git a/compiler/cgen.nim b/compiler/cgen.nim
index f91f66933..da041bf14 100644
--- a/compiler/cgen.nim
+++ b/compiler/cgen.nim
@@ -362,12 +362,14 @@ proc genObjectInit(p: BProc, section: TCProcSection, t: PType, a: var TLoc,
     linefmt(p, section, "$1.m_type = $2;$n", [r, genTypeInfo(p.module, t, a.lode.info)])
   of frEmbedded:
     if optTinyRtti in p.config.globalOptions:
+      var tmp: TLoc
       if mode == constructRefObj:
-        var n = newNodeIT(nkObjConstr, a.lode.info, t)
-        n.add newNodeIT(nkType, a.lode.info, t)
-        genObjConstr(p, n, a)
+        let objType = t.skipTypes(abstractInst+{tyRef})
+        rawConstExpr(p, newNodeIT(nkType, a.lode.info, objType), tmp)
+        linefmt(p, cpsStmts,
+            "#nimCopyMem((void*)$1, (NIM_CONST void*)&$2, sizeof($3));$n",
+            [rdLoc(a), rdLoc(tmp), getTypeDesc(p.module, objType)])
       else:
-        var tmp: TLoc
         rawConstExpr(p, newNodeIT(nkType, a.lode.info, t), tmp)
         genAssignment(p, a, tmp, {})
     else:
diff --git a/tests/destructor/tcomplexobjconstr.nim b/tests/destructor/tcomplexobjconstr.nim
index 23c615783..fd112b6e2 100644
--- a/tests/destructor/tcomplexobjconstr.nim
+++ b/tests/destructor/tcomplexobjconstr.nim
@@ -1,5 +1,6 @@
 discard """
-  output: "true"
+  output: '''true
+OK'''
   cmd: "nim c --gc:arc $file"
 """
 
@@ -31,3 +32,25 @@ assert y.more[2] of MyObject1
 assert y.more[2] of RootObj
 
 echo "true"
+
+# bug #12978
+type
+  Vector2* = object of RootObj
+    x*, y*: float
+
+type
+  Vertex* = ref object
+    point*: Vector2
+
+proc newVertex*(p: Vector2): Vertex =
+  return Vertex(point: p)
+
+proc createVertex*(p: Vector2): Vertex =
+  result = newVertex(p)
+
+proc p =
+  var x = Vector2(x: 1, y: 2)
+  let other = createVertex(x)
+  echo "OK"
+
+p()
7:02:46 +0200 Merge tests into a larger file (part 1 of ∞) (#9318)' href='/ahoang/Nim/commit/tests/cnstseq/tcnstseq.nim?h=devel&id=7f18d7cbc1fc8ad87c389b8d4d873e1d1169f794'>7f18d7cbc ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68