toverflow_reorder.nim 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. {.experimental: "codeReordering".}
  2. discard """
  3. output: "ok"
  4. cmd: "nim $target --overflowChecks:off $options $file"
  5. """
  6. # Tests nim's ability to detect overflows
  7. {.push overflowChecks: on.}
  8. var
  9. a = high(int)
  10. b = -2
  11. overflowDetected = false
  12. try:
  13. echo b - a
  14. except OverflowDefect:
  15. overflowDetected = true
  16. {.pop.} # overflow check
  17. doAssert(overflowDetected)
  18. block: # Overflow checks in a proc
  19. var
  20. a = high(int)
  21. b = -2
  22. overflowDetected = false
  23. {.push overflowChecks: on.}
  24. proc foo() =
  25. let c = b - a
  26. {.pop.}
  27. try:
  28. foo()
  29. except OverflowDefect:
  30. overflowDetected = true
  31. doAssert(overflowDetected)
  32. block: # Overflow checks in a forward declared proc
  33. var
  34. a = high(int)
  35. b = -2
  36. overflowDetected = false
  37. proc foo()
  38. {.push overflowChecks: on.}
  39. proc foo() =
  40. let c = b - a
  41. {.pop.}
  42. try:
  43. foo()
  44. except OverflowDefect:
  45. overflowDetected = true
  46. doAssert(overflowDetected)
  47. block: # Overflow checks doesn't affect fwd declaration
  48. var
  49. a = high(int)
  50. b = -2
  51. overflowDetected = false
  52. {.push overflowChecks: on.}
  53. proc foo()
  54. {.pop.}
  55. proc foo() =
  56. let c = b - a
  57. try:
  58. foo()
  59. except OverflowDefect:
  60. overflowDetected = true
  61. doAssert(not overflowDetected)
  62. echo "ok"