Better compilation error when calling `without` with wrong parameter

This commit is contained in:
Mark Spanbroek 2023-08-02 15:13:32 +02:00 committed by markspanbroek
parent 1569ef4526
commit 9af4ce1ca1
1 changed files with 6 additions and 2 deletions

View File

@ -2,13 +2,14 @@ import std/macros
import ./without
import ./private/binderror
const symbolKinds = {nnkSym, nnkOpenSymChoice, nnkClosedSymChoice}
const identKinds = {nnkIdent} + symbolKinds
proc undoSymbolResolution(expression, ident: NimNode): NimNode =
## Finds symbols in the expression that match the `ident` and replaces them
## with `ident`, effectively undoing any symbol resolution that happened
## before.
const symbolKinds = {nnkSym, nnkOpenSymChoice, nnkClosedSymChoice}
if expression.kind in symbolKinds and eqIdent($expression, $ident):
return ident
@ -22,6 +23,9 @@ macro without*(condition, errorname, body: untyped): untyped =
## Used to place guards that ensure that a Result contains a value.
## Exposes error when Result does not contain a value.
if errorname.kind notin identKinds:
error("expected an identifier, got " & errorname.repr, errorname)
let errorIdent = ident $errorname
# Nim's early symbol resolution might have picked up a symbol with the