From 9af4ce1ca1abb47c11e5c23e62e1cf3d3e1bcb94 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 2 Aug 2023 15:13:32 +0200 Subject: [PATCH] Better compilation error when calling `without` with wrong parameter --- questionable/withoutresult.nim | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/questionable/withoutresult.nim b/questionable/withoutresult.nim index 17cd333..786cc49 100644 --- a/questionable/withoutresult.nim +++ b/questionable/withoutresult.nim @@ -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