From d6d7653f250bac41ddfbfbb3b1c45f0b4409e07f Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Sat, 29 Aug 2026 20:41:36 +0800 Subject: [PATCH] =?UTF-8?q?fixes=20#26134;=20del(seq)=20performs=20self-as?= =?UTF-8?q?signment=20and=20=3Ddestroy=20for=20del(=E2=80=A6=20(#26138)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit …0) of 1-length seq fixes #26134 (cherry picked from commit dcec8e1cd1a33b3dfa6a9fa38784d0c9195b6aca) --- lib/system.nim | 4 +++- tests/stdlib/t26134.nim | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/stdlib/t26134.nim diff --git a/lib/system.nim b/lib/system.nim index ef241136b4..e053b2d67e 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -1257,7 +1257,9 @@ proc del*[T](x: var seq[T], i: Natural) {.noSideEffect.} = a.del(2) assert a == @[10, 11, 14, 13] let xl = x.len - 1 - movingCopy(x[i], x[xl]) + # Avoid moving the element onto itself when deleting the last item. + if i != xl: + movingCopy(x[i], x[xl]) setLen(x, xl) proc insert*[T](x: var seq[T], item: sink T, i = 0.Natural) {.noSideEffect.} = diff --git a/tests/stdlib/t26134.nim b/tests/stdlib/t26134.nim new file mode 100644 index 0000000000..2d2d916665 --- /dev/null +++ b/tests/stdlib/t26134.nim @@ -0,0 +1,24 @@ +discard """ + matrix: "--mm:orc --undef:nimPreviewNonVarDestructor" + output: "hello" +""" + +# bug #26134 + +type MyObject = object + +proc `=destroy`(v: var MyObject) = + echo "hello" + +proc remove(v: var seq[MyObject]) = + v.del(0) + +proc aaa(v: var seq[MyObject], i: sink MyObject) = + v.add(i) + +proc main = + var v: seq[MyObject] + v.aaa(MyObject()) + v.remove() + +main()