tnested.nim 2.9 KB

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