template_issues.nim 5.2 KB

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