* fixes #18469

* Update compiler/injectdestructors.nim
This commit is contained in:
Andreas Rumpf 2021-07-20 22:15:06 +02:00 • committed by GitHub
commit f8519657c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 74 additions and 15 deletions

View file

@ -0,0 +1,44 @@
discard """
output: '''a
b
c'''
cmd: "nim c --gc:arc $file"
"""
# bug #18469
type
Edge = object
neighbor {.cursor.}: Node
NodeObj = object
neighbors: seq[Edge]
label: string
visited: bool
Node = ref NodeObj
Graph = object
nodes: seq[Node]
proc `=destroy`(x: var NodeObj) =
echo x.label
`=destroy`(x.neighbors)
`=destroy`(x.label)
proc addNode(self: var Graph; label: string): Node =
self.nodes.add(Node(label: label))
result = self.nodes[^1]
proc addEdge(self: Graph; source, neighbor: Node) =
source.neighbors.add(Edge(neighbor: neighbor))
proc main =
var graph: Graph
let nodeA = graph.addNode("a")
let nodeB = graph.addNode("b")
let nodeC = graph.addNode("c")
graph.addEdge(nodeA, neighbor = nodeB)
graph.addEdge(nodeA, neighbor = nodeC)
main()