Skip to content

Commit 9ddcc13

Browse files
ire4ever1190Araq
authored andcommitted
Implicit return working for async proc (nim-lang#20933)
* Implicit return working for asyncdispatch proc Closes nim-lang#11558 * Test case * Test that return value is actually used * Update tests/async/t11558.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
1 parent 1cd1905 commit 9ddcc13

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

lib/pure/asyncmacro.nim

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,18 @@ proc asyncSingleProc(prc: NimNode): NimNode =
214214
# don't do anything with forward bodies (empty)
215215
if procBody.kind != nnkEmpty:
216216
# fix #13899, defer should not escape its original scope
217-
procBody = newStmtList(newTree(nnkBlockStmt, newEmptyNode(), procBody))
218-
procBody.add(createFutureVarCompletions(futureVarIdents, nil))
217+
let blockStmt = newStmtList(newTree(nnkBlockStmt, newEmptyNode(), procBody))
218+
procBody = newStmtList()
219219
let resultIdent = ident"result"
220+
procBody.add quote do:
221+
template setResult(x: `subRetType`) {.used.} =
222+
# If the proc has implicit return then this will get called
223+
`resultIdent` = x
224+
template setResult(x: untyped) {.used.} =
225+
# If the proc doesn't have implicit return then this will get called
226+
x
227+
procBody.add newCall(ident"setResult", blockStmt)
228+
procBody.add(createFutureVarCompletions(futureVarIdents, nil))
220229
procBody.insert(0): quote do:
221230
{.push warning[resultshadowed]: off.}
222231
when `subRetType` isnot void:

tests/async/t11558.nim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
discard """
2+
output: "Hello\n9"
3+
"""
4+
import std/asyncdispatch
5+
6+
proc foo(): Future[string] {.async.} =
7+
"Hello"
8+
9+
proc bar(): Future[int] {.async.} =
10+
result = 9
11+
12+
echo waitFor foo()
13+
echo waitFor bar()

0 commit comments

Comments
 (0)