Skip to content

Commit 984691b

Browse files
rockcaveranarimiran
authored andcommitted
Fix #19314 - fixing broken DoublyLinkedList after adding empty DoublyLinkedList (#19315) [backport]
* Update lists.nim * Update tlists.nim (cherry picked from commit 526a32e)
1 parent 5f70b1a commit 984691b

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

lib/pure/collections/lists.nim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -676,12 +676,12 @@ proc addMoved*[T](a, b: var DoublyLinkedList[T]) {.since: (1, 5, 1).} =
676676
assert s == [0, 1, 0, 1, 0, 1]
677677

678678
if b.head != nil:
679-
b.head.prev = a.tail
680-
if a.tail != nil:
681-
a.tail.next = b.head
682-
a.tail = b.tail
683-
if a.head == nil:
684-
a.head = b.head
679+
if a.head == nil:
680+
a.head = b.head
681+
else:
682+
b.head.prev = a.tail
683+
a.tail.next = b.head
684+
a.tail = b.tail
685685
if a.addr != b.addr:
686686
b.head = nil
687687
b.tail = nil

tests/stdlib/tlists.nim

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,18 @@ template main =
245245
doAssert a.toSeq == @[1]
246246
a.add(2)
247247
doAssert a.toSeq == @[1, 2]
248+
249+
block issue19314: # add (appends a shallow copy)
250+
var a: DoublyLinkedList[int]
251+
var b: DoublyLinkedList[int]
252+
253+
doAssert a.toSeq == @[]
254+
a.add(1)
255+
doAssert a.toSeq == @[1]
256+
a.add(b)
257+
doAssert a.toSeq == @[1]
258+
a.add(2)
259+
doAssert a.toSeq == @[1, 2]
248260

249261
static: main()
250262
main()

0 commit comments

Comments
 (0)