summary refs log tree commit diff stats
path: root/tests/template/template_issues.nim
blob: e56d444802452613626b7db5d9597595080455f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
discard """
output: '''
@[]
5
0
a
hi
Hello, World!
(e: 42)
hey
'''
"""


import macros, json


block t2057:
  proc mpf_get_d(x: int): float = float(x)
  proc mpf_cmp_d(a: int; b: float): int = 0

  template toFloatHelper(result, tooSmall, tooLarge: untyped) =
    result = mpf_get_d(a)
    if result == 0.0 and mpf_cmp_d(a,0.0) != 0:
      tooSmall
    if result == Inf:
      tooLarge

  proc toFloat(a: int): float =
    toFloatHelper(result) do:
      raise newException(ValueError, "number too small")
    do:
      raise newException(ValueError, "number too large")

  doAssert toFloat(8) == 8.0



import sequtils, os
block t2629:
  template glob_rst(basedir: string = ""): untyped =
    if baseDir.len == 0:
      to_seq(walk_files("*.rst"))
    else:
      to_seq(walk_files(basedir/"*.rst"))

  let rst_files = concat(glob_rst(), glob_rst("docs"))

  when true: echo rst_files


block t5417:
  macro genBody: untyped =
    let sbx = genSym(nskLabel, "test")
    when true:
      result = quote do:
        block `sbx`:
          break `sbx`
    else:
      template foo(s1, s2) =
        block s1:
          break s2
      result = getAst foo(sbx, sbx)

  proc test() =
    genBody()



block t909:
  template baz() =
    proc bar() =
      var x = 5
      iterator foo(): int {.closure.} =
        echo x
      var y = foo
      discard y()

  macro test(): untyped =
    result = getAst(baz())

  test()
  bar()



block t993:
  type PNode = ref object of RootObj

  template litNode(name, ty)  =
    type name = ref object of PNode
      val: ty
  litNode PIntNode, int

  template withKey(j: JsonNode; key: string; varname,
                    body: untyped): typed =
    if j.hasKey(key):
      let varname{.inject.}= j[key]
      block:
        body

  var j = parsejson("{\"zzz\":1}")
  withkey(j, "foo", x):
    echo(x)




block t1337:
  template someIt(a, pred): untyped =
    var it {.inject.} = 0
    pred

  proc aProc(n: auto) =
    n.someIt(echo(it))

  aProc(89)



import mlt
block t4564:
  type Bar = ref object of RootObj
  proc foo(a: Bar): int = 0
  var a: Bar
  let b = a.foo() > 0



block t8052:
  type
    UintImpl[N: static[int], T: SomeUnsignedInt] = object
      raw_data: array[N, T]

  template genLoHi(TypeImpl: untyped): untyped =
    template loImpl[N: static[int], T: SomeUnsignedInt](dst: TypeImpl[N div 2, T], src: TypeImpl[N, T]) =
      let halfSize = N div 2
      for i in 0 ..< halfSize:
        dst.raw_data[i] = src.raw_data[i]

    proc lo[N: static[int], T: SomeUnsignedInt](x: TypeImpl[N,T]): TypeImpl[N div 2, T] {.inline.}=
      loImpl(result, x)

  genLoHi(UintImpl)

  var a: UintImpl[4, uint32]

  a.raw_data = [1'u32, 2'u32, 3'u32, 4'u32]
  doAssert a.lo.raw_data.len == 2
  doAssert a.lo.raw_data[0] == 1
  doAssert a.lo.raw_data[1] == 2



block t2585:
  type
    RenderPass = object
       state: ref int
    RenderData = object
        fb: int
        walls: seq[RenderPass]
    Mat2 = int
    Vector2[T] = T
    Pixels=int

  template use(fb: int, st: untyped): untyped =
      echo "a ", $fb
      st
      echo "a ", $fb

  proc render(rdat: var RenderData; passes: var openarray[RenderPass]; proj: Mat2;
              indexType = 1) =
      for i in 0 ..< len(passes):
          echo "blah ", repr(passes[i])

  proc render2(rdat: var RenderData; screenSz: Vector2[Pixels]; proj: Mat2) =
      use rdat.fb:
          render(rdat, rdat.walls, proj, 1)



block t4292:
  template foo(s: string): string = s
  proc variadicProc(v: varargs[string, foo]) = echo v[0]
  variadicProc("a")



block t2670:
  template testTemplate(b: bool): typed =
    when b:
        var a = "hi"
    else:
        var a = 5
    echo a
  testTemplate(true)



block t4097:
  var i {.compileTime.} = 2

  template defineId(t: typedesc) =
    const id {.genSym.} = i
    static: inc(i)
    proc idFor(T: typedesc[t]): int {.inline, raises: [].} = id

  defineId(int8)
  defineId(int16)

  doAssert idFor(int8) == 2
  doAssert idFor(int16) == 3



block t5235:
  template outer(body: untyped) =
    template test(val: string) =
      const SomeConst: string = val
      echo SomeConst
    body

  outer:
    test("Hello, World!")


# bug #11941
type X = object
  e: int

proc works(T: type X, v: auto): T = T(e: v)
template fails(T: type X, v: auto): T = T(e: v)

var
  w = X.works(42)
  x = X.fails(42)

echo x

import mtempl5


proc foo(): auto =
  trap "foo":
    echo "hey"

discard foo()