Merge pull request #7967 from nitely/empty_openarray_7904

support empty toOpenArray
This commit is contained in:
Andreas Rumpf 2018-06-06 11:06:57 +02:00 • committed by GitHub
commit 1593b9cac4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 10 deletions

View file

@ -83,7 +83,7 @@ proc isInCurrentFrame(p: BProc, n: PNode): bool =
result = isInCurrentFrame(p, n.sons[0]) result = isInCurrentFrame(p, n.sons[0])
else: discard else: discard
proc genIndexCheck(p: BProc; arr, idx: TLoc) proc genBoundsCheck(p: BProc; arr, a, b: TLoc)
proc openArrayLoc(p: BProc, n: PNode): Rope = proc openArrayLoc(p: BProc, n: PNode): Rope =
var a: TLoc var a: TLoc
@ -97,8 +97,7 @@ proc openArrayLoc(p: BProc, n: PNode): Rope =
initLocExpr(p, q[3], c) initLocExpr(p, q[3], c)
# but first produce the required index checks: # but first produce the required index checks:
if optBoundsCheck in p.options: if optBoundsCheck in p.options:
genIndexCheck(p, a, b) genBoundsCheck(p, a, b, c)
genIndexCheck(p, a, c)
let ty = skipTypes(a.t, abstractVar+{tyPtr}) let ty = skipTypes(a.t, abstractVar+{tyPtr})
case ty.kind case ty.kind
of tyArray: of tyArray:

View file

@ -873,21 +873,26 @@ proc genCStringElem(p: BProc, n, x, y: PNode, d: var TLoc) =
putIntoDest(p, d, n, putIntoDest(p, d, n,
ropecg(p.module, "$1[$2]", rdLoc(a), rdCharLoc(b)), a.storage) ropecg(p.module, "$1[$2]", rdLoc(a), rdCharLoc(b)), a.storage)
proc genIndexCheck(p: BProc; arr, idx: TLoc) = proc genBoundsCheck(p: BProc; arr, a, b: TLoc) =
let ty = skipTypes(arr.t, abstractVarRange) let ty = skipTypes(arr.t, abstractVarRange)
case ty.kind case ty.kind
of tyOpenArray, tyVarargs: of tyOpenArray, tyVarargs:
linefmt(p, cpsStmts, "if ((NU)($1) >= (NU)($2Len_0)) #raiseIndexError();$n", linefmt(p, cpsStmts,
rdLoc(idx), rdLoc(arr)) "if ($2-$1 != -1 && " &
"((NU)($1) >= (NU)($3Len_0) || (NU)($2) >= (NU)($3Len_0))) #raiseIndexError();$n",
rdLoc(a), rdLoc(b), rdLoc(arr))
of tyArray: of tyArray:
let first = intLiteral(firstOrd(ty)) let first = intLiteral(firstOrd(ty))
if tfUncheckedArray notin ty.flags: if tfUncheckedArray notin ty.flags:
linefmt(p, cpsStmts, "if ($1 < $2 || $1 > $3) #raiseIndexError();$n", linefmt(p, cpsStmts,
rdCharLoc(idx), first, intLiteral(lastOrd(ty))) "if ($2-$1 != -1 && " &
"($2-$1 < -1 || $1 < $3 || $1 > $4 || $2 < $3 || $2 > $4)) #raiseIndexError();$n",
rdCharLoc(a), rdCharLoc(b), first, intLiteral(lastOrd(ty)))
of tySequence, tyString: of tySequence, tyString:
linefmt(p, cpsStmts, linefmt(p, cpsStmts,
"if (!$2 || (NU)($1) >= (NU)($2->$3)) #raiseIndexError();$n", "if ($2-$1 != -1 && " &
rdLoc(idx), rdLoc(arr), lenField(p)) "(!$3 || (NU)($1) >= (NU)($3->$4) || (NU)($2) >= (NU)($3->$4))) #raiseIndexError();$n",
rdLoc(a), rdLoc(b), rdLoc(arr), lenField(p))
else: discard else: discard
proc genOpenArrayElem(p: BProc, n, x, y: PNode, d: var TLoc) = proc genOpenArrayElem(p: BProc, n, x, y: PNode, d: var TLoc) =

View file

@ -11,6 +11,10 @@ discard """
2 2
3 3
4 4
2
1
2
3
''' '''
""" """
@ -47,3 +51,38 @@ foo(toOpenArray(arr, 8, 12))
var seqq = @[1, 2, 3, 4, 5] var seqq = @[1, 2, 3, 4, 5]
foo(toOpenArray(seqq, 1, 3)) foo(toOpenArray(seqq, 1, 3))
# empty openArray issue #7904
foo(toOpenArray(seqq, 0, -1))
foo(toOpenArray(seqq, 1, 0))
doAssertRaises(IndexError):
foo(toOpenArray(seqq, 0, -2))
foo(toOpenArray(arr, 9, 8))
foo(toOpenArray(arr, 0, -1))
foo(toOpenArray(arr, 1, 0))
doAssertRaises(IndexError):
foo(toOpenArray(arr, 10, 8))
# test openArray of openArray
proc oaEmpty(a: openArray[int]) =
foo(toOpenArray(a, 0, -1))
proc oaFirstElm(a: openArray[int]) =
foo(toOpenArray(a, 0, 0))
oaEmpty(toOpenArray(seqq, 0, -1))
oaEmpty(toOpenArray(seqq, 1, 0))
oaEmpty(toOpenArray(seqq, 1, 2))
oaFirstElm(toOpenArray(seqq, 1, seqq.len-1))
var arrNeg: array[-3 .. -1, int] = [1, 2, 3]
foo(toOpenArray(arrNeg, -3, -1))
foo(toOpenArray(arrNeg, 0, -1))
foo(toOpenArray(arrNeg, -3, -4))
doAssertRaises(IndexError):
foo(toOpenArray(arrNeg, -4, -1))
doAssertRaises(IndexError):
foo(toOpenArray(arrNeg, -1, 0))
doAssertRaises(IndexError):
foo(toOpenArray(arrNeg, -1, -3))