ttuples_issues.nim 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. # targets include `cpp` because in the past, there were several cpp-specific bugs with tuples.
  5. import std/tables
  6. template main() =
  7. block: # bug #4479
  8. type
  9. MyTuple = tuple
  10. num: int
  11. strings: seq[string]
  12. ints: seq[int]
  13. var foo = MyTuple((
  14. num: 7,
  15. strings: @[],
  16. ints: @[],
  17. ))
  18. var bar = MyTuple (
  19. num: 7,
  20. strings: @[],
  21. ints: @[],
  22. )
  23. var fooUnnamed = MyTuple((7, @[], @[]))
  24. var n = 7
  25. var fooSym = MyTuple((num: n, strings: @[], ints: @[]))
  26. block: # bug #1910
  27. var p = newOrderedTable[tuple[a:int], int]()
  28. var q = newOrderedTable[tuple[x:int], int]()
  29. for key in p.keys:
  30. echo key.a
  31. for key in q.keys:
  32. echo key.x
  33. block: # bug #2121
  34. type
  35. Item[K,V] = tuple
  36. key: K
  37. value: V
  38. var q = newseq[Item[int,int]](1)
  39. let (x,y) = q[0]
  40. block: # bug #2369
  41. type HashedElem[T] = tuple[num: int, storedVal: ref T]
  42. proc append[T](tab: var seq[HashedElem[T]], n: int, val: ref T) =
  43. #tab.add((num: n, storedVal: val))
  44. var he: HashedElem[T] = (num: n, storedVal: val)
  45. #tab.add(he)
  46. var g: seq[HashedElem[int]] = @[]
  47. proc foo() =
  48. var x: ref int
  49. new(x)
  50. x[] = 77
  51. g.append(44, x)
  52. block: # bug #1986
  53. proc test(): int64 =
  54. return 0xdeadbeef.int64
  55. const items = [
  56. (var1: test(), var2: 100'u32),
  57. (var1: test(), var2: 192'u32)
  58. ]
  59. block: # bug #14911
  60. doAssert $(a: 1) == "(a: 1)" # works
  61. doAssert $(`a`: 1) == "(a: 1)" # works
  62. doAssert $(`a`: 1, `b`: 2) == "(a: 1, b: 2)" # was: Error: named expression expected
  63. block: # bug #16822
  64. var scores: seq[(set[char], int)] = @{{'/'} : 10}
  65. var x1: set[char]
  66. for item in items(scores):
  67. x1 = item[0]
  68. doAssert x1 == {'/'}
  69. var x2: set[char]
  70. for (chars, value) in items(scores):
  71. x2 = chars
  72. doAssert x2 == {'/'}
  73. block: # bug #14574
  74. proc fn(): auto =
  75. let a = @[("foo", (12, 13))]
  76. for (k,v) in a:
  77. return (k,v)
  78. doAssert fn() == ("foo", (12, 13))
  79. block: # bug #14574
  80. iterator fn[T](a:T): lent T = yield a
  81. let a = (10, (11,))
  82. proc bar(): auto =
  83. for (x,y) in fn(a):
  84. return (x,y)
  85. doAssert bar() == (10, (11,))
  86. block: # bug #16331
  87. type T1 = tuple[a, b: int]
  88. proc p(b: bool): T1 =
  89. var x: T1 = (10, 20)
  90. x = if b: (x.b, x.a) else: (-x.b, -x.a)
  91. x
  92. doAssert p(false) == (-20, -10)
  93. doAssert p(true) == (20, 10)
  94. proc mainProc() =
  95. # other tests should be in `main`
  96. block:
  97. type A = tuple[x: int, y: int]
  98. doAssert (x: 1, y: 2).A == A (x: 1, y: 2) # MCS => can't use a template
  99. static:
  100. main()
  101. mainProc()
  102. main()
  103. mainProc()