summary refs log tree commit diff stats
path: root/tests/vm/tconst.nim
blob: c2bcf78b59793a334efb2863f2796e0b3bc77c61 (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
discard """
  targets: "c cpp js"
"""

import std/strutils

template forceConst(a: untyped): untyped =
  ## Force evaluation at CT, but `static(a)` is simpler
  const ret = a
  ret

proc isNimVm(): bool =
  when nimvm: result = true
  else: result = false

block:
  doAssert forceConst(isNimVm())
  doAssert not isNimVm()
  doAssert forceConst(isNimVm()) == static(isNimVm())
  doAssert forceConst(isNimVm()) == isNimVm().static

template main() =
  # xxx merge more const related tests here
  const ct = CompileTime
    # refs https://github.com/timotheecour/Nim/issues/718, apparently `CompileTime`
    # isn't cached, which seems surprising.
  block:
    const
      a = """
    Version $1|
    Compiled at: $2, $3
    """ % [NimVersion & spaces(44-len(NimVersion)), CompileDate, ct]
    let b = $a
    doAssert ct in b, $(b, ct)
    doAssert NimVersion in b

  block: # Test for fix on broken const unpacking
    template mytemp() =
      const
        (x, increment) = (4, true)
        a = 100
      discard (x, increment, a)
    mytemp()



static: main()
main()