Skip to content

Commit e4145ec

Browse files
committed
better result injection
1 parent c2d9417 commit e4145ec

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

chronos/asyncmacro2.nim

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
113113
raiseAssert("Unhandled async return type: " & $prc.kind)
114114

115115
let subtypeIsVoid = baseType.eqIdent("void")
116+
let returnTypeIsResult =
117+
returnType.kind == nnkBracketExpr and baseType.kind == nnkBracketExpr and
118+
(baseType[0].eqIdent("Result") or baseType[0].eqIdent("results.Result"))
116119

117120
if prc.kind in {nnkProcDef, nnkLambda, nnkMethodDef, nnkDo}:
118121
let
@@ -147,11 +150,12 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
147150
# fix #13899, `defer` should not escape its original scope
148151
procBody = newStmtList(newTree(nnkBlockStmt, newEmptyNode(), procBody))
149152

150-
# Support `let x = ? await ...`
151-
let ar = ident "assignResult?"
152-
procBody.insert 0, quote do:
153-
template `ar`(v: Result) =
154-
`internalFutureSym`.complete(v)
153+
if returnTypeIsResult:
154+
# Support `let x = ? await ...`, but only if `Result` exists as a symbol
155+
let ar = ident "assignResult?"
156+
procBody.insert 0, quote do:
157+
template `ar`(v: Result) =
158+
`internalFutureSym`.complete(v)
155159

156160
if not subtypeIsVoid:
157161
procBody.insert(0, newNimNode(nnkPragma).add(newIdentNode("push"),

0 commit comments

Comments
 (0)