diff options
author | ringabout <43030857+ringabout@users.noreply.github.com> | 2022-12-28 23:23:37 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-28 16:23:37 +0100 |
commit | 4b63ac4b87c54a1b31d60b7555e2c60d232817c8 (patch) | |
tree | 5cfcf092d967d47dbe9a5b8433755211589c19af /tests | |
parent | 7a74c2dc3a82488671237555faa95fb38ef31bd5 (diff) | |
download | Nim-4b63ac4b87c54a1b31d60b7555e2c60d232817c8.tar.gz |
fixes #21171; dynamic acyclic refs need to use dyn decRef (#21184)
* fixes #21171; dyn destructors for acyclic inherited refs * add a test * Update compiler/liftdestructors.nim
Diffstat (limited to 'tests')
-rw-r--r-- | tests/arc/t21184.nim | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/tests/arc/t21184.nim b/tests/arc/t21184.nim new file mode 100644 index 000000000..91d0c42c7 --- /dev/null +++ b/tests/arc/t21184.nim @@ -0,0 +1,77 @@ +discard """ + matrix: "--mm:orc" +""" + +import std/[with] + +type + Node* {.acyclic.} = ref object of RootObj + name: string + data: pointer + children: seq[Node] + TextNode = ref object of Node + text: string + +proc fakeEcho(s: string) = + if s.len < 0: + echo s + +proc newNode[T: Node](parent: Node): T = + new result + result.data = alloc0(250) + parent.children.add(result) + +proc newRootNode(): Node = + new result + result.data = alloc0(250) + +method printNode(node: Node) {.base.} = + fakeEcho node.name + +method printNode(node: TextNode) = + procCall printNode(Node(node)) + fakeEcho node.text + +proc printChildren(node: Node) = + for child in node.children: + child.printNode() + printChildren(child) + +proc free(node: Node) = + for child in node.children: + free(child) + dealloc(node.data) + +template node(parent: Node, body: untyped): untyped = + var node = newNode[Node](parent) + with node: + body + +proc textNode(parent: Node, text: string) = + var node = newNode[TextNode](parent) + node.text = text + +template withRootNode(body: untyped): untyped = + var root = newRootNode() + root.name = "root" + with root: + body + root.printNode() + printChildren(root) + root.free() + +proc doTest() = + withRootNode: + node: + name = "child1" + node: + name = "child2" + node: + name = "child3" + textNode "Hello, world!" + + +# bug #21171 +if isMainModule: + for i in 0..100000: + doTest() |