teffects6.nim 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. discard """
  2. action: compile
  3. """
  4. # XXX: it is not actually tested if the effects are inferred
  5. type
  6. PMenu = ref object
  7. PMenuItem = ref object
  8. proc createMenuItem*(menu: PMenu, label: string,
  9. action: proc (i: PMenuItem, p: pointer) {.cdecl.}) = discard
  10. var s: PMenu
  11. createMenuItem(s, "Go to definition...",
  12. proc (i: PMenuItem, p: pointer) {.cdecl.} =
  13. try:
  14. echo(i.repr)
  15. except ValueError:
  16. echo("blah")
  17. )
  18. proc noRaise(x: proc()) {.raises: [], effectsOf: x.} =
  19. # unknown call that might raise anything, but valid:
  20. x()
  21. proc doRaise() {.raises: [IoError].} =
  22. raise newException(IoError, "IO")
  23. proc use*() =
  24. noRaise(doRaise)
  25. # Here the compiler inferes that EIO can be raised.
  26. use()
  27. # bug #12642
  28. import os
  29. proc raises() {.raises: Exception.} = discard
  30. proc harmless() {.raises: [].} = discard
  31. let x = if paramStr(1) == "true": harmless else: raises
  32. let
  33. choice = 0
  34. proc withoutSideEffects(): int = 0
  35. proc withSideEffects(): int = echo "foo" # the echo causes the side effect
  36. let procPtr = case choice
  37. of 0: withoutSideEffects
  38. else: withSideEffects
  39. echo procPtr.repr