Fix Nim's issue #13899 using #14723 and add tests. (#104)

This commit is contained in:
Eugene Kabanov 2020-06-24 13:03:36 +03:00 committed by GitHub
parent 02b8da986b
commit 319e2bfc09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 1 deletions

View File

@ -1,5 +1,5 @@
packageName = "chronos"
version = "2.4.0"
version = "2.4.1"
author = "Status Research & Development GmbH"
description = "Chronos"
license = "Apache License 2.0 or MIT"

View File

@ -203,6 +203,9 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
futureVarIdents)
# 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))
if not subtypeIsVoid:

View File

@ -73,6 +73,31 @@ suite "Asynchronous issues test suite":
await sleepAsync(100.milliseconds)
result = (checkstr == "FooBarBaz")
proc testDefer(): Future[bool] {.async.} =
proc someConnect() {.async.} =
await sleepAsync(100.milliseconds)
proc someClose() {.async.} =
await sleepAsync(100.milliseconds)
proc testFooFails(): Future[bool] {.async.} =
await someConnect()
defer:
await someClose()
result = true
proc testFooSucceed(): Future[bool] {.async.} =
try:
await someConnect()
finally:
await someClose()
result = true
let r1 = await testFooFails()
let r2 = await testFooSucceed()
result = r1 and r2
test "Issue #6":
check waitFor(issue6()) == true
@ -84,3 +109,6 @@ suite "Asynchronous issues test suite":
test "Multiple await on single future test [Nim's issue #13889]":
check waitFor(testMultipleAwait()) == true
test "Defer for asynchronous procedures test [Nim's issue #13899]":
check waitFor(testDefer()) == true