From 0d4d0002b0f9ee22b27e5c83f280fe5a9d61f9f4 Mon Sep 17 00:00:00 2001 From: zah Date: Wed, 5 Aug 2020 16:28:11 +0300 Subject: [PATCH] Produce an error when the result variable is used in void async procs (#117) --- chronos/asyncmacro2.nim | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/chronos/asyncmacro2.nim b/chronos/asyncmacro2.nim index ec4079f6e3..7e7025e82e 100644 --- a/chronos/asyncmacro2.nim +++ b/chronos/asyncmacro2.nim @@ -200,9 +200,15 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} = # -> complete(retFuture, result) var iteratorNameSym = genSym(nskIterator, $prcName) var procBody = prc.body.processBody(retFutureSym, subtypeIsVoid, - futureVarIdents) + futureVarIdents) # don't do anything with forward bodies (empty) if procBody.kind != nnkEmpty: + if subtypeIsVoid: + let resultTemplate = quote do: + template result: auto {.used.} = + {.fatal: "You should not reference the `result` variable inside a void async proc".} + procBody = newStmtList(resultTemplate, procBody) + # fix #13899, `defer` should not escape its original scope procBody = newStmtList(newTree(nnkBlockStmt, newEmptyNode(), procBody))