Skip to content

Commit 473f136

Browse files
authored
fixes getRtti, inheritance and nimChckNilDisp (#1086)
fixes `nimChckNilDisp` called for `var object` fixes `getRtti` for `ptr` and `var` objects
1 parent 0f8f6cd commit 473f136

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed

src/hexer/lifter.nim

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,16 +528,23 @@ proc unravel(c: var LiftingCtx; typ: TypeCursor; paramA, paramB: TokenBuf) =
528528
let fn = lift(c, typ)
529529
maybeCallHook c, fn, paramA, paramB
530530

531+
proc depth(s: SymId): int =
532+
result = 0
533+
var root = s
534+
for r in inheritanceChain(s):
535+
root = r
536+
inc result
537+
531538
proc setupVTableField(c: var LiftingCtx; param: TokenBuf; cls: SymId) =
532539
copyIntoKind c.dest, AsgnS, c.info:
533540
copyIntoKind c.dest, DotX, c.info:
534541
copyIntoKind c.dest, DerefX, c.info:
535542
copyTree c.dest, param
536543
copyIntoSymUse c.dest, pool.syms.getOrIncl(VTableField), c.info
537-
c.dest.addIntLit(0, c.info)
544+
c.dest.addIntLit(depth(cls), c.info)
538545
copyIntoKind c.dest, CallX, c.info:
539546
copyIntoSymUse c.dest, getCompilerProc(c, "getRtti"), c.info
540-
copyIntoSymUse c.dest, cls, c.info
547+
copyTree c.dest, param
541548

542549
proc unravelDispatch(c: var LiftingCtx; orig: TypeCursor; paramA, paramB: TokenBuf) =
543550
#if isTrivial(c, typ):

src/hexer/vtables_backend.nim

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ proc evalOnce(c: var Context; dest: var TokenBuf; n: var Cursor): TempLoc =
103103
return result
104104

105105
let info = n.info
106-
let takeAddr = not constructsValue(n)
106+
let takeAddr = not constructsValue(n) and n.exprKind notin {AddrX, HaddrX}
107107
let argType = getType(c.typeCache, n)
108108
c.needsXelim = true
109109

@@ -191,7 +191,12 @@ proc trMethodCall(c: var Context; dest: var TokenBuf; n: var Cursor) =
191191
else:
192192
let info = n.info
193193
var temp = evalOnce(c, dest, n)
194-
if typ.typeKind in {RefT, PtrT}:
194+
195+
var paramList = fnType
196+
assert paramList.substructureKind == ParamsU
197+
inc paramList
198+
let param = takeLocal(paramList, SkipFinalParRi)
199+
if param.typ.typeKind in {RefT, PtrT}:
195200
# nil check
196201
if not temp.needsParRi:
197202
c.needsXelim = true
@@ -242,10 +247,12 @@ proc trGetRtti(c: var Context; dest: var TokenBuf; n: var Cursor) =
242247
inc n # call
243248
skip n # skip "getRtti" symbol
244249
assert n.kind == Symbol # we have the class name here
245-
let vtabName = getVTableName(c, n.symId)
250+
let typ = getType(c.typeCache, n)
251+
let cls = getClass(typ)
252+
let vtabName = getVTableName(c, cls)
246253
dest.copyIntoKind AddrX, info:
247254
dest.addSymUse vtabName, info
248-
maybeImport(c, n.symId, vtabName)
255+
maybeImport(c, cls, vtabName)
249256
inc n
250257
skipParRi n
251258

@@ -274,7 +281,7 @@ proc trCall(c: var Context; dest: var TokenBuf; n: var Cursor; forceStaticCall:
274281
dest.takeToken n # skip `(call)`
275282
trMethodCall c, dest, n
276283
takeParRi dest, n
277-
elif n.kind == Symbol and n.symId == c.getRttiSym:
284+
elif fn.kind == Symbol and fn.symId == c.getRttiSym:
278285
trGetRtti c, dest, n
279286
else:
280287
dest.takeToken n # skip `(call)`

tests/nimony/object/tmethods.nim

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import std/[assertions, syncio]
2+
3+
type
4+
TestObj = object of RootObj
5+
t: int
6+
7+
SubObject = object of TestObj
8+
9+
method test(t: ptr TestObj) {.base.} =
10+
echo "test called"
11+
12+
method test(t: ptr SubObject) =
13+
t.t = 5
14+
15+
block:
16+
var a: SubObject = SubObject()
17+
18+
test(addr a)
19+
assert a.t == 5
20+
21+
method foo(t: var TestObj) {.base.} =
22+
echo "test called"
23+
24+
method foo(t: var SubObject) =
25+
t.t = 5
26+
27+
block:
28+
var a: SubObject = SubObject()
29+
30+
foo(a)
31+
assert a.t == 5

0 commit comments

Comments
 (0)