summary refs log tree commit diff stats
path: root/tests/init/tcompiles.nim
blob: e86cad1e220589984c0e109eadd85bda150b4b7e (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
discard """
  matrix: "--warningAsError:ProveInit --warningAsError:Uninit"
"""

{.experimental: "strictdefs".}

type Test = object
  id: int

proc foo {.noreturn.} = discard

block:
  proc test(x: bool): Test =
    if x:
      foo()
    else:
      foo()

block:
  proc test(x: bool): Test =
    if x:
      result = Test()
    else:
      foo()

  discard test(true)

block:
  proc test(x: bool): Test =
    if x:
      result = Test()
    else:
      return Test()

  discard test(true)

block:
  proc test(x: bool): Test =
    if x:
      return Test()
    else:
      return Test()

  discard test(true)

block:
  proc test(x: bool): Test =
    if x:
      result = Test()
    else:
      result = Test()
      return

  discard test(true)

block:
  proc test(x: bool): Test =
    if x:
      result = Test()
      return
    else:
      raise newException(ValueError, "unreachable")

  discard test(true)

# bug #21615
# bug #16735

block:
  type Test {.requiresInit.} = object
    id: int

  proc bar(): int =
    raise newException(CatchableError, "error")

  proc test(): Test =
    raise newException(CatchableError, "")

  template catchError(body) =
    var done = false
    try:
      body
    except CatchableError:
      done = true
    doAssert done

  catchError:
    echo test()

  catchError:
    echo bar()