tpragmas_misc.nim 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. ##[
  2. tests for misc pragmas that don't need a separate file
  3. ]##
  4. block:
  5. static: doAssert not defined(tpragmas_misc_def)
  6. {.undef(tpragmas_misc_def).} # works even if not set
  7. static: doAssert not defined(tpragmas_misc_def)
  8. {.define(tpragmas_misc_def).}
  9. static: doAssert defined(tpragmas_misc_def)
  10. {.undef(tpragmas_misc_def).}
  11. static: doAssert not defined(tpragmas_misc_def)
  12. block: # (partial fix) bug #15920
  13. block: # var template pragmas don't work in templates
  14. template foo(expr) =
  15. expr
  16. proc fun1()=
  17. let a {.foo.} = 1
  18. template fun2()=
  19. let a {.foo.} = 1
  20. fun1() # ok
  21. fun2() # WAS bug
  22. template foo2() = discard # distractor (template or other symbol kind)
  23. block:
  24. template foo2(expr) =
  25. expr
  26. proc fun1()=
  27. let a {.foo2.} = 1
  28. template fun2()=
  29. let a {.foo2.} = 1
  30. fun1() # ok
  31. fun2() # bug: Error: invalid pragma: foo2
  32. block: # template pragmas don't work for templates, #18212
  33. # adapted from $nim/lib/std/private/since.nim
  34. # case without overload
  35. template since3(version: (int, int), body: untyped) {.dirty.} =
  36. when (NimMajor, NimMinor) >= version:
  37. body
  38. when true: # bug
  39. template fun3(): int {.since3: (1, 3).} = 12
  40. block: # ditto, w
  41. # case with overload
  42. template since2(version: (int, int), body: untyped) {.dirty.} =
  43. when (NimMajor, NimMinor) >= version:
  44. body
  45. template since2(version: (int, int, int), body: untyped) {.dirty.} =
  46. when (NimMajor, NimMinor, NimPatch) >= version:
  47. body
  48. when true: # bug
  49. template fun3(): int {.since2: (1, 3).} = 12
  50. when true: # D20210801T100514:here
  51. from macros import genSym
  52. block:
  53. template fn() =
  54. var ret {.gensym.}: int # must special case template pragmas so it doesn't get confused
  55. discard ret
  56. fn()
  57. static: discard genSym()
  58. block: # issue #10994
  59. macro foo(x): untyped = x
  60. template bar {.pragma.}
  61. proc a {.bar.} = discard # works
  62. proc b {.bar, foo.} = discard # doesn't