diff options
author | Jake Leahy <jake@leahy.dev> | 2022-12-10 04:10:33 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-09 18:10:33 +0100 |
commit | da3274d1b3bb5cc737a5aca900dd231e4223fa60 (patch) | |
tree | 94d5778b374067c22f848577d07d3935a13cae05 | |
parent | 0cd9bdcf9f6802421e0d8e4c28fc732012af605e (diff) | |
download | Nim-da3274d1b3bb5cc737a5aca900dd231e4223fa60.tar.gz |
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>
-rw-r--r-- | lib/pure/asyncmacro.nim | 13 | ||||
-rw-r--r-- | tests/async/t11558.nim | 13 |
2 files changed, 24 insertions, 2 deletions
diff --git a/lib/pure/asyncmacro.nim b/lib/pure/asyncmacro.nim index 7ee324369..1f89b15e7 100644 --- a/lib/pure/asyncmacro.nim +++ b/lib/pure/asyncmacro.nim @@ -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: diff --git a/tests/async/t11558.nim b/tests/async/t11558.nim new file mode 100644 index 000000000..99961e7b6 --- /dev/null +++ b/tests/async/t11558.nim @@ -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() |