cursor fields cannot form reference cycles (#21832)

* cursor fields cannot form a reference cycle

* fixes typo

* fixes position

(cherry picked from commit ebbad9e960)
This commit is contained in:
ringabout
2023-05-12 02:49:47 +08:00
committed by narimiran
parent 2d19520b5c
commit fcaacbf374
2 changed files with 28 additions and 13 deletions

View File

@@ -372,15 +372,18 @@ proc canFormAcycleAux(g: ModuleGraph; marker: var IntSet, typ: PType, orig: PTyp
proc canFormAcycleNode(g: ModuleGraph; marker: var IntSet, n: PNode, orig: PType, withRef: bool, hasTrace: bool): bool =
result = false
if n != nil:
result = canFormAcycleAux(g, marker, n.typ, orig, withRef, hasTrace)
if not result:
case n.kind
of nkNone..nkNilLit:
discard
else:
for i in 0..<n.len:
result = canFormAcycleNode(g, marker, n[i], orig, withRef, hasTrace)
if result: return
var hasCursor = n.kind == nkSym and sfCursor in n.sym.flags
# cursor fields don't own the refs, which cannot form reference cycles
if hasTrace or not hasCursor:
result = canFormAcycleAux(g, marker, n.typ, orig, withRef, hasTrace)
if not result:
case n.kind
of nkNone..nkNilLit:
discard
else:
for i in 0..<n.len:
result = canFormAcycleNode(g, marker, n[i], orig, withRef, hasTrace)
if result: return
proc sameBackendType*(x, y: PType): bool

View File

@@ -51,10 +51,7 @@ cyclicNo((Cyclone, ))
cyclicNo(Acyclic)
cyclicYes(LinkedNode)
when false:
# todo fix me
cyclicNo(LinkedNodeWithCursor)
cyclicNo(LinkedNodeWithCursor) # cursor doesn't increase rc, cannot form a cycle
type
ObjectWithoutCycles = object
@@ -121,3 +118,18 @@ block:
proc `=trace`(x: var myseq[Node]; env: pointer) = discard
cyclicYes(Node)
block:
type
Foo = object
id: ptr ref Foo
cyclicNo(Foo)
block:
type
InheritableObj = object of RootObj
InheritableRef = ref object of RootObj
cyclicYes(InheritableObj)
cyclicYes(InheritableRef)