tunreachable.nim 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. discard """
  2. cmd: "nim check --warningAsError:UnreachableCode $file"
  3. action: "reject"
  4. nimout: '''
  5. tunreachable.nim(26, 3) Error: unreachable code after 'return' statement or '{.noReturn.}' proc [UnreachableCode]
  6. tunreachable.nim(33, 3) Error: unreachable code after 'return' statement or '{.noReturn.}' proc [UnreachableCode]
  7. tunreachable.nim(42, 3) Error: unreachable code after 'return' statement or '{.noReturn.}' proc [UnreachableCode]
  8. tunreachable.nim(65, 5) Error: unreachable code after 'return' statement or '{.noReturn.}' proc [UnreachableCode]
  9. tunreachable.nim(77, 5) Error: unreachable code after 'return' statement or '{.noReturn.}' proc [UnreachableCode]
  10. '''
  11. """
  12. # bug #9839
  13. template myquit1():untyped=
  14. ## foo
  15. quit(1)
  16. template myquit2():untyped=
  17. echo 123
  18. myquit1()
  19. proc main1()=
  20. # BUG: uncommenting this doesn't give `Error: unreachable statement`
  21. myquit2()
  22. echo "after"
  23. main1()
  24. proc main2() =
  25. myquit1()
  26. echo "after"
  27. main2()
  28. proc main3() =
  29. if true:
  30. return
  31. else:
  32. return
  33. echo "after"
  34. main3()
  35. block:
  36. # Cases like strings are not checked for exhaustiveness unless they have an else
  37. proc main4(x: string) =
  38. case x
  39. of "a":
  40. return
  41. # reachable
  42. echo "after"
  43. main4("a")
  44. proc main5(x: string) =
  45. case x
  46. of "a":
  47. return
  48. else:
  49. return
  50. # unreachable
  51. echo "after"
  52. main5("a")
  53. block:
  54. # In this case no else is needed because it's exhaustive
  55. proc exhaustive(x: bool) =
  56. case x
  57. of true:
  58. return
  59. of false:
  60. return
  61. echo "after"
  62. exhaustive(true)