summary refs log tree commit diff stats
path: root/tests/controlflow
diff options
context:
space:
mode:
Diffstat (limited to 'tests/controlflow')
-rw-r--r--tests/controlflow/tblock1.nim16
-rw-r--r--tests/controlflow/tcontrolflow.nim116
-rw-r--r--tests/controlflow/tstatret.nim9
-rw-r--r--tests/controlflow/tunamedbreak.nim15
-rw-r--r--tests/controlflow/tunreachable.nim79
-rw-r--r--tests/controlflow/tunreachable2.nim12
6 files changed, 247 insertions, 0 deletions
diff --git a/tests/controlflow/tblock1.nim b/tests/controlflow/tblock1.nim
new file mode 100644
index 000000000..70c844513
--- /dev/null
+++ b/tests/controlflow/tblock1.nim
@@ -0,0 +1,16 @@
+discard """
+  errormsg: "undeclared identifier: \'ha\'"
+  file: "tblock1.nim"
+  line: 14
+"""
+# check for forward label and
+# for failure when label is not declared
+
+proc main =
+  block endLess:
+    write(stdout, "Muaahh!\N")
+    break endLess
+
+  break ha #ERROR
+
+main()
diff --git a/tests/controlflow/tcontrolflow.nim b/tests/controlflow/tcontrolflow.nim
new file mode 100644
index 000000000..dd21a2bb6
--- /dev/null
+++ b/tests/controlflow/tcontrolflow.nim
@@ -0,0 +1,116 @@
+discard """
+  output: '''
+10
+true true
+true false
+false true
+false false
+i == 2
+'''
+"""
+
+
+block tbreak:
+  var
+    x = false
+    run = true
+
+  while run:
+    run = false
+    block myblock:
+      if true:
+        break myblock
+      echo "leaving myblock"
+    x = true
+  doAssert(x)
+
+  # bug #1418
+  iterator foo: int =
+    for x in 0 .. 9:
+      for y in [10,20,30,40,50,60,70,80,90]:
+        yield x + y
+
+  for p in foo():
+    echo p
+    break
+
+  iterator permutations: int =
+    yield 10
+
+  for p in permutations():
+    break
+
+  # regression:
+  proc main =
+    for x in [true, false]:
+      for y in [true, false]:
+        echo x, " ", y
+
+  main()
+
+
+
+block tcontinue:
+  var i = 0
+  while i < 400:
+
+    if i == 10: break
+    elif i == 3:
+      inc i
+      continue
+    inc i
+
+  var f = "failure"
+  var j = 0
+  while j < 300:
+    for x in 0..34:
+      if j < 300: continue
+      if x == 10:
+        echo "failure: should never happen"
+        break
+    f = "came here"
+    break
+
+  if i == 10:
+    doAssert f == "came here"
+  else:
+    echo "failure"
+
+
+
+block tnestif:
+  var
+      x, y: int
+  x = 2
+  if x == 0:
+      write(stdout, "i == 0")
+      if y == 0:
+          writeLine(stdout, x)
+      else:
+          writeLine(stdout, y)
+  elif x == 1:
+      writeLine(stdout, "i == 1")
+  elif x == 2:
+      writeLine(stdout, "i == 2")
+  else:
+      writeLine(stdout, "looks like Python")
+  #OUT i == 2
+
+# bug https://github.com/nim-lang/RFCs/issues/451
+for i in 1..2: # works
+  break
+
+block: # works
+  for i in 1..2:
+    break
+
+block: # works
+  block:
+    discard 12 + 3
+  for i in 1..2:
+    break
+
+block named: # works
+  if true:
+    break named
+  doAssert false, "not reached"
diff --git a/tests/controlflow/tstatret.nim b/tests/controlflow/tstatret.nim
new file mode 100644
index 000000000..a3558e6f4
--- /dev/null
+++ b/tests/controlflow/tstatret.nim
@@ -0,0 +1,9 @@
+discard """
+  nimout: '''
+tstatret.nim(9, 7) Warning: unreachable code after 'return' statement or '{.noReturn.}' proc [UnreachableCode]
+'''
+"""
+# no statement after return
+proc main() =
+  return
+  echo("huch?") #ERROR_MSG statement not allowed after
diff --git a/tests/controlflow/tunamedbreak.nim b/tests/controlflow/tunamedbreak.nim
new file mode 100644
index 000000000..46113cabc
--- /dev/null
+++ b/tests/controlflow/tunamedbreak.nim
@@ -0,0 +1,15 @@
+
+discard """
+  cmd: "nim check $file"
+  action: "reject"
+  nimout: '''
+tunamedbreak.nim(12, 5) Error: Using an unnamed break in a block is deprecated; Use a named block with a named break instead [UnnamedBreak]
+tunamedbreak.nim(15, 3) Error: Using an unnamed break in a block is deprecated; Use a named block with a named break instead [UnnamedBreak]
+  '''
+"""
+for i in 1..2: # errors
+  block:
+    break
+
+block: # errors
+  break
diff --git a/tests/controlflow/tunreachable.nim b/tests/controlflow/tunreachable.nim
new file mode 100644
index 000000000..06321ce8a
--- /dev/null
+++ b/tests/controlflow/tunreachable.nim
@@ -0,0 +1,79 @@
+discard """
+  cmd: "nim check --warningAsError:UnreachableCode $file"
+  action: "reject"
+  nimout: '''
+tunreachable.nim(26, 3) Error: unreachable code after 'return' statement or '{.noReturn.}' proc [UnreachableCode]
+tunreachable.nim(33, 3) Error: unreachable code after 'return' statement or '{.noReturn.}' proc [UnreachableCode]
+tunreachable.nim(42, 3) Error: unreachable code after 'return' statement or '{.noReturn.}' proc [UnreachableCode]
+tunreachable.nim(65, 5) Error: unreachable code after 'return' statement or '{.noReturn.}' proc [UnreachableCode]
+tunreachable.nim(77, 5) Error: unreachable code after 'return' statement or '{.noReturn.}' proc [UnreachableCode]
+'''
+"""
+  
+# bug #9839
+template myquit1():untyped=
+  ## foo
+  quit(1)
+template myquit2():untyped=
+  echo 123
+  myquit1()
+
+proc main1()=
+
+  # BUG: uncommenting this doesn't give `Error: unreachable statement`
+  myquit2()
+
+  echo "after"
+
+main1()
+
+proc main2() =
+  myquit1()
+
+  echo "after"
+
+main2()
+
+proc main3() =
+  if true:
+    return
+  else:
+    return
+  echo "after"
+
+main3()
+
+
+block:
+  # Cases like strings are not checked for exhaustiveness unless they have an else
+  proc main4(x: string) =
+    case x
+    of "a":
+      return
+    # reachable
+    echo "after"
+
+  main4("a")
+
+  proc main5(x: string) =
+    case x
+    of "a":
+      return
+    else:
+      return
+    # unreachable
+    echo "after"
+
+  main5("a")
+
+block:
+  # In this case no else is needed because it's exhaustive
+  proc exhaustive(x: bool) =
+    case x
+    of true:
+      return
+    of false:
+      return
+    echo "after"
+
+  exhaustive(true)
diff --git a/tests/controlflow/tunreachable2.nim b/tests/controlflow/tunreachable2.nim
new file mode 100644
index 000000000..a658880f0
--- /dev/null
+++ b/tests/controlflow/tunreachable2.nim
@@ -0,0 +1,12 @@
+discard """
+  matrix: "--warningAsError:UnreachableCode"
+"""
+
+proc test(): bool =
+  block okay:
+    if true: break okay
+    return false
+
+  return true # Line 7 is here
+
+doAssert test()