Skip to content

Commit

Permalink
[Cpp] Fixes an issue when mixing hooks and calls (#23428)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgomez committed Mar 21, 2024
1 parent 50c1e93 commit 33902d9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion compiler/ccgstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,10 @@ proc genSingleVar(p: BProc, v: PSym; vn, value: PNode) =
line(p, cpsStmts, decl)
else:
tmp = initLocExprSingleUse(p, value)
lineF(p, cpsStmts, "$# = $#;\n", [decl, tmp.rdLoc])
if value.kind == nkEmpty:
lineF(p, cpsStmts, "$#;\n", [decl])
else:
lineF(p, cpsStmts, "$# = $#;\n", [decl, tmp.rdLoc])
return
assignLocalVar(p, vn)
initLocalVar(p, v, imm)
Expand Down
1 change: 1 addition & 0 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,7 @@ proc genProcAux*(m: BModule, prc: PSym) =
elif sfConstructor in prc.flags:
resNode.sym.loc.flags.incl lfIndirect
fillLoc(resNode.sym.loc, locParam, resNode, "this", OnHeap)
prc.loc.r = getTypeDesc(m, resNode.sym.loc.t, dkVar)
else:
fillResult(p.config, resNode, prc.typ)
assignParam(p, res, prc.typ.returnType)
Expand Down
15 changes: 14 additions & 1 deletion tests/cpp/tconstructor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,17 @@ type Foo {.exportc.} = object

proc makeFoo(): Foo {.used, constructor, nodecl.} = discard

echo $Foo()
echo $Foo()

type Boo = object
proc `=copy`(dest: var Boo; src: Boo) = discard

proc makeBoo(): Boo {.constructor.} = Boo()
proc makeBoo2(): Boo = Boo()

block:
proc main =
var b = makeBoo()
var b2 = makeBoo2()

main()

0 comments on commit 33902d9

Please sign in to comment.