Files
Nim/tests/arc/tcursor_field_obj_constr.nim
Andreas Rumpf f8519657c4 fixes #18469 (#18544)
* fixes #18469

* Update compiler/injectdestructors.nim
2021-07-20 22:15:06 +02:00

45 lines
814 B
Nim

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()