titer_issues.nim 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. discard """
  2. target: "c js"
  3. output: '''
  4. 0
  5. 1
  6. 2
  7. 3
  8. 4
  9. 1
  10. start
  11. false
  12. 0
  13. 1
  14. 2
  15. end
  16. @[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42]
  17. 1002
  18. 0
  19. 1
  20. 2
  21. 7
  22. 9002
  23. 9004
  24. 9006
  25. 9008
  26. 9010
  27. 9012
  28. 9014
  29. 9016
  30. 9018
  31. @[1, 2]
  32. @[1, 2, 3]
  33. 1
  34. nested finally
  35. outer finally
  36. nested finally
  37. outer finally
  38. nested finally
  39. outer finally
  40. nested finally
  41. outer finally
  42. In defer
  43. trying
  44. exception caught
  45. finally block
  46. '''
  47. """
  48. import sequtils, strutils
  49. block t338:
  50. proc moo(): iterator (): int =
  51. iterator fooGen: int {.closure.} =
  52. while true:
  53. yield result
  54. result.inc
  55. return fooGen
  56. var foo = moo()
  57. for i in 0 .. 4:
  58. echo foo()
  59. block t8041:
  60. iterator xy[T](a: T, b: set[T]): T =
  61. if a in b:
  62. yield a
  63. for a in xy(1'i8, {}):
  64. for b in xy(a, {}):
  65. echo a
  66. block t3837_chained:
  67. iterator t1(): int {.closure.} =
  68. yield 1
  69. iterator t2(): int {.closure.} =
  70. for i in t1():
  71. yield i
  72. for i in t2():
  73. echo $i
  74. proc iter1(): (iterator: int) =
  75. let coll = [0,1,2]
  76. result = iterator: int {.closure.} =
  77. for i in coll:
  78. yield i
  79. proc iter2(it: (iterator: int)): (iterator: int) =
  80. result = iterator: int {.closure.} =
  81. echo finished(it)
  82. for i in it():
  83. yield i
  84. echo "start"
  85. let myiter1 = iter1()
  86. let myiter2 = iter2(myiter1)
  87. for i in myiter2():
  88. echo i
  89. echo "end"
  90. type Iterable[T] = (iterator: T) | Slice[T]
  91. ## Everything that can be iterated over, iterators and slices so far.
  92. proc toIter[T](s: Slice[T]): iterator: T =
  93. ## Iterate over a slice.
  94. iterator it: T {.closure.} =
  95. for x in s.a..s.b:
  96. yield x
  97. return it
  98. proc toIter[T](i: iterator: T): iterator: T =
  99. ## Nop
  100. i
  101. iterator map[T,S](i: Iterable[T], f: proc(x: T): S): S =
  102. let i = toIter(i)
  103. for x in i():
  104. yield f(x)
  105. proc filter[T](i: Iterable[T], f: proc(x: T): bool): iterator: T =
  106. let i = toIter(i)
  107. iterator it: T {.closure.} =
  108. for x in i():
  109. if f(x):
  110. yield x
  111. result = it
  112. iterator filter[T](i: Iterable[T], f: proc(x: T): bool): T =
  113. let i = toIter(i)
  114. for x in i():
  115. if f(x):
  116. yield x
  117. var it = toSeq(filter(2..10, proc(x: int): bool = x mod 2 == 0))
  118. doAssert it == @[2, 4, 6, 8, 10]
  119. it = toSeq(map(filter(2..10, proc(x: int): bool = x mod 2 == 0), proc(x: int): int = x * 2))
  120. doAssert it == @[4, 8, 12, 16, 20]
  121. block t3221_complex:
  122. iterator permutations[T](ys: openArray[T]): seq[T] =
  123. var
  124. d = 1
  125. c = newSeq[int](ys.len)
  126. xs = newSeq[T](ys.len)
  127. for i, y in ys: xs[i] = y
  128. yield xs
  129. block outer:
  130. while true:
  131. while d > 1:
  132. dec d
  133. c[d] = 0
  134. while c[d] >= d:
  135. inc d
  136. if d >= ys.len: break outer
  137. let i = if (d and 1) == 1: c[d] else: 0
  138. swap xs[i], xs[d]
  139. yield xs
  140. inc c[d]
  141. proc dig_vectors(): void =
  142. var v_nums: seq[int]
  143. v_nums = newSeq[int](1)
  144. for perm in permutations(toSeq(0 .. 1)):
  145. v_nums[0] = 1
  146. dig_vectors()
  147. block t3499_keepstate:
  148. proc slice[T](iter: iterator(): T {.closure.}, sl: auto): seq[T] =
  149. var res: seq[int64] = @[]
  150. var i = 0
  151. for n in iter():
  152. if i > sl.b:
  153. break
  154. if i >= sl.a:
  155. res.add(n)
  156. inc i
  157. res
  158. iterator harshad(): int64 {.closure.} =
  159. for n in 1 ..< int64.high:
  160. var sum = 0
  161. for ch in string($n):
  162. sum += parseInt("" & ch)
  163. if n mod sum == 0:
  164. yield n
  165. echo harshad.slice 0 ..< 20
  166. for n in harshad():
  167. if n > 1000:
  168. echo n
  169. break
  170. # bug #3499 last snippet fixed
  171. # bug #705 last snippet fixed
  172. block t1725_nested:
  173. iterator factory(): int {.closure.} =
  174. iterator bar(): int {.closure.} =
  175. yield 0
  176. yield 1
  177. yield 2
  178. for x in bar(): yield x
  179. for x in factory():
  180. echo x
  181. block t2023_objiter:
  182. type
  183. Obj = object
  184. iter: iterator (): int8 {.closure.}
  185. iterator test(): int8 {.closure.} =
  186. yield 7
  187. proc init():Obj=
  188. result.iter = test
  189. var o = init()
  190. echo(o.iter())
  191. block:
  192. # bug #13739
  193. iterator myIter(arg: openArray[int]): int =
  194. var tmp = 0
  195. let len = arg.len
  196. while tmp < len:
  197. yield arg[tmp] * 2
  198. inc tmp
  199. proc someProc() =
  200. var data = [4501,4502,4503,4504,4505,4506,4507,4508,4509]
  201. # StmtListExpr should not get special treatment.
  202. for x in myIter((discard;data)):
  203. echo x
  204. someProc()
  205. block:
  206. # bug #12576
  207. iterator ff(sq: varargs[seq[int]]): int =
  208. for x in sq:
  209. echo x
  210. for x in ff(@[1, 2], @[1, 2, 3]):
  211. echo x
  212. # bug #19575
  213. iterator bb() {.closure.} =
  214. while true:
  215. try: discard
  216. except: break
  217. finally: break
  218. var a = bb
  219. iterator cc() {.closure.} =
  220. while true:
  221. try: discard
  222. except:
  223. if true:
  224. break
  225. finally:
  226. if true:
  227. break
  228. var a2 = cc
  229. # bug #16876
  230. block:
  231. iterator a(num: int): int {.closure.} =
  232. if num == 1:
  233. yield num
  234. else:
  235. for i in a(num - 1):
  236. yield i
  237. for i in a(5):
  238. echo i
  239. block:
  240. # bug #19911 (return in nested try)
  241. # try yield -> try
  242. iterator p1: int {.closure.} =
  243. try:
  244. yield 0
  245. try:
  246. return
  247. finally:
  248. echo "nested finally"
  249. echo "shouldn't run"
  250. finally:
  251. echo "outer finally"
  252. echo "shouldn't run"
  253. for _ in p1():
  254. discard
  255. # try -> try yield
  256. iterator p2: int {.closure.} =
  257. try:
  258. try:
  259. yield 0
  260. return
  261. finally:
  262. echo "nested finally"
  263. echo "shouldn't run"
  264. finally:
  265. echo "outer finally"
  266. echo "shouldn't run"
  267. for _ in p2():
  268. discard
  269. # try yield -> try yield
  270. iterator p3: int {.closure.} =
  271. try:
  272. yield 0
  273. try:
  274. yield 0
  275. return
  276. finally:
  277. echo "nested finally"
  278. echo "shouldn't run"
  279. finally:
  280. echo "outer finally"
  281. echo "shouldn't run"
  282. for _ in p3():
  283. discard
  284. # try -> try
  285. iterator p4: int {.closure.} =
  286. try:
  287. try:
  288. return
  289. finally:
  290. echo "nested finally"
  291. echo "shouldn't run"
  292. finally:
  293. echo "outer finally"
  294. echo "shouldn't run"
  295. for _ in p4():
  296. discard
  297. # bug #18824
  298. iterator poc_iterator: int {.closure.} =
  299. block bug18824:
  300. try:
  301. break bug18824
  302. finally:
  303. echo "In defer"
  304. for _ in poc_iterator():
  305. discard
  306. # bug #20624
  307. iterator tryFinally() {.closure.} =
  308. block route:
  309. try:
  310. echo "trying"
  311. raise
  312. except:
  313. echo "exception caught"
  314. break route
  315. finally:
  316. echo "finally block"
  317. var x = tryFinally
  318. x()
  319. block: # bug #24033
  320. type Query = ref object
  321. iterator pairs(query: Query): (int, (string, float32)) =
  322. var output: (int, (string, float32)) = (0, ("foo", 3.14))
  323. for id in @[0, 1, 2]:
  324. output[0] = id
  325. yield output
  326. var collections: seq[(int, string, string)]
  327. for id, (str, num) in Query():
  328. collections.add (id, str, $num)
  329. doAssert collections[1] == (1, "foo", "3.14")