tparams_gensymed.nim 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. discard """
  2. output: '''
  3. 0
  4. 1
  5. 2
  6. 3
  7. 0
  8. 1
  9. 2
  10. 3
  11. '''
  12. """
  13. # bug #1915
  14. import macros
  15. # Test that parameters are properly gensym'ed finally:
  16. template genNodeKind(kind, name: untyped) =
  17. proc name*(children: varargs[NimNode]): NimNode {.compiletime.}=
  18. result = newNimNode(kind)
  19. for c in children:
  20. result.add(c)
  21. genNodeKind(nnkNone, None)
  22. # Test that generics in templates still work (regression to fix #1915)
  23. # bug #2004
  24. type Something = object
  25. proc testA(x: Something) = discard
  26. template def(name: untyped) =
  27. proc testB[T](reallyUniqueName: T) =
  28. `test name`(reallyUniqueName)
  29. def A
  30. var x: Something
  31. testB(x)
  32. # bug #2215
  33. # Test that templates in generics still work (regression to fix the
  34. # regression...)
  35. template forStatic(index, slice, predicate: untyped) =
  36. const a = slice.a
  37. const b = slice.b
  38. when a <= b:
  39. template iteration(i: int) =
  40. block:
  41. const index = i
  42. predicate
  43. template iterateStartingFrom(i: int) =
  44. when i <= b:
  45. iteration i
  46. iterateStartingFrom i + 1
  47. iterateStartingFrom a
  48. proc concreteProc(x: int) =
  49. forStatic i, 0..3:
  50. echo i
  51. proc genericProc(x: any) =
  52. forStatic i, 0..3:
  53. echo i
  54. concreteProc(7) # This works
  55. genericProc(7) # This doesn't compile
  56. import tables
  57. # bug #9476
  58. proc getTypeInfo*(T: typedesc): pointer =
  59. var dummy: T
  60. getTypeInfo(dummy)
  61. macro implementUnary(op: untyped): untyped =
  62. result = newStmtList()
  63. template defineTable(tableSymbol) =
  64. var tableSymbol = initTable[pointer, pointer]()
  65. let tableSymbol = genSym(nskVar, "registeredProcs")
  66. result.add(getAst(defineTable(tableSymbol)))
  67. template defineRegisterInstantiation(tableSym, regTemplSym, instSym, op) =
  68. template regTemplSym*(T: typedesc) =
  69. let ti = getTypeInfo(T)
  70. proc instSym(xOrig: int): int {.gensym, cdecl.} =
  71. let x {.inject.} = xOrig
  72. op
  73. tableSym[ti] = cast[pointer](instSym)
  74. let regTemplSymbol = ident("registerInstantiation")
  75. let instSymbol = ident("instantiation")
  76. result.add(getAst(defineRegisterInstantiation(
  77. tableSymbol, regTemplSymbol, instSymbol, op
  78. )))
  79. echo result.repr
  80. implementUnary(): x*x
  81. registerInstantiation(int)
  82. registerInstantiation(float)
  83. # bug #10192
  84. template nest(body) {.dirty.} =
  85. template p1(b1: untyped) {.dirty, used.} =
  86. template implp1: untyped {.dirty.} = b1
  87. template p2(b2: untyped) {.dirty, used.} =
  88. template implp2: untyped {.dirty.} = b2
  89. body
  90. implp1
  91. implp2
  92. template test() =
  93. nest:
  94. p1:
  95. var foo = "bar"
  96. p2:
  97. doAssert(foo.len == 3)
  98. test()