summary refs log tree commit diff stats
path: root/tests/arc/tcontrolflow.nim
blob: dbc1159034b7967fd8c3960a6140f5309256b6c5 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
discard """
  output: '''begin A
elif
end A
destroyed
begin false
if
end false
destroyed
begin true
if
end true
7
##index 2 not in 0 .. 1##
true
'''
  cmd: "nim c --gc:arc -d:danger $file"
"""
# we use the -d:danger switch to detect uninitialized stack
# slots more reliably (there shouldn't be any, of course).

type
  Foo = object
    id: int

proc `=destroy`(x: var Foo) =
  if x.id != 0:
    echo "destroyed"
    x.id = 0

proc construct(): Foo = Foo(id: 3)

proc elifIsEasy(cond: bool) =
  echo "begin A"
  if cond:
    echo "if"
  elif construct().id == 3:
    echo "elif"
  else:
    echo "else"
  echo "end A"

elifIsEasy(false)


proc orIsHard(cond: bool) =
  echo "begin ", cond
  if cond or construct().id == 3:
    echo "if"
  else:
    echo "else"
  echo "end ", cond

orIsHard(false)
orIsHard(true)

type
  Control = ref object
    x: int

  MouseEvent = ref object
    control: Control
    button: int

proc run(data: Control) =
  var evt = MouseEvent(button: 1)
  evt.control = data
  if evt.button == 1:
    discard
  else:
    return

  echo data.x

var c = Control(x: 7)

run(c)

proc sysFatal(exceptn: typedesc, message: string) {.inline.} =
  var buf = newStringOfCap(200)
  add(buf, "##")
  add(buf, message)
  add(buf, "##")
  echo buf

proc ifexpr(i, a, b: int) {.compilerproc, noinline.} =
  sysFatal(IndexDefect,
    if b < a: "index out of bounds, the container is empty"
    else: "index " & $i & " not in " & $a & " .. " & $b)

ifexpr(2, 0, 1)

# bug #14899
template toSeq(): untyped =
  block:
    var result = @[1]
    result

proc clItems(s: seq[int]) =
  assert s.len == 1

proc escapeCheck =
  clItems(toSeq())

escapeCheck()

# bug #14900

proc seqsEqual(a, b: string): bool =
  if false:
    false
  else:
    (var result1 = a; result1) == (var result2 = b; result2)

# can be const or var too
let expected = "hello"

echo seqsEqual(expected, expected)