ttypetraits.nim 1.3 KB

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