77# distribution, for details about the copyright.
88#
99
10- import std/ [macros]
10+ import
11+ std/ [macros]
1112
1213proc skipUntilStmtList (node: NimNode ): NimNode {.compileTime .} =
1314 # Skips a nest of StmtList's.
@@ -131,6 +132,12 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
131132 cast [`internalFutureType`](`internalFutureSym`)
132133 procBody = prc.body.processBody (castFutureSym, baseTypeIsVoid)
133134
135+ # Support `let x = ? await ...`
136+ assignResultSym = ident " assignResult?"
137+ assignResult = quote do :
138+ template `assignResultSym` (v: Result ) =
139+ `castFutureSym`.complete (v)
140+
134141 # don't do anything with forward bodies (empty)
135142 if procBody.kind != nnkEmpty:
136143 # fix #13899, `defer` should not escape its original scope
@@ -143,11 +150,12 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
143150 template result : auto {.used .} =
144151 {.fatal : " You should not reference the `result` variable inside" &
145152 " a void async proc" .}
153+
146154 # -> complete(chronosInternalRetFuture)
147155 let complete =
148156 newCall (newIdentNode (" complete" ), castFutureSym)
149157
150- newStmtList (resultTemplate, procBodyBlck, complete)
158+ newStmtList (resultTemplate, assignResult, procBodyBlck, complete)
151159 else :
152160 # -> iterator nameIter(chronosInternalRetFuture: Future[T]): FutureBase {.closure.} =
153161 # -> {.push warning[resultshadowed]: off.}
@@ -170,6 +178,8 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
170178 newNimNode (nnkPragma).add (
171179 newIdentNode (" pop" )),
172180
181+ assignResult,
182+
173183 procBodyBlck,
174184
175185 # -> complete(chronosInternalRetFuture, result)
@@ -311,5 +321,5 @@ macro async*(prc: untyped): untyped =
311321 result .add asyncSingleProc (oneProc)
312322 else :
313323 result = asyncSingleProc (prc)
314- when chronosDumpAsync:
324+ when true or chronosDumpAsync:
315325 echo repr result
0 commit comments