template_issues.nim 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. discard """
  2. output: '''
  3. @[]
  4. 5
  5. 0
  6. a
  7. hi
  8. Hello, World!
  9. (e: 42)
  10. hey
  11. foo
  12. foo
  13. foo
  14. false
  15. true
  16. '''
  17. """
  18. import macros, json
  19. block t2057:
  20. proc mpf_get_d(x: int): float = float(x)
  21. proc mpf_cmp_d(a: int; b: float): int = 0
  22. template toFloatHelper(result, tooSmall, tooLarge: untyped) =
  23. result = mpf_get_d(a)
  24. if result == 0.0 and mpf_cmp_d(a,0.0) != 0:
  25. tooSmall
  26. if result == Inf:
  27. tooLarge
  28. proc toFloat(a: int): float =
  29. toFloatHelper(result) do:
  30. raise newException(ValueError, "number too small")
  31. do:
  32. raise newException(ValueError, "number too large")
  33. doAssert toFloat(8) == 8.0
  34. import sequtils, os
  35. block t2629:
  36. template glob_rst(basedir: string = ""): untyped =
  37. if baseDir.len == 0:
  38. to_seq(walk_files("*.rst"))
  39. else:
  40. to_seq(walk_files(basedir/"*.rst"))
  41. let rst_files = concat(glob_rst(), glob_rst("docs"))
  42. when true: echo rst_files
  43. block t5417:
  44. macro genBody: untyped =
  45. let sbx = genSym(nskLabel, "test")
  46. when true:
  47. result = quote do:
  48. block `sbx`:
  49. break `sbx`
  50. else:
  51. template foo(s1, s2) =
  52. block s1:
  53. break s2
  54. result = getAst foo(sbx, sbx)
  55. proc test() =
  56. genBody()
  57. block t909:
  58. template baz() =
  59. proc bar() =
  60. var x = 5
  61. iterator foo(): int {.closure.} =
  62. echo x
  63. var y = foo
  64. discard y()
  65. macro test(): untyped =
  66. result = getAst(baz())
  67. test()
  68. bar()
  69. block t993:
  70. type PNode = ref object of RootObj
  71. template litNode(name, ty) =
  72. type name = ref object of PNode
  73. val: ty
  74. litNode PIntNode, int
  75. template withKey(j: JsonNode; key: string; varname,
  76. body: untyped): typed =
  77. if j.hasKey(key):
  78. let varname{.inject.}= j[key]
  79. block:
  80. body
  81. var j = parsejson("{\"zzz\":1}")
  82. withkey(j, "foo", x):
  83. echo(x)
  84. block t1337:
  85. template someIt(a, pred): untyped =
  86. var it {.inject.} = 0
  87. pred
  88. proc aProc(n: auto) =
  89. n.someIt(echo(it))
  90. aProc(89)
  91. import mlt
  92. block t4564:
  93. type Bar = ref object of RootObj
  94. proc foo(a: Bar): int = 0
  95. var a: Bar
  96. let b = a.foo() > 0
  97. block t8052:
  98. type
  99. UintImpl[N: static[int], T: SomeUnsignedInt] = object
  100. raw_data: array[N, T]
  101. template genLoHi(TypeImpl: untyped): untyped =
  102. template loImpl[N: static[int], T: SomeUnsignedInt](dst: TypeImpl[N div 2, T], src: TypeImpl[N, T]) =
  103. let halfSize = N div 2
  104. for i in 0 ..< halfSize:
  105. dst.raw_data[i] = src.raw_data[i]
  106. proc lo[N: static[int], T: SomeUnsignedInt](x: TypeImpl[N,T]): TypeImpl[N div 2, T] {.inline.}=
  107. loImpl(result, x)
  108. genLoHi(UintImpl)
  109. var a: UintImpl[4, uint32]
  110. a.raw_data = [1'u32, 2'u32, 3'u32, 4'u32]
  111. doAssert a.lo.raw_data.len == 2
  112. doAssert a.lo.raw_data[0] == 1
  113. doAssert a.lo.raw_data[1] == 2
  114. block t2585:
  115. type
  116. RenderPass = object
  117. state: ref int
  118. RenderData = object
  119. fb: int
  120. walls: seq[RenderPass]
  121. Mat2 = int
  122. Vector2[T] = T
  123. Pixels=int
  124. template use(fb: int, st: untyped): untyped =
  125. echo "a ", $fb
  126. st
  127. echo "a ", $fb
  128. proc render(rdat: var RenderData; passes: var openArray[RenderPass]; proj: Mat2;
  129. indexType = 1) =
  130. for i in 0 ..< len(passes):
  131. echo "blah ", repr(passes[i])
  132. proc render2(rdat: var RenderData; screenSz: Vector2[Pixels]; proj: Mat2) =
  133. use rdat.fb:
  134. render(rdat, rdat.walls, proj, 1)
  135. block t4292:
  136. template foo(s: string): string = s
  137. proc variadicProc(v: varargs[string, foo]) = echo v[0]
  138. variadicProc("a")
  139. block t2670:
  140. template testTemplate(b: bool): typed =
  141. when b:
  142. var a = "hi"
  143. else:
  144. var a = 5
  145. echo a
  146. testTemplate(true)
  147. block t4097:
  148. var i {.compileTime.} = 2
  149. template defineId(t: typedesc) =
  150. const id {.genSym.} = i
  151. static: inc(i)
  152. proc idFor(T: typedesc[t]): int {.inline, raises: [].} = id
  153. defineId(int8)
  154. defineId(int16)
  155. doAssert idFor(int8) == 2
  156. doAssert idFor(int16) == 3
  157. block t5235:
  158. template outer(body: untyped) =
  159. template test(val: string) =
  160. const SomeConst: string = val
  161. echo SomeConst
  162. body
  163. outer:
  164. test("Hello, World!")
  165. # bug #11941
  166. type X = object
  167. e: int
  168. proc works(T: type X, v: auto): T = T(e: v)
  169. template fails(T: type X, v: auto): T = T(e: v)
  170. var
  171. w = X.works(42)
  172. x = X.fails(42)
  173. echo x
  174. import mtempl5
  175. proc foo(): auto =
  176. trap "foo":
  177. echo "hey"
  178. discard foo()
  179. # bug #4722
  180. type
  181. IteratorF*[In] = iterator() : In {.closure.}
  182. template foof(In: untyped) : untyped =
  183. proc ggg*(arg: IteratorF[In]) =
  184. for i in arg():
  185. echo "foo"
  186. iterator hello() : int {.closure.} =
  187. for i in 1 .. 3:
  188. yield i
  189. foof(int)
  190. ggg(hello)
  191. # bug #2586
  192. var z = 10'u8
  193. echo z < 9 # Works
  194. echo z > 9 # Error: type mismatch
  195. # bug #5993
  196. template foo(p: proc) =
  197. var bla = 5
  198. p(bla)
  199. foo() do(t: var int):
  200. discard
  201. t = 5
  202. proc bar(t: var int) =
  203. t = 5
  204. foo(bar)
  205. block: # bug #12595
  206. template test() =
  207. let i = 42
  208. discard {i: ""}
  209. test()