fixes #2420; negative indexing for slicing is obsolete (breaking change!)

This commit is contained in:
Araq 2015-03-28 00:15:04 +01:00
commit 2b80d75aa2
8 changed files with 82 additions and 79 deletions

View file

@ -201,7 +201,8 @@ proc resolveOverloads(c: PContext, n, orig: PNode,
elif nfDotSetter in n.flags: elif nfDotSetter in n.flags:
internalAssert f.kind == nkIdent and n.sonsLen == 3 internalAssert f.kind == nkIdent and n.sonsLen == 3
let calleeName = newStrNode(nkStrLit, f.ident.s[0.. -2]).withInfo(n.info) let calleeName = newStrNode(nkStrLit,
f.ident.s[0..f.ident.s.len-2]).withInfo(n.info)
let callOp = newIdentNode(getIdent".=", n.info) let callOp = newIdentNode(getIdent".=", n.info)
n.sons[0..1] = [callOp, n[1], calleeName] n.sons[0..1] = [callOp, n[1], calleeName]
orig.sons[0..1] = [callOp, orig[1], calleeName] orig.sons[0..1] = [callOp, orig[1], calleeName]

View file

@ -71,11 +71,12 @@ proc destroyCase(c: PContext, n: PNode, holder: PNode): PNode =
result.addSon(newNode(nkDotExpr, n.info, @[holder, n.sons[0]])) result.addSon(newNode(nkDotExpr, n.info, @[holder, n.sons[0]]))
for i in countup(1, n.len - 1): for i in countup(1, n.len - 1):
# of A, B: # of A, B:
var caseBranch = newNode(n[i].kind, n[i].info, n[i].sons[0 .. -2]) let ni = n[i]
var caseBranch = newNode(ni.kind, ni.info, ni.sons[0..ni.len-2])
let stmt = destroyFieldOrFields(c, n[i].lastSon, holder) let stmt = destroyFieldOrFields(c, ni.lastSon, holder)
if stmt == nil: if stmt == nil:
caseBranch.addSon(newNode(nkStmtList, n[i].info, @[])) caseBranch.addSon(newNode(nkStmtList, ni.info, @[]))
else: else:
caseBranch.addSon(stmt) caseBranch.addSon(stmt)
nonTrivialFields += stmt.len nonTrivialFields += stmt.len
@ -209,7 +210,7 @@ proc insertDestructors(c: PContext,
if j < totalVars - 1: if j < totalVars - 1:
var remainingVars = newNodeI(varSection.kind, info) var remainingVars = newNodeI(varSection.kind, info)
remainingVars.sons = varSection.sons[(j+1)..(-1)] remainingVars.sons = varSection.sons[(j+1)..varSection.len-1]
let (outer, inner) = insertDestructors(c, remainingVars) let (outer, inner) = insertDestructors(c, remainingVars)
if outer != nil: if outer != nil:
tryStmt.addSon(outer) tryStmt.addSon(outer)

View file

@ -167,10 +167,10 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
result.typ = n[1].typ result.typ = n[1].typ
of mDotDot: of mDotDot:
result = n result = n
# we only need to warnings here about negative indexing: # disallow negative indexing for now:
if not c.p.bracketExpr.isNil:
if isNegative(n.sons[1]) or (n.len > 2 and isNegative(n.sons[2])): if isNegative(n.sons[1]) or (n.len > 2 and isNegative(n.sons[2])):
message(n.info, warnDeprecated, localError(n.info, "use '^' instead of '-'; negative indexing is obsolete")
"use '^' instead of '-'; negative indexing")
of mRoof: of mRoof:
# error correction: # error correction:
result = n.sons[1] result = n.sons[1]

View file

@ -1281,7 +1281,7 @@ proc semStmtList(c: PContext, n: PNode, flags: TExprFlags): PNode =
var tryStmt = newNodeI(nkTryStmt, n.sons[i].info) var tryStmt = newNodeI(nkTryStmt, n.sons[i].info)
var body = newNodeI(nkStmtList, n.sons[i].info) var body = newNodeI(nkStmtList, n.sons[i].info)
if i < n.sonsLen - 1: if i < n.sonsLen - 1:
body.sons = n.sons[(i+1)..(-1)] body.sons = n.sons[(i+1)..n.len-1]
tryStmt.addSon(body) tryStmt.addSon(body)
tryStmt.addSon(deferPart) tryStmt.addSon(deferPart)
n.sons[i] = semTry(c, tryStmt) n.sons[i] = semTry(c, tryStmt)

View file

@ -256,7 +256,7 @@ proc asyncSockHandleWrite(h: RootRef) =
# do nothing instead. # do nothing instead.
discard discard
elif bytesSent != sock.sendBuffer.len: elif bytesSent != sock.sendBuffer.len:
sock.sendBuffer = sock.sendBuffer[bytesSent .. -1] sock.sendBuffer = sock.sendBuffer[bytesSent .. ^1]
elif bytesSent == sock.sendBuffer.len: elif bytesSent == sock.sendBuffer.len:
sock.sendBuffer = "" sock.sendBuffer = ""
@ -550,7 +550,7 @@ proc send*(sock: AsyncSocket, data: string) =
sock.sendBuffer.add(data) sock.sendBuffer.add(data)
sock.deleg.mode = fmReadWrite sock.deleg.mode = fmReadWrite
elif bytesSent != data.len: elif bytesSent != data.len:
sock.sendBuffer.add(data[bytesSent .. -1]) sock.sendBuffer.add(data[bytesSent .. ^1])
sock.deleg.mode = fmReadWrite sock.deleg.mode = fmReadWrite
proc timeValFromMilliseconds(timeout = 500): Timeval = proc timeValFromMilliseconds(timeout = 500): Timeval =

View file

@ -2865,7 +2865,7 @@ template spliceImpl(s, a, L, b: expr): stmt {.immediate.} =
when hostOS != "standalone": when hostOS != "standalone":
proc `[]`*(s: string, x: Slice[int]): string {.inline.} = proc `[]`*(s: string, x: Slice[int]): string {.inline.} =
## slice operation for strings. ## slice operation for strings.
result = s.substr(x.a-|s, x.b-|s) result = s.substr(x.a, x.b)
proc `[]=`*(s: var string, x: Slice[int], b: string) = proc `[]=`*(s: var string, x: Slice[int], b: string) =
## slice assignment for strings. If ## slice assignment for strings. If
@ -2876,8 +2876,8 @@ when hostOS != "standalone":
## var s = "abcdef" ## var s = "abcdef"
## s[1 .. -2] = "xyz" ## s[1 .. -2] = "xyz"
## assert s == "axyzf" ## assert s == "axyzf"
var a = x.a-|s var a = x.a
var L = x.b-|s - a + 1 var L = x.b - a + 1
if L == b.len: if L == b.len:
for i in 0 .. <L: s[i+a] = b[i] for i in 0 .. <L: s[i+a] = b[i]
else: else:
@ -2919,8 +2919,8 @@ proc `[]=`*[Idx, T](a: var array[Idx, T], x: Slice[Idx], b: openArray[T]) =
proc `[]`*[T](s: seq[T], x: Slice[int]): seq[T] = proc `[]`*[T](s: seq[T], x: Slice[int]): seq[T] =
## slice operation for sequences. ## slice operation for sequences.
var a = x.a-|s var a = x.a
var L = x.b-|s - a + 1 var L = x.b - a + 1
newSeq(result, L) newSeq(result, L)
for i in 0.. <L: result[i] = s[i + a] for i in 0.. <L: result[i] = s[i + a]
@ -2928,8 +2928,8 @@ proc `[]=`*[T](s: var seq[T], x: Slice[int], b: openArray[T]) =
## slice assignment for sequences. If ## slice assignment for sequences. If
## ``b.len`` is not exactly the number of elements that are referred to ## ``b.len`` is not exactly the number of elements that are referred to
## by `x`, a `splice`:idx: is performed. ## by `x`, a `splice`:idx: is performed.
var a = x.a-|s var a = x.a
var L = x.b-|s - a + 1 var L = x.b - a + 1
if L == b.len: if L == b.len:
for i in 0 .. <L: s[i+a] = b[i] for i in 0 .. <L: s[i+a] = b[i]
else: else:

View file

@ -59,11 +59,12 @@ News
echo x.T echo x.T
inc x inc x
- **Negative indexing for slicing is deprecated and will be removed in the - **Negative indexing for slicing does not work anymore!** Instead
next version of the language.** Instead of ``a[0.. -1]`` you can of ``a[0.. -1]`` you can
use ``a[0.. ^1]``. This also works with accessing a single use ``a[0.. ^1]``. This also works with accessing a single
element ``a[^1]``. Note that we cannot detect this reliably as it is element ``a[^1]``. Note that we cannot detect this reliably as it is
determined at **runtime** whether negative indexing is used! determined at **runtime** whether negative indexing is used!
``a[0.. -1]`` now produces the empty string/sequence.
- The compiler now warns about code like ``foo +=1`` which uses inconsistent - The compiler now warns about code like ``foo +=1`` which uses inconsistent
spacing around binary operators. Later versions of the language will parse spacing around binary operators. Later versions of the language will parse
these as unary operators instead so that ``echo $foo`` finally can do what these as unary operators instead so that ``echo $foo`` finally can do what