summary refs log tree commit diff stats
path: root/tests/effects/teffects6.nim
blob: d3af224348ee92735153a027efd1232a49d79844 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
discard """
action: compile
"""

# XXX: it is not actually tested if the effects are inferred

type
  PMenu = ref object
  PMenuItem = ref object

proc createMenuItem*(menu: PMenu, label: string,
                    action: proc (i: PMenuItem, p: pointer) {.cdecl.}) = discard

var s: PMenu
createMenuItem(s, "Go to definition...",
      proc (i: PMenuItem, p: pointer) {.cdecl.} =
        try:
          echo(i.repr)
        except ValueError:
          echo("blah")
)


proc noRaise(x: proc()) {.raises: [], effectsOf: x.} =
  # unknown call that might raise anything, but valid:
  x()

proc doRaise() {.raises: [IoError].} =
  raise newException(IoError, "IO")

proc use*() =
  noRaise(doRaise)
  # Here the compiler inferes that EIO can be raised.


use()

# bug #12642
import os

proc raises() {.raises: Exception.} = discard
proc harmless() {.raises: [].} = discard

let x = if paramStr(1) == "true": harmless else: raises

let
  choice = 0

proc withoutSideEffects(): int = 0
proc withSideEffects(): int = echo "foo" # the echo causes the side effect

let procPtr = case choice
              of 0: withoutSideEffects
              else: withSideEffects

echo procPtr.repr