tdiscardable.nim 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. discard """
  2. output: '''
  3. tdiscardable
  4. 1
  5. 1
  6. something defered
  7. something defered
  8. hi
  9. '''
  10. """
  11. echo "tdiscardable"
  12. # Test the discardable pragma
  13. proc p(x, y: int): int {.discardable.} =
  14. return x + y
  15. # test that it is inherited from generic procs too:
  16. proc q[T](x, y: T): T {.discardable.} =
  17. return x + y
  18. p(8, 2)
  19. q[float](0.8, 0.2)
  20. # bug #942
  21. template maybeMod(x: SomeInteger, module: Natural): untyped =
  22. if module > 0: x mod module
  23. else: x
  24. proc foo(b: int):int =
  25. var x = 1
  26. result = x.maybeMod(b) # Works fine
  27. proc bar(b: int):int =
  28. result = 1
  29. result = result.maybeMod(b) # Error: value returned by statement has to be discarded
  30. echo foo(0)
  31. echo bar(0)
  32. # bug #9726
  33. proc foo: (proc: int) =
  34. proc bar: int = 1
  35. return bar
  36. discard foo()
  37. # bug #10842
  38. proc myDiscardable(): int {.discardable.} =
  39. discard
  40. proc main1() =
  41. defer:
  42. echo "something defered"
  43. discard myDiscardable()
  44. proc main2() =
  45. defer:
  46. echo "something defered"
  47. myDiscardable()
  48. main1()
  49. main2()
  50. block: # bug #13583
  51. block:
  52. proc hello(): int {.discardable.} = 12
  53. iterator test(): int {.closure.} =
  54. while true:
  55. hello()
  56. let t = test
  57. block:
  58. proc hello(): int {.discardable.} = 12
  59. iterator test(): int {.closure.} =
  60. while true:
  61. block:
  62. yield 12
  63. hello()
  64. let t = test
  65. doAssert t() == 12
  66. block:
  67. proc hello(): int {.discardable.} = 12
  68. iterator test(): int {.closure.} =
  69. while true:
  70. yield 12
  71. hello()
  72. let t = test
  73. doAssert t() == 12
  74. block:
  75. proc bar(): string {.discardable.} =
  76. "15"
  77. proc foo(): int =
  78. while true:
  79. raise newException(ValueError, "check")
  80. 12
  81. doAssertRaises(ValueError):
  82. doAssert foo() == 12
  83. block: # issue #10440
  84. proc x(): int {.discardable.} = discard
  85. try:
  86. x()
  87. finally:
  88. echo "hi"
  89. import macros
  90. block: # issue #14665
  91. macro test(): untyped =
  92. let b = @[1, 2, 3, 4]
  93. result = nnkStmtList.newTree()
  94. var i = 0
  95. while i < b.len:
  96. if false:
  97. # this quote do is mandatory, removing it fixes the problem
  98. result.add quote do:
  99. let testtest = 5
  100. else:
  101. result.add quote do:
  102. let test = 6
  103. inc i
  104. # removing this continue fixes the problem too
  105. continue
  106. inc i
  107. test()
  108. block: # bug #23775
  109. proc retInt(): int {.discardable.} =
  110. 42
  111. proc retString(): string {.discardable.} =
  112. "text"
  113. type
  114. Enum = enum
  115. A, B, C, D
  116. proc doStuff(msg: Enum) =
  117. case msg:
  118. of A:
  119. retString()
  120. of B:
  121. retInt()
  122. of C:
  123. discard retString()
  124. else:
  125. let _ = retString()
  126. doStuff(C)
  127. block:
  128. proc test(): (int, int) {.discardable.} =
  129. discard
  130. if true:
  131. test()
  132. else:
  133. quit()