From 2fbb793f4e32cbf866da8d832c87c663fa32e67b Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Tue, 16 Sep 2025 23:05:09 +0800 Subject: [PATCH] fixes #25173; SinglyLinkedList.remove broken / AssertionDefect (#25175) fixes #25173 (cherry picked from commit 51a9ada0436958ba3c3423802dd0d26dec88e18b) --- lib/pure/collections/lists.nim | 2 ++ tests/stdlib/tlists.nim | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/pure/collections/lists.nim b/lib/pure/collections/lists.nim index 0e52d5ca00..81983269ef 100644 --- a/lib/pure/collections/lists.nim +++ b/lib/pure/collections/lists.nim @@ -710,6 +710,8 @@ proc remove*[T](L: var SinglyLinkedList[T], n: SinglyLinkedNode[T]): bool {.disc L.head = n.next if L.tail.next == n: L.tail.next = L.head # restore cycle + if L.tail == n: + L.tail = nil # reset tail if we removed the last node else: var prev = L.head while prev.next != n and prev.next != nil: diff --git a/tests/stdlib/tlists.nim b/tests/stdlib/tlists.nim index 5993278c79..9339a6df05 100644 --- a/tests/stdlib/tlists.nim +++ b/tests/stdlib/tlists.nim @@ -273,5 +273,17 @@ template main = list.add(n4) doAssert list.toSeq == @["sonic", "the", "hedgehog"] + + block: + var list = initSinglyLinkedList[int]() + + list.add(4) + list.remove(list.head) + + list.add(5) + list.remove(list.head) + + list.add(6) + static: main() main()