Fix: nested without calls in generic code

This commit is contained in:
Mark Spanbroek 2022-08-09 08:54:26 +02:00 committed by markspanbroek
parent 4abeef5c36
commit 90ea780ba9
2 changed files with 20 additions and 4 deletions

View File

@ -1,15 +1,15 @@
import std/options
var captureEnabled {.global, compileTime.}: bool
var captures {.global, compileTime.}: int
var errorVariable: ptr ref CatchableError
template captureBindError*(error: var ref CatchableError, expression): auto =
let previousErrorVariable = errorVariable
errorVariable = addr error
static: captureEnabled = true
static: inc captures
let evaluated = expression
static: captureEnabled = false
static: dec captures
errorVariable = previousErrorVariable
@ -19,6 +19,6 @@ func error[T](option: Option[T]): ref CatchableError =
newException(ValueError, "Option is set to `none`")
template bindFailed*(expression) =
when captureEnabled:
when captures > 0:
mixin error
errorVariable[] = expression.error

View File

@ -294,6 +294,22 @@ suite "result":
foo()
test "without statement with error works in nested generic calls":
proc works(_: type int): ?!int =
without _ =? int.failure "error1", err:
return success 42
proc fails(_: type int): ?!int =
return failure "error2"
proc foo =
without a =? int.works() and b =? int.fails(), error:
check error.msg == "error2"
return
fail()
foo()
test "catch can be used to convert exceptions to results":
check parseInt("42").catch == 42.success
check parseInt("foo").catch.error of ValueError