tconst.nim 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. discard """
  2. targets: "c cpp js"
  3. """
  4. import std/strutils
  5. template forceConst(a: untyped): untyped =
  6. ## Force evaluation at CT, but `static(a)` is simpler
  7. const ret = a
  8. ret
  9. proc isNimVm(): bool =
  10. when nimvm: result = true
  11. else: result = false
  12. block:
  13. doAssert forceConst(isNimVm())
  14. doAssert not isNimVm()
  15. doAssert forceConst(isNimVm()) == static(isNimVm())
  16. doAssert forceConst(isNimVm()) == isNimVm().static
  17. template main() =
  18. # xxx merge more const related tests here
  19. const ct = CompileTime
  20. # refs https://github.com/timotheecour/Nim/issues/718, apparently `CompileTime`
  21. # isn't cached, which seems surprising.
  22. block:
  23. const
  24. a = """
  25. Version $1|
  26. Compiled at: $2, $3
  27. """ % [NimVersion & spaces(44-len(NimVersion)), CompileDate, ct]
  28. let b = $a
  29. doAssert ct in b, $(b, ct)
  30. doAssert NimVersion in b
  31. block: # Test for fix on broken const unpacking
  32. template mytemp() =
  33. const
  34. (x, increment) = (4, true)
  35. a = 100
  36. discard (x, increment, a)
  37. mytemp()
  38. static: main()
  39. main()