disallow negative indexing
This commit is contained in:
parent
96d15e9258
commit
ed0d9e271d
7 changed files with 164 additions and 148 deletions
|
|
@ -557,6 +557,7 @@ type
|
|||
mIntToStr, mInt64ToStr, mFloatToStr, mCStrToStr, mStrToStr, mEnumToStr,
|
||||
mAnd, mOr, mEqStr, mLeStr, mLtStr, mEqSet, mLeSet, mLtSet, mMulSet,
|
||||
mPlusSet, mMinusSet, mSymDiffSet, mConStrStr, mSlice,
|
||||
mDotDot, # this one is only necessary to give nice compile time warnings
|
||||
mFields, mFieldPairs, mOmpParFor,
|
||||
mAppendStrCh, mAppendStrStr, mAppendSeqElem,
|
||||
mInRange, mInSet, mRepr, mExit, mSetLengthStr, mSetLengthSeq,
|
||||
|
|
|
|||
|
|
@ -1749,6 +1749,7 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
|
|||
initLocExpr(p, x, a)
|
||||
initLocExpr(p, e.sons[2], b)
|
||||
genDeepCopy(p, a, b)
|
||||
of mDotDot: genCall(p, e, d)
|
||||
else: internalError(e.info, "genMagicExpr: " & $op)
|
||||
|
||||
proc genConstExpr(p: BProc, n: PNode): PRope
|
||||
|
|
|
|||
|
|
@ -419,7 +419,7 @@ proc evalOp(m: TMagic, n, a, b, c: PNode): PNode =
|
|||
of mNewString, mNewStringOfCap,
|
||||
mExit, mInc, ast.mDec, mEcho, mSwap, mAppendStrCh,
|
||||
mAppendStrStr, mAppendSeqElem, mSetLengthStr, mSetLengthSeq,
|
||||
mParseExprToAst, mParseStmtToAst, mExpandToAst, mTypeTrait,
|
||||
mParseExprToAst, mParseStmtToAst, mExpandToAst, mTypeTrait, mDotDot,
|
||||
mNLen..mNError, mEqRef, mSlurp, mStaticExec, mNGenSym, mSpawn, mParallel:
|
||||
discard
|
||||
else: internalError(a.info, "evalOp(" & $m & ')')
|
||||
|
|
|
|||
|
|
@ -135,6 +135,13 @@ proc isStrangeArray(t: PType): bool =
|
|||
let t = t.skipTypes(abstractInst)
|
||||
result = t.kind == tyArray and t.firstOrd != 0
|
||||
|
||||
proc isNegative(n: PNode): bool =
|
||||
let n = n.skipConv
|
||||
if n.kind in {nkCharLit..nkUInt64Lit}:
|
||||
result = n.intVal < 0
|
||||
elif n.kind in nkCallKinds and n.sons[0].kind == nkSym:
|
||||
result = n.sons[0].sym.magic in {mUnaryMinusI, mUnaryMinusI64}
|
||||
|
||||
proc magicsAfterOverloadResolution(c: PContext, n: PNode,
|
||||
flags: TExprFlags): PNode =
|
||||
case n[0].sym.magic
|
||||
|
|
@ -158,6 +165,12 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
|
|||
of mProcCall:
|
||||
result = n
|
||||
result.typ = n[1].typ
|
||||
of mDotDot:
|
||||
result = n
|
||||
# we only need to warnings here about negative indexing:
|
||||
if isNegative(n.sons[1]) or (n.len > 2 and isNegative(n.sons[2])):
|
||||
message(n.info, warnDeprecated,
|
||||
"use '^' instead of '-'; negative indexing")
|
||||
of mRoof:
|
||||
# error correction:
|
||||
result = n.sons[1]
|
||||
|
|
|
|||
|
|
@ -228,7 +228,7 @@ type
|
|||
when defined(nimalias):
|
||||
{.deprecated: [TSlice: Slice].}
|
||||
|
||||
proc `..`*[T](a, b: T): Slice[T] {.noSideEffect, inline.} =
|
||||
proc `..`*[T](a, b: T): Slice[T] {.noSideEffect, inline, magic: "DotDot".} =
|
||||
## `slice`:idx: operator that constructs an interval ``[a, b]``, both `a`
|
||||
## and `b` are inclusive. Slices can also be used in the set constructor
|
||||
## and in ordinal case statements, but then they are special-cased by the
|
||||
|
|
@ -236,7 +236,7 @@ proc `..`*[T](a, b: T): Slice[T] {.noSideEffect, inline.} =
|
|||
result.a = a
|
||||
result.b = b
|
||||
|
||||
proc `..`*[T](b: T): Slice[T] {.noSideEffect, inline.} =
|
||||
proc `..`*[T](b: T): Slice[T] {.noSideEffect, inline, magic: "DotDot".} =
|
||||
## `slice`:idx: operator that constructs an interval ``[default(T), b]``
|
||||
result.b = b
|
||||
|
||||
|
|
@ -2864,11 +2864,11 @@ template spliceImpl(s, a, L, b: expr): stmt {.immediate.} =
|
|||
|
||||
when hostOS != "standalone":
|
||||
proc `[]`*(s: string, x: Slice[int]): string {.inline.} =
|
||||
## slice operation for strings. Negative indexes are supported.
|
||||
## slice operation for strings.
|
||||
result = s.substr(x.a-|s, x.b-|s)
|
||||
|
||||
proc `[]=`*(s: var string, x: Slice[int], b: string) =
|
||||
## slice assignment for strings. Negative indexes are supported. If
|
||||
## slice assignment for strings. If
|
||||
## ``b.len`` is not exactly the number of elements that are referred to
|
||||
## by `x`, a `splice`:idx: is performed:
|
||||
##
|
||||
|
|
@ -2884,8 +2884,7 @@ when hostOS != "standalone":
|
|||
spliceImpl(s, a, L, b)
|
||||
|
||||
proc `[]`*[Idx, T](a: array[Idx, T], x: Slice[int]): seq[T] =
|
||||
## slice operation for arrays. Negative indexes are **not** supported
|
||||
## because the array might have negative bounds.
|
||||
## slice operation for arrays.
|
||||
when low(a) < 0:
|
||||
{.error: "Slicing for arrays with negative indices is unsupported.".}
|
||||
var L = x.b - x.a + 1
|
||||
|
|
@ -2893,8 +2892,7 @@ proc `[]`*[Idx, T](a: array[Idx, T], x: Slice[int]): seq[T] =
|
|||
for i in 0.. <L: result[i] = a[i + x.a]
|
||||
|
||||
proc `[]=`*[Idx, T](a: var array[Idx, T], x: Slice[int], b: openArray[T]) =
|
||||
## slice assignment for arrays. Negative indexes are **not** supported
|
||||
## because the array might have negative bounds.
|
||||
## slice assignment for arrays.
|
||||
when low(a) < 0:
|
||||
{.error: "Slicing for arrays with negative indices is unsupported.".}
|
||||
var L = x.b - x.a + 1
|
||||
|
|
@ -2904,16 +2902,14 @@ proc `[]=`*[Idx, T](a: var array[Idx, T], x: Slice[int], b: openArray[T]) =
|
|||
sysFatal(RangeError, "different lengths for slice assignment")
|
||||
|
||||
proc `[]`*[Idx, T](a: array[Idx, T], x: Slice[Idx]): seq[T] =
|
||||
## slice operation for arrays. Negative indexes are **not** supported
|
||||
## because the array might have negative bounds.
|
||||
## slice operation for arrays.
|
||||
var L = ord(x.b) - ord(x.a) + 1
|
||||
newSeq(result, L)
|
||||
for i in 0.. <L:
|
||||
result[i] = a[Idx(ord(x.a) + i)]
|
||||
|
||||
proc `[]=`*[Idx, T](a: var array[Idx, T], x: Slice[Idx], b: openArray[T]) =
|
||||
## slice assignment for arrays. Negative indexes are **not** supported
|
||||
## because the array might have negative bounds.
|
||||
## slice assignment for arrays.
|
||||
var L = ord(x.b) - ord(x.a) + 1
|
||||
if L == b.len:
|
||||
for i in 0 .. <L:
|
||||
|
|
@ -2922,14 +2918,14 @@ proc `[]=`*[Idx, T](a: var array[Idx, T], x: Slice[Idx], b: openArray[T]) =
|
|||
sysFatal(RangeError, "different lengths for slice assignment")
|
||||
|
||||
proc `[]`*[T](s: seq[T], x: Slice[int]): seq[T] =
|
||||
## slice operation for sequences. Negative indexes are supported.
|
||||
## slice operation for sequences.
|
||||
var a = x.a-|s
|
||||
var L = x.b-|s - a + 1
|
||||
newSeq(result, L)
|
||||
for i in 0.. <L: result[i] = s[i + a]
|
||||
|
||||
proc `[]=`*[T](s: var seq[T], x: Slice[int], b: openArray[T]) =
|
||||
## slice assignment for sequences. Negative indexes are supported. If
|
||||
## slice assignment for sequences. If
|
||||
## ``b.len`` is not exactly the number of elements that are referred to
|
||||
## by `x`, a `splice`:idx: is performed.
|
||||
var a = x.a-|s
|
||||
|
|
|
|||
1
todo.txt
1
todo.txt
|
|
@ -6,7 +6,6 @@ version 0.10.4
|
|||
- make 'nil' work for 'add' and 'len'
|
||||
- add "all threads are blocked" detection to 'spawn'
|
||||
- overloading of '='
|
||||
- disallow negative indexing
|
||||
|
||||
|
||||
version 1.0
|
||||
|
|
|
|||
|
|
@ -59,6 +59,12 @@ News
|
|||
echo x.T
|
||||
inc x
|
||||
|
||||
- **Negative indexing for slicing is deprecated and will be removed in the
|
||||
next version of the language.** Instead of ``a[0.. -1]`` you can
|
||||
use ``a[0.. ^1]``. This also works with accessing a single
|
||||
element ``a[^1]``. Note that we cannot detect this reliably as it is
|
||||
determined at **runtime** whether negative indexing is used!
|
||||
|
||||
|
||||
Language Additions
|
||||
------------------
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue