From 6a62f6a51d41e892e9e02b2adffec791025d4081 Mon Sep 17 00:00:00 2001 From: Araq Date: Thu, 2 Jan 2020 22:34:49 +0100 Subject: [PATCH] fixes #12978 --- compiler/cgen.nim | 10 ++++++---- tests/destructor/tcomplexobjconstr.nim | 25 ++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/compiler/cgen.nim b/compiler/cgen.nim index f91f66933b90..da041bf148a4 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 23c615783551..fd112b6e24fb 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()