Skip to content

Commit 02609f1

Browse files
authored
fixes #25205 #14873; resets importc obj with nimZeroMem in specializeResetT for refc (#25207)
fixes #25205 fixes #14873 ```nim type SysLockObj {.importc: "pthread_mutex_t", pure, final, header: """#include <sys/types.h> #include <pthread.h>""", byref.} = object when defined(linux) and defined(amd64): abi: array[40 div sizeof(clong), clong] ``` Before this PR, in refc, `resetLoc` generates field assignments for each fields of `importc` object. But the field `abi` is not a genuine field, which doesn't exits in the struct. We could use `zeroMem` to reset the memory if not leave it alone
1 parent 483389d commit 02609f1

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

compiler/ccgreset.nim

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@ proc specializeResetT(p: BProc, accessor: Rope, typ: PType) =
6767
var x = typ.baseClass
6868
if x != nil: x = x.skipTypes(skipPtrs)
6969
specializeResetT(p, accessor.parentObj(p.module), x)
70-
if typ.n != nil: specializeResetN(p, accessor, typ.n, typ)
70+
if typ.n != nil:
71+
if typ.sym != nil and sfImportc in typ.sym.flags:
72+
# imported C struct, nimZeroMem
73+
p.s(cpsStmts).addCallStmt(cgsymValue(p.module, "nimZeroMem"),
74+
cCast(ptrType(CPointer), cAddr(accessor)),
75+
cSizeof(getTypeDesc(p.module, typ)))
76+
else:
77+
specializeResetN(p, accessor, typ.n, typ)
7178
of tyTuple:
7279
let typ = getUniqueType(typ)
7380
for i, a in typ.ikids:

tests/stdlib/tlocks.nim

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,30 @@ import std/assertions
99

1010
var m = createMyType[int]()
1111
doAssert m.use() == 3
12+
13+
14+
import std/locks
15+
16+
type
17+
S = object
18+
r: proc()
19+
20+
B = object
21+
d: Lock
22+
w: S
23+
24+
proc v(x: ptr B) {.exportc.} = reset(x[])
25+
26+
type
27+
Test = object
28+
path: string # Removing this makes both cases work.
29+
lock: Lock
30+
31+
# A: This is not fine.
32+
var a = Test()
33+
34+
proc main(): void =
35+
# B: This is fine.
36+
var b = Test()
37+
38+
main()

0 commit comments

Comments
 (0)