newruntime: make system.delete and friends work with owned ref (bugfix); reported on the forum

(cherry picked from commit 61c66abbfe)
This commit is contained in:
Araq
2019-08-05 00:14:30 +02:00
committed by narimiran
parent b1cba3528b
commit c558efdb5c

View File

@@ -2111,6 +2111,13 @@ proc add*[T](x: var seq[T], y: openArray[T]) {.noSideEffect.} =
setLen(x, xl + y.len)
for i in 0..high(y): x[xl+i] = y[i]
when defined(nimV2):
template movingCopy(a, b) =
a = move(b)
else:
template movingCopy(a, b) =
shallowCopy(a, b)
proc del*[T](x: var seq[T], i: Natural) {.noSideEffect.} =
## Deletes the item at index `i` by putting ``x[high(x)]`` into position `i`.
##
@@ -2123,7 +2130,7 @@ proc del*[T](x: var seq[T], i: Natural) {.noSideEffect.} =
## var i = @[1, 2, 3, 4, 5]
## i.del(2) # => @[1, 2, 5, 4]
let xl = x.len - 1
shallowCopy(x[i], x[xl])
movingCopy(x[i], x[xl])
setLen(x, xl)
proc delete*[T](x: var seq[T], i: Natural) {.noSideEffect.} =
@@ -2139,7 +2146,7 @@ proc delete*[T](x: var seq[T], i: Natural) {.noSideEffect.} =
## i.delete(2) # => @[1, 2, 4, 5]
template defaultImpl =
let xl = x.len
for j in i.int..xl-2: shallowCopy(x[j], x[j+1])
for j in i.int..xl-2: movingCopy(x[j], x[j+1])
setLen(x, xl-1)
when nimvm:
@@ -2161,7 +2168,7 @@ proc insert*[T](x: var seq[T], item: T, i = 0.Natural) {.noSideEffect.} =
setLen(x, xl+1)
var j = xl-1
while j >= i:
shallowCopy(x[j+1], x[j])
movingCopy(x[j+1], x[j])
dec(j)
when nimvm:
defaultImpl()
@@ -3912,9 +3919,9 @@ template spliceImpl(s, a, L, b: untyped): untyped =
if shift > 0:
# enlarge:
setLen(s, newLen)
for i in countdown(newLen-1, a+b.len): shallowCopy(s[i], s[i-shift])
for i in countdown(newLen-1, a+b.len): movingCopy(s[i], s[i-shift])
else:
for i in countup(a+b.len, newLen-1): shallowCopy(s[i], s[i-shift])
for i in countup(a+b.len, newLen-1): movingCopy(s[i], s[i-shift])
# cut down:
setLen(s, newLen)
# fill the hole: