Skip to content

Commit 32d4bf3

Browse files
authored
fixes an old ARC bug: the produced copy/sink operations don't copy the hidden type field for objects with enabled inheritance; fixes #19205 [backport:1.6] (#19232)
1 parent 4f64c9f commit 32d4bf3

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

compiler/ast.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ type
673673
mSwap, mIsNil, mArrToSeq,
674674
mNewString, mNewStringOfCap, mParseBiggestFloat,
675675
mMove, mWasMoved, mDestroy, mTrace,
676-
mDefault, mUnown, mFinished, mIsolate, mAccessEnv, mReset,
676+
mDefault, mUnown, mFinished, mIsolate, mAccessEnv, mAccessTypeField, mReset,
677677
mArray, mOpenArray, mRange, mSet, mSeq, mVarargs,
678678
mRef, mPtr, mVar, mDistinct, mVoid, mTuple,
679679
mOrdinal, mIterableType,

compiler/ccgexprs.nim

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1741,6 +1741,13 @@ proc genGetTypeInfoV2(p: BProc, e: PNode, d: var TLoc) =
17411741
# use the dynamic type stored at offset 0:
17421742
putIntoDest(p, d, e, rdMType(p, a, nilCheck))
17431743

1744+
proc genAccessTypeField(p: BProc; e: PNode; d: var TLoc) =
1745+
var a: TLoc
1746+
initLocExpr(p, e[1], a)
1747+
var nilCheck = Rope(nil)
1748+
# use the dynamic type stored at offset 0:
1749+
putIntoDest(p, d, e, rdMType(p, a, nilCheck))
1750+
17441751
template genDollar(p: BProc, n: PNode, d: var TLoc, frmt: string) =
17451752
var a: TLoc
17461753
initLocExpr(p, n[1], a)
@@ -2449,6 +2456,7 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
24492456
of mMove: genMove(p, e, d)
24502457
of mDestroy: genDestroy(p, e)
24512458
of mAccessEnv: unaryExpr(p, e, d, "$1.ClE_0")
2459+
of mAccessTypeField: genAccessTypeField(p, e, d)
24522460
of mSlice: genSlice(p, e, d)
24532461
of mTrace: discard "no code to generate"
24542462
else:

compiler/ccgtypes.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ proc getRecordDesc(m: BModule, typ: PType, name: Rope,
583583

584584
if typ.kind == tyObject:
585585
if typ[0] == nil:
586-
if (typ.sym != nil and sfPure in typ.sym.flags) or tfFinal in typ.flags:
586+
if lacksMTypeField(typ):
587587
appcg(m, result, " {$n", [])
588588
else:
589589
if optTinyRtti in m.config.globalOptions:

compiler/liftdestructors.nim

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,12 @@ proc symPrototype(g: ModuleGraph; typ: PType; owner: PSym; kind: TTypeAttachedOp
941941
incl result.flags, sfFromGeneric
942942
incl result.flags, sfGeneratedOp
943943

944+
proc genTypeFieldCopy(c: var TLiftCtx; t: PType; body, x, y: PNode) =
945+
let xx = genBuiltin(c, mAccessTypeField, "accessTypeField", x)
946+
let yy = genBuiltin(c, mAccessTypeField, "accessTypeField", y)
947+
xx.typ = getSysType(c.g, c.info, tyPointer)
948+
yy.typ = xx.typ
949+
body.add newAsgnStmt(xx, yy)
944950

945951
proc produceSym(g: ModuleGraph; c: PContext; typ: PType; kind: TTypeAttachedOp;
946952
info: TLineInfo; idgen: IdGenerator): PSym =
@@ -980,6 +986,10 @@ proc produceSym(g: ModuleGraph; c: PContext; typ: PType; kind: TTypeAttachedOp;
980986
fillStrOp(a, typ, result.ast[bodyPos], d, src)
981987
else:
982988
fillBody(a, typ, result.ast[bodyPos], d, src)
989+
if tk == tyObject and a.kind in {attachedAsgn, attachedSink, attachedDeepCopy} and not lacksMTypeField(typ):
990+
# bug #19205: Do not forget to also copy the hidden type field:
991+
genTypeFieldCopy(a, typ, result.ast[bodyPos], d, src)
992+
983993
if not a.canRaise: incl result.flags, sfNeverRaises
984994
completePartialOp(g, idgen.module, typ, kind, result)
985995

compiler/types.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,3 +1705,6 @@ proc isCharArrayPtr*(t: PType; allowPointerToChar: bool): bool =
17051705
result = allowPointerToChar
17061706
else:
17071707
discard
1708+
1709+
proc lacksMTypeField*(typ: PType): bool {.inline.} =
1710+
(typ.sym != nil and sfPure in typ.sym.flags) or tfFinal in typ.flags

tests/arc/tarcmisc.nim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ ok
3030
true
3131
copying
3232
123
33+
42
3334
closed
3435
destroying variable: 20
3536
destroying variable: 10
@@ -482,3 +483,17 @@ method testMethod(self: BrokenObject) {.base.} =
482483

483484
let mikasa = BrokenObject()
484485
mikasa.testMethod()
486+
487+
# bug #19205
488+
type
489+
InputSectionBase* = object of RootObj
490+
relocations*: seq[int] # traced reference. string has a similar SIGSEGV.
491+
InputSection* = object of InputSectionBase
492+
493+
proc fooz(sec: var InputSectionBase) =
494+
if sec of InputSection: # this line SIGSEGV.
495+
echo 42
496+
497+
var sec = create(InputSection)
498+
sec[] = InputSection(relocations: newSeq[int]())
499+
fooz sec[]

0 commit comments

Comments
 (0)