From 61c66abbfea43752acb73d5c451fef5fce8e0d35 Mon Sep 17 00:00:00 2001 From: Araq Date: Mon, 5 Aug 2019 00:14:30 +0200 Subject: [PATCH] newruntime: make system.delete and friends work with owned ref (bugfix); reported on the forum --- lib/system.nim | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/system.nim b/lib/system.nim index 28eb1a1ef4..3db7d93f12 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -143,7 +143,7 @@ when defined(nimHasRunnableExamples): ## assert double(5) == 10 ## block: ## at block scope ## defer: echo "done" - ## + ## ## result = 2 * x else: template runnableExamples*(body: untyped) = @@ -2113,6 +2113,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`. ## @@ -2125,7 +2132,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.} = @@ -2141,7 +2148,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: @@ -2163,7 +2170,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() @@ -3914,9 +3921,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: