Skip to content

Commit

Permalink
Implicit return working for async proc (nim-lang#20933)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
2 people authored and survivorm committed Feb 28, 2023
1 parent 1cd1905 commit 9ddcc13
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/pure/asyncmacro.nim
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,18 @@ proc asyncSingleProc(prc: NimNode): NimNode =
# don't do anything with forward bodies (empty)
if procBody.kind != nnkEmpty:
# fix #13899, defer should not escape its original scope
procBody = newStmtList(newTree(nnkBlockStmt, newEmptyNode(), procBody))
procBody.add(createFutureVarCompletions(futureVarIdents, nil))
let blockStmt = newStmtList(newTree(nnkBlockStmt, newEmptyNode(), procBody))
procBody = newStmtList()
let resultIdent = ident"result"
procBody.add quote do:
template setResult(x: `subRetType`) {.used.} =
# If the proc has implicit return then this will get called
`resultIdent` = x
template setResult(x: untyped) {.used.} =
# If the proc doesn't have implicit return then this will get called
x
procBody.add newCall(ident"setResult", blockStmt)
procBody.add(createFutureVarCompletions(futureVarIdents, nil))
procBody.insert(0): quote do:
{.push warning[resultshadowed]: off.}
when `subRetType` isnot void:
Expand Down
13 changes: 13 additions & 0 deletions tests/async/t11558.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
discard """
output: "Hello\n9"
"""
import std/asyncdispatch

proc foo(): Future[string] {.async.} =
"Hello"

proc bar(): Future[int] {.async.} =
result = 9

echo waitFor foo()
echo waitFor bar()

0 comments on commit 9ddcc13

Please sign in to comment.