topenarray.nim 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. proc fn1[T](a: openArray[T]): seq[T] =
  5. for ai in a: result.add ai
  6. proc fn2[T](a: var openArray[T]): seq[T] =
  7. for ai in a: result.add ai
  8. proc fn3[T](a: var openArray[T]) =
  9. for i, ai in mpairs(a): ai = i * 10
  10. proc main =
  11. var a = [1,2,3,4,5]
  12. doAssert fn1(a.toOpenArray(1,3)) == @[2,3,4]
  13. doAssert fn2(toOpenArray(a, 1, 3)) == @[2,3,4]
  14. doAssert fn2(a.toOpenArray(1,3)) == @[2,3,4]
  15. fn3(a.toOpenArray(1,3))
  16. when defined(js): discard # xxx bug #15952: `a` left unchanged
  17. else: doAssert a == [1, 0, 10, 20, 5]
  18. block: # bug #12521
  19. block:
  20. type slice[T] = openArray[T]
  21. # Proc using that alias
  22. proc testing(sl: slice[int]): seq[int] =
  23. for item in sl:
  24. result.add item
  25. let mySeq = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
  26. doAssert testing(mySeq) == mySeq
  27. doAssert testing(mySeq[2..^2]) == mySeq[2..^2]
  28. block:
  29. type slice = openArray[int]
  30. # Proc using that alias
  31. proc testing(sl: slice): seq[int] =
  32. for item in sl:
  33. result.add item
  34. let mySeq = @[1, 2, 3, 4, 5, 6, 7, 8, 9]
  35. doAssert testing(mySeq) == mySeq
  36. doAssert testing(mySeq[2..^2]) == mySeq[2..^2]
  37. block: # bug #23321
  38. block:
  39. proc foo(x: openArray[int]) =
  40. doAssert x[0] == 0
  41. var d = new array[1, int]
  42. foo d[].toOpenArray(0, 0)
  43. block:
  44. proc foo(x: openArray[int]) =
  45. doAssert x[0] == 0
  46. proc task(x: var array[1, int]): var array[1, int] =
  47. result = x
  48. var d: array[1, int]
  49. foo task(d).toOpenArray(0, 0)
  50. block:
  51. proc foo(x: openArray[int]) =
  52. doAssert x[0] == 0
  53. proc task(x: var array[1, int]): lent array[1, int] =
  54. result = x
  55. var d: array[1, int]
  56. foo task(d).toOpenArray(0, 0)
  57. block:
  58. proc foo(x: openArray[int]) =
  59. doAssert x[0] == 0
  60. proc task(x: var array[1, int]): ptr array[1, int] =
  61. result = addr x
  62. var d: array[1, int]
  63. foo task(d)[].toOpenArray(0, 0)
  64. main()
  65. static: main()