Implicit return working for async proc (#20933)
* Implicit return working for asyncdispatch proc Closes #11558 * Test case * Test that return value is actually used * Update tests/async/t11558.nim Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
parent
0cd9bdcf9f
commit
da3274d1b3
2 changed files with 24 additions and 2 deletions
|
|
@ -214,9 +214,18 @@ proc asyncSingleProc(prc: NimNode): NimNode =
|
||||||
# don't do anything with forward bodies (empty)
|
# don't do anything with forward bodies (empty)
|
||||||
if procBody.kind != nnkEmpty:
|
if procBody.kind != nnkEmpty:
|
||||||
# fix #13899, defer should not escape its original scope
|
# fix #13899, defer should not escape its original scope
|
||||||
procBody = newStmtList(newTree(nnkBlockStmt, newEmptyNode(), procBody))
|
let blockStmt = newStmtList(newTree(nnkBlockStmt, newEmptyNode(), procBody))
|
||||||
procBody.add(createFutureVarCompletions(futureVarIdents, nil))
|
procBody = newStmtList()
|
||||||
let resultIdent = ident"result"
|
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:
|
procBody.insert(0): quote do:
|
||||||
{.push warning[resultshadowed]: off.}
|
{.push warning[resultshadowed]: off.}
|
||||||
when `subRetType` isnot void:
|
when `subRetType` isnot void:
|
||||||
|
|
|
||||||
13
tests/async/t11558.nim
Normal file
13
tests/async/t11558.nim
Normal file
|
|
@ -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()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue