Skip to content

Commit

Permalink
Fix const in async regression (nim-lang#21898)
Browse files Browse the repository at this point in the history
* Add test case for a const being used inside an async proc

* Use `typeof` to get the type of the block instead of overloaded templates

This removes the problem with the symbol having different types

I am unsure why I didn't use this in the first place. IIRC I had problems with `typeof` when I first tried to use it in the original implementation
  • Loading branch information
ire4ever1190 authored May 25, 2023
1 parent b7925bf commit a8718d8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
12 changes: 5 additions & 7 deletions lib/pure/asyncmacro.nim
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,11 @@ proc asyncSingleProc(prc: NimNode): NimNode =
procBody = newStmtList()
let resultIdent = ident"result"
procBody.add quote do:
template nimAsyncDispatchSetResult(x: `subRetType`) {.used.} =
# If the proc has implicit return then this will get called
`resultIdent` = x
template nimAsyncDispatchSetResult(x: untyped) {.used.} =
# If the proc doesn't have implicit return then this will get called
x
procBody.add newCall(ident"nimAsyncDispatchSetResult", blockStmt)
# Check whether there is an implicit return
when typeof(`blockStmt`) is void:
`blockStmt`
else:
`resultIdent` = `blockStmt`
procBody.add(createFutureVarCompletions(futureVarIdents, nil))
procBody.insert(0): quote do:
{.push warning[resultshadowed]: off.}
Expand Down
13 changes: 13 additions & 0 deletions tests/async/t21893.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
discard """
output: "@[97]\ntrue"
"""

import asyncdispatch

proc test(): Future[bool] {.async.} =
const S4 = @[byte('a')]
echo S4
return true

echo waitFor test()

0 comments on commit a8718d8

Please sign in to comment.