summary refs log tree commit diff stats
path: root/tests/overflow/toverflow_reorder.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/overflow/toverflow_reorder.nim')
-rw-r--r--tests/overflow/toverflow_reorder.nim84
1 files changed, 84 insertions, 0 deletions
diff --git a/tests/overflow/toverflow_reorder.nim b/tests/overflow/toverflow_reorder.nim
new file mode 100644
index 000000000..fcf7b0c82
--- /dev/null
+++ b/tests/overflow/toverflow_reorder.nim
@@ -0,0 +1,84 @@
+{.experimental: "codeReordering".}
+
+discard """
+  output: "ok"
+  cmd: "nim $target --overflowChecks:off $options $file"
+"""
+# Tests nim's ability to detect overflows
+
+{.push overflowChecks: on.}
+
+var
+  a = high(int)
+  b = -2
+  overflowDetected = false
+
+try:
+  echo b - a
+except OverflowDefect:
+  overflowDetected = true
+
+{.pop.} # overflow check
+
+doAssert(overflowDetected)
+
+block: # Overflow checks in a proc
+  var
+    a = high(int)
+    b = -2
+    overflowDetected = false
+
+  {.push overflowChecks: on.}
+  proc foo() =
+    let c = b - a
+  {.pop.}
+
+  try:
+    foo()
+  except OverflowDefect:
+    overflowDetected = true
+
+  doAssert(overflowDetected)
+
+block: # Overflow checks in a forward declared proc
+  var
+    a = high(int)
+    b = -2
+    overflowDetected = false
+
+  proc foo()
+
+  {.push overflowChecks: on.}
+  proc foo() =
+    let c = b - a
+  {.pop.}
+
+  try:
+    foo()
+  except OverflowDefect:
+    overflowDetected = true
+
+  doAssert(overflowDetected)
+
+block: # Overflow checks doesn't affect fwd declaration
+  var
+    a = high(int)
+    b = -2
+    overflowDetected = false
+
+  {.push overflowChecks: on.}
+  proc foo()
+  {.pop.}
+
+  proc foo() =
+    let c = b - a
+
+  try:
+    foo()
+  except OverflowDefect:
+    overflowDetected = true
+
+  doAssert(not overflowDetected)
+
+
+echo "ok"