ttypetraits.nim 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. discard """
  2. matrix: "--mm:refc; --mm:orc"
  3. targets: "c cpp js"
  4. """
  5. # xxx merge with tests/metatype/ttypetraits.nim
  6. import std/typetraits
  7. import std/assertions
  8. macro testClosure(fn: typed, flag: static bool) =
  9. if flag:
  10. doAssert hasClosure(fn)
  11. else:
  12. doAssert not hasClosure(fn)
  13. block:
  14. proc h1() =
  15. echo 1
  16. testClosure(h1, false)
  17. proc h2() {.nimcall.} =
  18. echo 2
  19. testClosure(h2, false)
  20. block:
  21. proc fn(): auto =
  22. proc hello() {.nimcall.} =
  23. echo 3
  24. hello
  25. let name = fn()
  26. testClosure(name, false)
  27. block:
  28. proc fn(): auto =
  29. proc hello() =
  30. echo 3
  31. hello
  32. let name = fn()
  33. testClosure(name, false)
  34. block:
  35. proc fn(): auto =
  36. var x = 0
  37. proc hello() =
  38. echo 3
  39. inc x
  40. hello
  41. let name = fn()
  42. testClosure(name, true)
  43. block:
  44. proc fn(): auto =
  45. var x = 0
  46. proc hello() {.closure.} =
  47. echo 3
  48. inc x
  49. hello
  50. let name = fn()
  51. testClosure(name, true)
  52. block:
  53. proc fn(): auto =
  54. var x = 0
  55. proc hello() {.closure.} =
  56. echo 3
  57. inc x
  58. hello
  59. let name = fn()
  60. testClosure(name, true)
  61. let name2 = name
  62. testClosure(name2, true)
  63. block:
  64. iterator hello(): int =
  65. yield 1
  66. testClosure(hello, false)
  67. when not defined(js):
  68. block:
  69. iterator hello(): int {.closure.}=
  70. yield 1
  71. testClosure(hello, true)