tdiscardable.nim 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. discard """
  2. output: '''
  3. tdiscardable
  4. 1
  5. 1
  6. something defered
  7. something defered
  8. '''
  9. """
  10. echo "tdiscardable"
  11. # Test the discardable pragma
  12. proc p(x, y: int): int {.discardable.} =
  13. return x + y
  14. # test that it is inherited from generic procs too:
  15. proc q[T](x, y: T): T {.discardable.} =
  16. return x + y
  17. p(8, 2)
  18. q[float](0.8, 0.2)
  19. # bug #942
  20. template maybeMod(x: SomeInteger, module: Natural): untyped =
  21. if module > 0: x mod module
  22. else: x
  23. proc foo(b: int):int =
  24. var x = 1
  25. result = x.maybeMod(b) # Works fine
  26. proc bar(b: int):int =
  27. result = 1
  28. result = result.maybeMod(b) # Error: value returned by statement has to be discarded
  29. echo foo(0)
  30. echo bar(0)
  31. # bug #9726
  32. proc foo: (proc: int) =
  33. proc bar: int = 1
  34. return bar
  35. discard foo()
  36. # bug #10842
  37. proc myDiscardable(): int {.discardable.} =
  38. discard
  39. proc main1() =
  40. defer:
  41. echo "something defered"
  42. discard myDiscardable()
  43. proc main2() =
  44. defer:
  45. echo "something defered"
  46. myDiscardable()
  47. main1()
  48. main2()
  49. block: # bug #13583
  50. block:
  51. proc hello(): int {.discardable.} = 12
  52. iterator test(): int {.closure.} =
  53. while true:
  54. hello()
  55. let t = test
  56. block:
  57. proc hello(): int {.discardable.} = 12
  58. iterator test(): int {.closure.} =
  59. while true:
  60. block:
  61. yield 12
  62. hello()
  63. let t = test
  64. doAssert t() == 12
  65. block:
  66. proc hello(): int {.discardable.} = 12
  67. iterator test(): int {.closure.} =
  68. while true:
  69. yield 12
  70. hello()
  71. let t = test
  72. doAssert t() == 12