Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@boydgreenfield
Last active December 30, 2015 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boydgreenfield/7866606 to your computer and use it in GitHub Desktop.
Save boydgreenfield/7866606 to your computer and use it in GitHub Desktop.
Disabling overflow checking works for procs but not for blocks within a when isMainModule block?
{.push checks: off.}
proc unsafe_mult2(x: int32): int32 =
result = x * 2
{.pop.}
{.push checks: off.}
proc unsafe_assert(x: int): void =
assert x == (x + 1)
{.pop.}
when isMainModule:
var x, y, z: int32
x = 2147483647'i32 # 2**31 - 1
# First use the proc versions
try:
let x_unsafe = unsafe_mult2(x)
echo("No overflow when checks: off declared around the proc.")
except EOverflow:
echo("Overflow!")
try:
unsafe_assert(10)
echo("Unsafe assert works when checks: off declared around the proc.")
except EAssertionFailed:
echo("Unsafe assert doesn't work.")
# Now look at disabling exceptions w/in the isMainModule block directly
try:
y = x * 2
except EOverflow:
echo("Overflow!")
# Still overflows
{.push checks: off.}
try:
z = x * 2
echo("No overflow!")
except EOverflow:
echo("Overflow!")
{.pop.}
# Still overflows
{.push checks: off.}
block within_a_block:
try:
z = x * 2
echo("No overflow!")
except EOverflow:
echo("Overflow!")
{.pop.}
# Assertion fails
try:
assert 1 == 2
except EAssertionFailed:
echo("In what world? (Assertion checking on)")
{.push checks: off.}
try:
assert 1 == 2
echo("Assertion checking successfully turned off.")
except EAssertionFailed:
echo("In what world?")
{.pop.}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment