tnested.nim 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. discard """
  2. targets: "c js"
  3. output: '''
  4. foo88
  5. 23 24foo 88
  6. foo88
  7. 23 24foo 88
  8. 11
  9. int: 108
  10. 0
  11. 11
  12. 1
  13. 11
  14. 2
  15. 11
  16. 3
  17. 11
  18. 4
  19. 11
  20. 5
  21. 11
  22. 6
  23. 11
  24. 7
  25. 11
  26. 8
  27. 11
  28. 9
  29. 11
  30. 11
  31. py
  32. py
  33. py
  34. py
  35. px
  36. 6
  37. proc (){.closure, noSideEffect, gcsafe.}
  38. '''
  39. """
  40. block tnestedclosure:
  41. proc main(param: int) =
  42. var foo = 23
  43. proc outer(outerParam: string) =
  44. var outerVar = 88
  45. echo outerParam, outerVar
  46. proc inner() =
  47. block Test:
  48. echo foo, " ", param, outerParam, " ", outerVar
  49. inner()
  50. outer("foo")
  51. # test simple closure within dummy 'main':
  52. proc dummy =
  53. proc main2(param: int) =
  54. var fooB = 23
  55. proc outer(outerParam: string) =
  56. var outerVar = 88
  57. echo outerParam, outerVar
  58. proc inner() =
  59. block Test:
  60. echo fooB, " ", param, outerParam, " ", outerVar
  61. inner()
  62. outer("foo")
  63. main2(24)
  64. dummy()
  65. main(24)
  66. # Jester + async triggered this bug:
  67. proc cbOuter() =
  68. var response = "hohoho"
  69. block:
  70. proc cbIter() =
  71. block:
  72. proc fooIter() =
  73. doAssert response == "hohoho"
  74. fooIter()
  75. cbIter()
  76. cbOuter()
  77. block tnestedproc:
  78. proc p(x, y: int): int =
  79. result = x + y
  80. echo p((proc (): int =
  81. var x = 7
  82. return x)(),
  83. (proc (): int = return 4)())
  84. block deeplynested:
  85. # bug #4070
  86. proc id(f: (proc())): auto =
  87. return f
  88. proc foo(myinteger: int): (iterator(): int) =
  89. return iterator(): int {.closure.} =
  90. proc bar() =
  91. proc kk() =
  92. echo "int: ", myinteger
  93. kk()
  94. id(bar)()
  95. discard foo(108)()
  96. block tclosure2:
  97. when true:
  98. proc ax =
  99. for xxxx in 0..9:
  100. var i = 0
  101. proc bx =
  102. if i > 10:
  103. echo xxxx
  104. return
  105. i += 1
  106. #for j in 0 .. 0: echo i
  107. bx()
  108. bx()
  109. echo i
  110. ax()
  111. when true:
  112. proc accumulator(start: int): (proc(): int {.closure.}) =
  113. var x = start-1
  114. #let dummy = proc =
  115. # discard start
  116. result = proc (): int =
  117. #var x = 9
  118. for i in 0 .. 0: x = x + 1
  119. return x
  120. var a = accumulator(3)
  121. let b = accumulator(4)
  122. echo a() + b() + a()
  123. proc outer =
  124. proc py() =
  125. # no closure here:
  126. for i in 0..3: echo "py"
  127. py()
  128. outer()
  129. when true:
  130. proc outer2 =
  131. var errorValue = 3
  132. proc fac[T](n: T): T =
  133. if n < 0: result = errorValue
  134. elif n <= 1: result = 1
  135. else: result = n * fac(n-1)
  136. proc px() {.closure.} =
  137. echo "px"
  138. proc py() {.closure.} =
  139. echo "py"
  140. let
  141. mapping = {
  142. "abc": px,
  143. "xyz": py
  144. }
  145. mapping[0][1]()
  146. echo fac(3)
  147. outer2()
  148. # bug #5688
  149. import typetraits
  150. block:
  151. proc myDiscard[T](a: T) = discard
  152. proc foo() =
  153. let a = 5
  154. let f = (proc() =
  155. myDiscard (proc() = echo a)
  156. )
  157. echo name(typeof(f))
  158. foo()
  159. block:
  160. iterator foo: int {.closure.} =
  161. yield 1
  162. yield 2
  163. yield 3
  164. proc pork =
  165. let call = foo
  166. for i in call():
  167. discard i
  168. let call2 = foo
  169. while not finished(call2):
  170. discard call2()
  171. pork()