diff options
Diffstat (limited to 'tests/array')
-rw-r--r-- | tests/array/troof1.nim | 45 | ||||
-rw-r--r-- | tests/array/troof2.nim | 10 | ||||
-rw-r--r-- | tests/array/troof3.nim | 5 | ||||
-rw-r--r-- | tests/array/troof4.nim | 37 | ||||
-rw-r--r-- | tests/array/troofregression.nim | 46 |
5 files changed, 74 insertions, 69 deletions
diff --git a/tests/array/troof1.nim b/tests/array/troof1.nim index 96669a121..b486c3448 100644 --- a/tests/array/troof1.nim +++ b/tests/array/troof1.nim @@ -1,7 +1,11 @@ discard """ output: '''@[2, 3, 4]321 9.0 4.0 -(a: 1.0, b: 2.0, c: 8.0)2.0''' +3 +@[(Field0: 1, Field1: 2), (Field0: 3, Field1: 5)] +2 +@["a", "new one", "c"] +@[1, 2, 3]''' """ proc foo[T](x, y: T): T = x @@ -11,26 +15,29 @@ var b: array[3, array[2, float]] = [[1.0,2], [3.0,4], [8.0,9]] echo a[1.. ^1], a[^2], a[^3], a[^4] echo b[^1][^1], " ", (b[^2]).foo(b[^1])[^1] -type - MyArray = object - a, b, c: float +b[^1] = [8.8, 8.9] -var - ma = MyArray(a: 1.0, b: 2.0, c: 3.0) +var c: seq[(int, int)] = @[(1,2), (3,4)] -proc len(x: MyArray): int = 3 +proc takeA(x: ptr int) = echo x[] -proc `[]=`(x: var MyArray; idx: range[0..2]; val: float) = - case idx - of 0: x.a = val - of 1: x.b = val - of 2: x.c = val +takeA(addr c[^1][0]) +c[^1][1] = 5 +echo c -proc `[]`(x: var MyArray; idx: range[0..2]): float = - case idx - of 0: result = x.a - of 1: result = x.b - of 2: result = x.c +proc useOpenarray(x: openArray[int]) = + echo x[^2] -ma[^1] = 8.0 -echo ma, ma[^2] +proc mutOpenarray(x: var openArray[string]) = + x[^2] = "new one" + +useOpenarray([1, 2, 3]) + +var z = @["a", "b", "c"] +mutOpenarray(z) +echo z + +# bug #6675 +var y: array[1..5, int] = [1,2,3,4,5] +y[3..5] = [1, 2, 3] +echo y[3..5] diff --git a/tests/array/troof2.nim b/tests/array/troof2.nim deleted file mode 100644 index e4b4f4b3c..000000000 --- a/tests/array/troof2.nim +++ /dev/null @@ -1,10 +0,0 @@ -discard """ - errormsg: "invalid context for '^' as 'foo()' has side effects" - line: "9" -""" -# XXX This needs to be fixed properly! -proc foo(): seq[int] {.sideEffect.} = - echo "ha" - -let f = foo()[^1] - diff --git a/tests/array/troof3.nim b/tests/array/troof3.nim index 4b6e22223..efe0eafb8 100644 --- a/tests/array/troof3.nim +++ b/tests/array/troof3.nim @@ -1,8 +1,7 @@ discard """ - errormsg: "invalid context for '^' as len!=high+1 for 'a'" - line: "8" + output: '''c''' """ -var a: array[1..3, string] +var a: array['a'..'c', string] = ["a", "b", "c"] echo a[^1] diff --git a/tests/array/troof4.nim b/tests/array/troof4.nim deleted file mode 100644 index 7a262d9de..000000000 --- a/tests/array/troof4.nim +++ /dev/null @@ -1,37 +0,0 @@ -discard """ - errormsg: "no surrounding array access context for '^'" - line: "37" -""" - -proc foo[T](x, y: T): T = x - -var a = @[1, 2, 3, 4] -var b: array[3, array[2, float]] = [[1.0,2], [3.0,4], [8.0,9]] -echo a[1.. ^1], a[^2], a[^3], a[^4] -echo b[^1][^1], " ", (b[^2]).foo(b[^1])[^1] - -type - MyArray = object - a, b, c: float - -var - ma = MyArray(a: 1.0, b: 2.0, c: 3.0) - -proc len(x: MyArray): int = 3 - -proc `[]=`(x: var MyArray; idx: range[0..2]; val: float) = - case idx - of 0: x.a = val - of 1: x.b = val - of 2: x.c = val - -proc `[]`(x: var MyArray; idx: range[0..2]): float = - case idx - of 0: result = x.a - of 1: result = x.b - of 2: result = x.c - -ma[^1] = 8.0 -echo ma, ma[^2] - -echo(^1) diff --git a/tests/array/troofregression.nim b/tests/array/troofregression.nim new file mode 100644 index 000000000..0b96123a4 --- /dev/null +++ b/tests/array/troofregression.nim @@ -0,0 +1,46 @@ +############################### +#### part from Arraymancer + +type + MetadataArray* = object + data*: array[8, int] + len*: int + +# Commenting the converter removes the error "lib/system.nim(3536, 3) Error: for a 'var' type a variable needs to be passed" +converter toMetadataArray*(se: varargs[int]): MetadataArray {.inline.} = + result.len = se.len + for i in 0..<se.len: + result.data[i] = se[i] + + +when NimVersion >= "0.17.3": + type Index = int or BackwardsIndex + template `^^`(s, i: untyped): untyped = + when i is BackwardsIndex: + s.len - int(i) + else: i +else: + type Index = int + template `^^`(s, i: untyped): untyped = + i + +## With Nim devel from the start of the week (~Oct30) I managed to trigger "lib/system.nim(3536, 4) Error: expression has no address" +## but I can't anymore after updating Nim (Nov5) +## Now commenting this plain compiles and removes the error "lib/system.nim(3536, 3) Error: for a 'var' type a variable needs to be passed" +proc `[]`*(a: var MetadataArray, idx: Index): var int {.inline.} = + a.data[a ^^ idx] + + +############################## +### Completely unrelated lib that triggers the issue + +type + MySeq[T] = ref object + data: seq[T] + +proc test[T](sx: MySeq[T]) = + # Removing the backward index removes the error "lib/system.nim(3536, 3) Error: for a 'var' type a variable needs to be passed" + echo sx.data[^1] # error here + +let s = MySeq[int](data: @[1, 2, 3]) +s.test() |