Empty check in shallow [backport] (#9676)

This commit is contained in:
ishowta
2018-11-21 19:56:38 +09:00
committed by Andreas Rumpf
parent b78699917e
commit 6ff596d4f8
2 changed files with 25 additions and 0 deletions

View File

@@ -4017,6 +4017,7 @@ proc shallow*[T](s: var seq[T]) {.noSideEffect, inline.} =
## marks a sequence `s` as `shallow`:idx:. Subsequent assignments will not
## perform deep copies of `s`. This is only useful for optimization
## purposes.
if s.len == 0: return
when not defined(JS) and not defined(nimscript):
var s = cast[PGenericSeq](s)
s.reserved = s.reserved or seqShallowFlag

View File

@@ -170,6 +170,30 @@ block tshallowseq:
xxx()
block tshallowemptyseq:
proc test() =
var nilSeq: seq[int] = @[]
var emptySeq: seq[int] = newSeq[int]()
block:
var t = @[1,2,3]
shallow(nilSeq)
t = nilSeq
doAssert t == @[]
block:
var t = @[1,2,3]
shallow(emptySeq)
t = emptySeq
doAssert t == @[]
block:
var t = @[1,2,3]
shallowCopy(t, nilSeq)
doAssert t == @[]
block:
var t = @[1,2,3]
shallowCopy(t, emptySeq)
doAssert t == @[]
test()
import strutils
block ttoseq: