Fixes #23962 resetLocdoenst produce any cgen code in importcpp types (#23964)

This commit is contained in:
Juan M Gómez
2024-08-18 12:21:17 +01:00
committed by GitHub
parent f7c11a8978
commit 2e4d344b43
3 changed files with 53 additions and 1 deletions

View File

@@ -482,7 +482,10 @@ include ccgreset
proc resetLoc(p: BProc, loc: var TLoc) =
let containsGcRef = optSeqDestructors notin p.config.globalOptions and containsGarbageCollectedRef(loc.t)
let typ = skipTypes(loc.t, abstractVarRange)
if isImportedCppType(typ): return
if isImportedCppType(typ):
var didGenTemp = false
linefmt(p, cpsStmts, "$1 = $2;$n", [rdLoc(loc), genCppInitializer(p.module, p, typ, didGenTemp)])
return
if optSeqDestructors in p.config.globalOptions and typ.kind in {tyString, tySequence}:
assert loc.snippet != ""

17
tests/cpp/23962.h Normal file
View File

@@ -0,0 +1,17 @@
#include <iostream>
struct Foo {
Foo(int inX): x(inX) {
std::cout << "Ctor Foo(" << x << ")\n";
}
~Foo() {
std::cout << "Destory Foo(" << x << ")\n";
}
void print() {
std::cout << "Foo.x = " << x << '\n';
}
int x;
};

32
tests/cpp/t23962.nim Normal file
View File

@@ -0,0 +1,32 @@
discard """
cmd: "nim cpp $file"
output: '''
Ctor Foo(-1)
Destory Foo(-1)
Ctor Foo(-1)
Destory Foo(-1)
Ctor Foo(-1)
Destory Foo(-1)
Foo.x = 1
Foo.x = 2
Foo.x = -1
'''
"""
type
Foo {.importcpp, header: "23962.h".} = object
x: cint
proc print(f: Foo) {.importcpp.}
#also tests the right constructor is used
proc makeFoo(x: int32 = -1): Foo {.importcpp:"Foo(#)", constructor.}
proc test =
var xs = newSeq[Foo](3)
xs[0].x = 1
xs[1].x = 2
for x in xs:
x.print
test()