tmacro6.nim 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. discard """
  2. errormsg: "expression '123' is of type 'int literal(123)' and has to be used (or discarded)"
  3. line: 71
  4. """
  5. import macros
  6. proc foo(a, b, c: int): int =
  7. result += a
  8. result += b
  9. result += c
  10. macro bar(a, b, c: int): int =
  11. result = newCall(ident"echo")
  12. result.add a
  13. result.add b
  14. result.add c
  15. macro baz(a, b, c: int): int =
  16. let stmt = nnkStmtListExpr.newTree()
  17. stmt.add newCall(ident"echo", a)
  18. stmt.add newCall(ident"echo", b)
  19. stmt.add newCall(ident"echo", c)
  20. stmt.add newLit(123)
  21. return c
  22. # test no result type with explicit return
  23. macro baz2(a, b, c: int) =
  24. let stmt = nnkStmtListExpr.newTree()
  25. stmt.add newCall(ident"echo", a)
  26. stmt.add newCall(ident"echo", b)
  27. stmt.add newCall(ident"echo", c)
  28. return stmt
  29. # test explicit void type with explicit return
  30. macro baz3(a, b, c: int): void =
  31. let stmt = nnkStmtListExpr.newTree()
  32. stmt.add newCall(ident"echo", a)
  33. stmt.add newCall(ident"echo", b)
  34. stmt.add newCall(ident"echo", c)
  35. return stmt
  36. # test no result type with result variable
  37. macro baz4(a, b, c: int) =
  38. result = nnkStmtListExpr.newTree()
  39. result.add newCall(ident"echo", a)
  40. result.add newCall(ident"echo", b)
  41. result.add newCall(ident"echo", c)
  42. # test explicit void type with result variable
  43. macro baz5(a, b, c: int): void =
  44. let result = nnkStmtListExpr.newTree()
  45. result.add newCall(ident"echo", a)
  46. result.add newCall(ident"echo", b)
  47. result.add newCall(ident"echo", c)
  48. macro foobar1(): int =
  49. result = quote do:
  50. echo "Hello World"
  51. 1337
  52. echo foobar1()
  53. # this should create an error message, because 123 has to be discarded.
  54. macro foobar2() =
  55. result = quote do:
  56. echo "Hello World"
  57. 123
  58. echo foobar2()