diff options
Diffstat (limited to 'tests/controlflow')
-rw-r--r-- | tests/controlflow/tcontrolflow.nim | 21 | ||||
-rw-r--r-- | tests/controlflow/tunamedbreak.nim | 15 | ||||
-rw-r--r-- | tests/controlflow/tunreachable.nim | 79 | ||||
-rw-r--r-- | tests/controlflow/tunreachable2.nim | 12 |
4 files changed, 126 insertions, 1 deletions
diff --git a/tests/controlflow/tcontrolflow.nim b/tests/controlflow/tcontrolflow.nim index 258f3f50d..dd21a2bb6 100644 --- a/tests/controlflow/tcontrolflow.nim +++ b/tests/controlflow/tcontrolflow.nim @@ -19,7 +19,7 @@ block tbreak: run = false block myblock: if true: - break + break myblock echo "leaving myblock" x = true doAssert(x) @@ -95,3 +95,22 @@ block tnestif: 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/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() |