This commit is contained in:
ringabout
2026-05-20 20:19:23 +08:00
parent 4d77a1d9fc
commit 8fb0023f46
3 changed files with 11 additions and 19 deletions

View File

@@ -2815,15 +2815,6 @@ proc genWasMoved(p: BProc; n: PNode) =
#linefmt(p, cpsStmts, "#nimZeroMem((void*)$1, sizeof($2));$n",
# [addrLoc(p.config, a), getTypeDesc(p.module, a.t)])
proc genMoveCall(p: BProc; n: PNode; d: var TLoc) =
n[1] = makeAddr(n[1], p.module.idgen)
let moveSym = n[0].sym
let oldOwner = moveSym.owner
# TODO: sem instantiation issues, without this, C++ types are broken
setOwner(moveSym, p.module.module)
genCall(p, n, d)
setOwner(moveSym, oldOwner)
proc genMove(p: BProc; n: PNode; d: var TLoc) =
if n.len == 4:
# generated by liftdestructors:
@@ -2853,7 +2844,8 @@ proc genMove(p: BProc; n: PNode; d: var TLoc) =
genAssignment(p, d, a, {})
resetLoc(p, a)
else:
genMoveCall(p, n, d)
n[1] = makeAddr(n[1], p.module.idgen)
genCall(p, n, d)
else:
var a: TLoc = initLocExpr(p, n[1].skipAddr, {lfEnforceDeref, lfPrepareForMutation})
genAssignment(p, d, a, {})

7
tests/ccgbugs2/m25800.h Normal file
View File

@@ -0,0 +1,7 @@
/*TYPESECTION*/
struct CppRef {
int* data;
CppRef() : data(new int(42)) {}
~CppRef() { delete data; data = nullptr; }
void reset() { delete data; data = nullptr; }
};

View File

@@ -4,16 +4,9 @@ discard """
"""
# Bug Report 1: {.importcpp.} on =wasMoved generates invalid preprocessor directive #.
{.emit: """/*TYPESECTION*/
struct CppRef {
int* data;
CppRef() : data(new int(42)) {}
~CppRef() { delete data; data = nullptr; }
void reset() { delete data; data = nullptr; }
};
""".}
type CppRef* {.importcpp, bycopy, noInit.} = object
type CppRef* {.importcpp, bycopy, noInit, header: "m25800.h".} = object
proc `=destroy`(x: var CppRef) {.importcpp: "#.~CppRef()".}
proc `=wasMoved`(x: var CppRef) {.importcpp: "#.reset()".}