tmacros_issues.nim 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. discard """
  2. nimout: '''
  3. IntLit 5
  4. proc (x: int): string => typeDesc[proc[string, int]]
  5. proc (x: int): void => typeDesc[proc[void, int]]
  6. proc (x: int) => typeDesc[proc[void, int]]
  7. x => UncheckedArray[int]
  8. a
  9. s
  10. d
  11. f
  12. TTaa
  13. TTaa
  14. TTaa
  15. TTaa
  16. true
  17. true
  18. nil
  19. 42
  20. false
  21. true
  22. '''
  23. output: '''
  24. range[0 .. 100]
  25. array[0 .. 100, int]
  26. 10
  27. test
  28. '''
  29. """
  30. import macros, parseutils
  31. block t7723:
  32. macro foo1(): untyped =
  33. result = newStmtList()
  34. result.add quote do:
  35. proc init(foo: int, bar: typedesc[int]): int =
  36. foo
  37. #expandMacros:
  38. foo1()
  39. doAssert init(1, int) == 1
  40. block t8706:
  41. macro varargsLen(args:varargs[untyped]): untyped =
  42. doAssert args.kind == nnkArglist
  43. doAssert args.len == 0
  44. result = newLit(args.len)
  45. template bar(a0:varargs[untyped]): untyped =
  46. varargsLen(a0)
  47. template foo(x: int, a0:varargs[untyped]): untyped =
  48. bar(a0)
  49. doAssert foo(42) == 0
  50. doAssert bar() == 0
  51. block t9194:
  52. type
  53. Foo1 = range[0 .. 100]
  54. Foo2 = array[0 .. 100, int]
  55. macro get(T: typedesc): untyped =
  56. # Get the X out of typedesc[X]
  57. let tmp = getTypeImpl(T)
  58. result = newStrLitNode(getTypeImpl(tmp[1]).repr)
  59. echo Foo1.get
  60. echo Foo2.get
  61. block t1944:
  62. template t(e: untyped): untyped =
  63. macro m(eNode: untyped): untyped =
  64. echo eNode.treeRepr
  65. m e
  66. t 5
  67. block t926:
  68. proc test(f: var NimNode) {.compileTime.} =
  69. f = newNimNode(nnkStmtList)
  70. f.add newCall(newIdentNode("echo"), newLit(10))
  71. macro blah(prc: untyped): untyped =
  72. result = prc
  73. test(result)
  74. proc test() {.blah.} =
  75. echo 5
  76. block t2211:
  77. macro showType(t:typed): untyped =
  78. let ty = t.getType
  79. echo t.repr, " => ", ty.repr
  80. showType(proc(x:int): string)
  81. showType(proc(x:int): void)
  82. showType(proc(x:int))
  83. var x: UncheckedArray[int]
  84. showType(x)
  85. block t1140:
  86. proc parse_until_symbol(node: NimNode, value: string, index: var int): bool {.compiletime.} =
  87. var splitValue: string
  88. var read = value.parseUntil(splitValue, '$', index)
  89. # when false:
  90. if false:
  91. var identifier: string
  92. read = value.parseWhile(identifier, {}, index)
  93. node.add newCall("add", ident("result"), newCall("$", ident(identifier)))
  94. if splitValue.len > 0:
  95. node.insert node.len, newCall("add", ident("result"), newStrLitNode(splitValue))
  96. proc parse_template(node: NimNode, value: string) {.compiletime.} =
  97. var index = 0
  98. while index < value.len and
  99. parse_until_symbol(node, value, index): discard
  100. macro tmpli(body: untyped): typed =
  101. result = newStmtList()
  102. result.add parseExpr("result = \"\"")
  103. result.parse_template body[1].strVal
  104. proc actual: string {.used.} = tmpli html"""
  105. <p>Test!</p>
  106. """
  107. proc another: string {.used.} = tmpli html"""
  108. <p>what</p>
  109. """
  110. block tbugs:
  111. type
  112. Foo = object
  113. s: char
  114. iterator test2(f: string): Foo =
  115. for i in f:
  116. yield Foo(s: i)
  117. macro test(): untyped =
  118. for i in test2("asdf"):
  119. echo i.s
  120. test()
  121. # bug 1297
  122. type TType = tuple[s: string]
  123. macro echotest(): untyped =
  124. var t: TType
  125. t.s = ""
  126. t.s.add("test")
  127. result = newCall(newIdentNode("echo"), newStrLitNode(t.s))
  128. echotest()
  129. # bug #1103
  130. type
  131. Td = tuple
  132. a:string
  133. b:int
  134. proc get_data(d: Td) : string {.compileTime.} =
  135. result = d.a # Works if a literal string is used here.
  136. # Bugs if line A or B is active. Works with C
  137. result &= "aa" # A
  138. #result.add("aa") # B
  139. #result = result & "aa" # C
  140. macro m(s:static[Td]) : untyped =
  141. echo get_data(s)
  142. echo get_data(s)
  143. result = newEmptyNode()
  144. const s = ("TT", 3)
  145. m(s)
  146. m(s)
  147. # bug #933
  148. proc nilcheck(): NimNode {.compileTime.} =
  149. echo(result == nil) # true
  150. echo(result.isNil) # true
  151. echo(repr(result)) # nil
  152. macro testnilcheck(): untyped =
  153. result = newNimNode(nnkStmtList)
  154. discard nilcheck()
  155. testnilcheck()
  156. # bug #1323
  157. proc calc(): array[1, int] =
  158. result[0].inc()
  159. result[0].inc()
  160. const c = calc()
  161. doAssert c[0] == 2
  162. # bug #3046
  163. macro sampleMacroInt(i: int): untyped =
  164. echo i.intVal
  165. macro sampleMacroBool(b: bool): untyped =
  166. echo b.boolVal
  167. sampleMacroInt(42)
  168. sampleMacroBool(false)
  169. sampleMacroBool(system.true)