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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
# bug #4462
import macros
import os
block:
proc foo(t: typedesc) {.compileTime.} =
assert sameType(getType(t), getType(int))
static:
foo(int)
# #4412
block:
proc default[T](t: typedesc[T]): T {.inline.} = discard
static:
var x = default(type(0))
# #6379
import algorithm
static:
var numArray = [1, 2, 3, 4, -1]
numArray.sort(cmp)
assert numArray == [-1, 1, 2, 3, 4]
var str = "cba"
str.sort(cmp)
assert str == "abc"
# #6086
import math, sequtils, sugar
block:
proc f: int =
toSeq(10..<10_000).filter(
a => a == ($a).map(
d => (d.ord-'0'.ord).int^4
).sum
).sum
var a = f()
const b = f()
assert a == b
block:
proc f(): seq[char] =
result = "hello".map(proc(x: char): char = x)
var runTime = f()
const compTime = f()
assert runTime == compTime
# #6083
block:
proc abc(): seq[int] =
result = @[0]
result.setLen(2)
var tmp: int
for i in 0 ..< 2:
inc tmp
result[i] = tmp
const fact1000 = abc()
assert fact1000 == @[1, 2]
# Tests for VM ops
block:
static:
# for joint test, the project path is different, so I disabled it:
when false:
assert "vm" in getProjectPath()
let b = getEnv("UNSETENVVAR")
assert b == ""
assert existsEnv("UNSERENVVAR") == false
putEnv("UNSETENVVAR", "VALUE")
assert getEnv("UNSETENVVAR") == "VALUE"
assert existsEnv("UNSETENVVAR") == true
assert fileExists("MISSINGFILE") == false
assert dirExists("MISSINGDIR") == false
# #7210
block:
static:
proc f(size: int): int =
var some = newStringOfCap(size)
result = size
doAssert f(4) == 4
# #6689
block:
static:
proc foo(): int = 0
var f: proc(): int
doAssert f.isNil
f = foo
doAssert(not f.isNil)
block:
static:
var x: ref ref int
new(x)
doAssert(not x.isNil)
# #7871
static:
type Obj = object
field: int
var s = newSeq[Obj](1)
var o = Obj()
s[0] = o
o.field = 2
doAssert s[0].field == 0
# #8125
static:
let def_iter_var = ident("it")
# #8142
static:
type Obj = object
names: string
proc pushName(o: var Obj) =
var s = ""
s.add("FOOBAR")
o.names.add(s)
var o = Obj()
o.names = ""
o.pushName()
o.pushName()
doAssert o.names == "FOOBARFOOBAR"
# #8154
import parseutils
static:
type Obj = object
i: int
proc foo(): Obj =
discard parseInt("1", result.i, 0)
static:
doAssert foo().i == 1
# #10333
block:
const
encoding: auto = [
["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"],
["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"],
["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"],
["", "M", "MM", "MMM", "--", "-", "--", "---", "----", "--"],
]
doAssert encoding.len == 4
# #10886
proc tor(): bool =
result = true
result = false or result
proc tand(): bool =
result = false
result = true and result
const
ctor = tor()
ctand = not tand()
static:
doAssert ctor
doAssert ctand
block: # bug #13081
type Kind = enum
k0, k1, k2, k3
type Foo = object
x0: float
case kind: Kind
of k0: discard
of k1: x1: int
of k2: x2: string
of k3: x3: string
const j1 = Foo(x0: 1.2, kind: k1, x1: 12)
const j2 = Foo(x0: 1.3, kind: k2, x2: "abc")
const j3 = Foo(x0: 1.3, kind: k3, x3: "abc2")
static:
doAssert $j1 == "(x0: 1.2, kind: k1, x1: 12)"
doAssert $j2 == """(x0: 1.3, kind: k2, x2: "abc")"""
doAssert $j3 == """(x0: 1.3, kind: k3, x3: "abc2")"""
doAssert $j1 == "(x0: 1.2, kind: k1, x1: 12)"
doAssert $j2 == """(x0: 1.3, kind: k2, x2: "abc")"""
doAssert $j3 == """(x0: 1.3, kind: k3, x3: "abc2")"""
doAssert j1.x1 == 12
static:
doAssert j1.x1 == 12
block: # bug #15595
proc fn0()=echo 0
proc fn1()=discard
proc main=
var local = 0
proc fn2()=echo local
var a0 = fn0
var a1 = fn1
var a2 = fn2
var a3: proc()
var a4: proc()
doAssert a0 == fn0 # bugfix
doAssert a1 == fn1 # ditto
doAssert a2 == fn2 # ditto
doAssert fn0 != fn1
doAssert a2 != nil
doAssert a3 == nil # bugfix
doAssert a3 == a4 # bugfix
static: main()
main()
# bug #15363
import sequtils
block:
func identity(a: bool): bool = a
var a: seq[bool] = static:
newSeq[bool](0).mapIt(it) # segfaults
var b: seq[bool] = static:
newSeq[bool](0).filterIt(it) # does not segfault
var c: seq[bool] = static:
newSeq[bool](0).map(identity) # does not segfault
var d: seq[bool] = static:
newSeq[bool](0).map(proc (a: bool): bool = false) # segfaults
var e: seq[bool] = static:
newSeq[bool](0).filter(identity) # does not segfault
var f: seq[bool] = static:
newSeq[bool](0).filter(proc (a: bool): bool = false) # segfaults
doAssert a == @[]
doAssert b == @[]
doAssert c == @[]
doAssert d == @[]
doAssert e == @[]
doAssert f == @[]
import tables
block: # bug #8007
type
CostKind = enum
Fixed,
Dynamic
Cost = object
case kind*: CostKind
of Fixed:
cost*: int
of Dynamic:
handler*: proc(value: int): int {.nimcall.}
proc foo(value: int): int {.nimcall.} =
sizeof(value)
const a: array[2, Cost] =[
Cost(kind: Fixed, cost: 999),
Cost(kind: Dynamic, handler: foo)
]
# OK with arrays & object variants
doAssert $a == "[(kind: Fixed, cost: 999), (kind: Dynamic, handler: ...)]"
const b: Table[int, Cost] = {
0: Cost(kind: Fixed, cost: 999),
1: Cost(kind: Dynamic, handler: foo)
}.toTable
# KO with Tables & object variants
# echo b # {0: (kind: Fixed, cost: 0), 1: (kind: Dynamic, handler: ...)} # <----- wrong behaviour
doAssert $b == "{0: (kind: Fixed, cost: 999), 1: (kind: Dynamic, handler: ...)}"
const c: Table[int, int] = {
0: 100,
1: 999
}.toTable
# OK with Tables and primitive int
doAssert $c == "{0: 100, 1: 999}"
# For some reason the following gives
# Error: invalid type for const: Cost
const d0 = Cost(kind: Fixed, cost: 999)
# OK with seq & object variants
const d = @[Cost(kind: Fixed, cost: 999), Cost(kind: Dynamic, handler: foo)]
doAssert $d == "@[(kind: Fixed, cost: 999), (kind: Dynamic, handler: ...)]"
block: # VM wrong register free causes errors in unrelated code
block: # bug #15597
#[
Error: unhandled exception: 'sym' is not accessible using discriminant 'kind' of type 'TNode' [FieldDefect]
in /Users/timothee/git_clone/nim/Nim_prs/compiler/vm.nim(1176) rawExecute
in opcIndCall
in let prc = if not isClosure: bb.sym else: bb[0].sym
]#
proc bar2(head: string): string = "asdf"
proc gook(u1: int) = discard
type PathEntry = object
kind: int
path: string
iterator globOpt(): int =
var u1: int
gook(u1)
gook(u1)
gook(u1)
gook(u1)
gook(u1)
gook(u1)
gook(u1)
gook(u1)
gook(u1)
gook(u1)
gook(u1)
gook(u1)
gook(u1)
gook(u1)
var entry = PathEntry()
entry.path = bar2("")
if false:
echo "here2"
proc processAux(a: float) = discard
template bar(iter: untyped): untyped =
var ret: float
for x in iter: break
ret
proc main() =
processAux(bar(globOpt()))
static: main()
block: # ditto
# D20201024T133245
type Deque = object
proc initDeque2(initialSize: int = 4): Deque = Deque()
proc len2(a: Deque): int = 2
proc baz(dir: string): bool = true
proc bar2(head: string): string = "asdf"
proc bar3(path: var string) = path = path
type PathEntry = object
kind: int
path: string
proc initGlobOpt(dir: string, a1=false,a2=false,a3=false,a4=false): string = dir
iterator globOpt(dir: string): int =
var stack = initDeque2()
doAssert baz("")
let z = stack.len2
if stack.len2 >= 0:
var entry = PathEntry()
let current = if true: stack.len2 else: stack.len2
entry.path = bar2("")
bar3(entry.path)
if false:
echo "here2" # comment here => you get same error as https://github.com/nim-lang/Nim/issues/15704
proc processAux(a: float) = discard
template bar(iter: untyped): untyped =
var ret: float
for x in iter: break
ret
proc main() =
processAux(bar(globOpt(initGlobOpt("."))))
static: main()
block: # bug #15704
#[
Error: attempt to access a nil address kind: rkFloat
]#
type Deque = object
proc initDeque2(initialSize: int = 4): Deque = Deque()
proc len2(a: Deque): int = 2
proc baz(dir: string): bool = true
proc bar2(head: string): string = "asdf"
proc bar3(path: var string) = path = path
type PathEntry = object
kind: int
path: string
depth: int
proc initGlobOpt(dir: string, a1=false,a2=false,a3=false,a4=false): string =
dir
iterator globOpt(dir: string): int =
var stack = initDeque2()
doAssert baz("")
let z = stack.len2
var a5: int
if stack.len2 >= 0:
var entry = PathEntry()
if false:
echo "here"
let current = if true: stack.len2 else: stack.len2
entry.depth = 1
entry.path = bar2("")
bar3(entry.path)
proc processAux(a: float) = discard
template bar(iter: untyped): untyped =
var ret: float
for x in iter:
break
ret
const dir = "."
proc main() =
processAux(bar(globOpt(initGlobOpt(dir))))
static: main()
|