From ae8b899c8213895385515929bf8531be0648cc88 Mon Sep 17 00:00:00 2001 From: rockcavera Date: Thu, 30 Dec 2021 14:07:10 -0300 Subject: [PATCH 1/3] Update lists.nim --- lib/pure/collections/lists.nim | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/pure/collections/lists.nim b/lib/pure/collections/lists.nim index 8b255fb603c97..80d33eeecdeea 100644 --- a/lib/pure/collections/lists.nim +++ b/lib/pure/collections/lists.nim @@ -530,11 +530,13 @@ proc addMoved*[T](a, b: var SinglyLinkedList[T]) {.since: (1, 5, 1).} = ci assert s == [0, 1, 0, 1, 0, 1] - if a.tail != nil: - a.tail.next = b.head - a.tail = b.tail - if a.head == nil: - a.head = b.head + if b.head != nil: + if a.tail != nil: + a.tail.next = b.head + if a.head == nil: + a.head = b.head + if b.tail != nil: + a.tail = b.tail if a.addr != b.addr: b.head = nil b.tail = nil From 45a064a5cb5716ab2bee18108018b6591b52a994 Mon Sep 17 00:00:00 2001 From: rockcavera Date: Thu, 30 Dec 2021 14:08:17 -0300 Subject: [PATCH 2/3] Update tlists.nim --- tests/stdlib/tlists.nim | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/stdlib/tlists.nim b/tests/stdlib/tlists.nim index 54fa8b24fd178..2f6d5ee6a58f0 100644 --- a/tests/stdlib/tlists.nim +++ b/tests/stdlib/tlists.nim @@ -233,6 +233,18 @@ template main = doAssert l.toSeq == [1] doAssert l.remove(l.head) == true doAssert l.toSeq == [] + + block issue19297: # add (appends a shallow copy) + var a: SinglyLinkedList[int] + var b: SinglyLinkedList[int] + + doAssert a.toSeq == @[] + a.add(1) + doAssert a.toSeq == @[1] + a.add(b) + doAssert a.toSeq == @[1] + a.add(2) + doAssert a.toSeq == @[1, 2] static: main() main() From 9e8f4c184b48e005ee39f545962b2aff94f42cb4 Mon Sep 17 00:00:00 2001 From: rockcavera Date: Thu, 30 Dec 2021 16:35:03 -0300 Subject: [PATCH 3/3] removed check `if b.tail != nil` The tail of the list being null it is still possible to retrieve its end by going through all nodes from the head. So checking for null from `b.tail` is unnecessary. However, setting `a.tail = b.tail` only if `a.head != nil`, so you don't break a good list with an already broken one. --- lib/pure/collections/lists.nim | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/pure/collections/lists.nim b/lib/pure/collections/lists.nim index 80d33eeecdeea..de5550d6e0842 100644 --- a/lib/pure/collections/lists.nim +++ b/lib/pure/collections/lists.nim @@ -531,11 +531,10 @@ proc addMoved*[T](a, b: var SinglyLinkedList[T]) {.since: (1, 5, 1).} = assert s == [0, 1, 0, 1, 0, 1] if b.head != nil: - if a.tail != nil: - a.tail.next = b.head if a.head == nil: a.head = b.head - if b.tail != nil: + else: + a.tail.next = b.head a.tail = b.tail if a.addr != b.addr: b.head = nil