mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-02 13:53:11 +00:00
Fix error when using an existing name as error variable
This commit is contained in:
parent
90ea780ba9
commit
cfe17ca899
@ -1,12 +1,16 @@
|
||||
import std/macros
|
||||
import ./without
|
||||
import ./private/binderror
|
||||
|
||||
template without*(condition, errorname, body): untyped =
|
||||
macro without*(condition, errorname, body): untyped =
|
||||
## Used to place guards that ensure that a Result contains a value.
|
||||
## Exposes error when Result does not contain a value.
|
||||
|
||||
var error: ref CatchableError
|
||||
let errorIdent = ident $errorname
|
||||
|
||||
without captureBindError(error, condition):
|
||||
template errorname: ref CatchableError = error
|
||||
body
|
||||
quote do:
|
||||
var error: ref CatchableError
|
||||
|
||||
without captureBindError(error, `condition`):
|
||||
template `errorIdent`: ref CatchableError = error
|
||||
`body`
|
||||
|
||||
@ -281,6 +281,15 @@ suite "result":
|
||||
check e2.msg == "error2"
|
||||
check e1.msg == "error1"
|
||||
|
||||
test "without statement works in generic code using existing error name":
|
||||
let existingName {.used.} = "some variable"
|
||||
|
||||
proc shouldCompile(_: type int): ?!int =
|
||||
without _ =? int.failure "error", existingName:
|
||||
return success 42
|
||||
|
||||
discard int.shouldCompile()
|
||||
|
||||
test "without statements with error work in nested calls":
|
||||
proc bar(): ?!int =
|
||||
without _ =? int.failure "error", err:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user