From 2346ed3c6c3775e340c13f17664b7892033dbbbe Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Thu, 15 Aug 2019 18:35:42 +0200 Subject: [PATCH] Fixup error reporting --- chronos/asyncmacro2.nim | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/chronos/asyncmacro2.nim b/chronos/asyncmacro2.nim index b970e4df..8207da82 100644 --- a/chronos/asyncmacro2.nim +++ b/chronos/asyncmacro2.nim @@ -29,7 +29,7 @@ template createCb(retFutureSym, iteratorNameSym, bind finished var nameIterVar = iteratorNameSym - #{.push stackTrace: off.} + {.push stackTrace: off.} proc identName(udata: pointer = nil) {.closure.} = try: if not(nameIterVar.finished()): @@ -63,7 +63,7 @@ template createCb(retFutureSym, iteratorNameSym, retFutureSym.fail(getCurrentException()) identName() - #{.pop.} + {.pop.} proc createFutureVarCompletions(futureVarIdents: seq[NimNode], fromNode: NimNode): NimNode {.compileTime.} = @@ -151,6 +151,9 @@ proc verifyReturnType(typeName: string) {.compileTime.} = error("Expected return type of 'Future' got '$1'" % typeName) +macro unsupported(s: static[string]): untyped = + error s + proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} = ## This macro transforms a single procedure into a closure iterator. ## The ``async`` macro supports a stmtList holding multiple async procedures. @@ -190,9 +193,6 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} = else: baseType outerProcBody.add quote do: var `retFutureSym` = newFuture[`subRetType`](`prcName`) - template waitFor[T](f: Future[T]): T = - static: - assert(false, "waitFor can not be used within {.async.}") # -> iterator nameIter(): FutureBase {.closure.} = # -> {.push warning[resultshadowed]: off.} @@ -249,6 +249,8 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} = # -> return retFuture outerProcBody.add newNimNode(nnkReturnStmt, prc.body[^1]).add(retFutureSym) + prc.addPragma(newColonExpr(ident "stackTrace", ident "off")) + result = prc if subtypeIsVoid: @@ -264,8 +266,8 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} = template await*[T](f: Future[T]): auto = when declared(chronosInternalRetFuture): - when not declared(chronosInternalTmpFuture): - var chronosInternalTmpFuture: FutureBase + when not declaredInScope(chronosInternalTmpFuture): + var chronosInternalTmpFuture {.inject.}: FutureBase chronosInternalTmpFuture = f chronosInternalRetFuture.child = chronosInternalTmpFuture yield chronosInternalTmpFuture @@ -273,21 +275,19 @@ template await*[T](f: Future[T]): auto = chronosInternalRetFuture.child = nil cast[type(f)](chronosInternalTmpFuture).internalRead() else: - static: - assert(false, "Await only available within {.async.}") + unsupported "await is only available within {.async.}" template awaitne*[T](f: Future[T]): Future[T] = when declared(chronosInternalRetFuture): - when not declared(chronosInternalTmpFuture): - var chronosInternalTmpFuture: FutureBase + when not declaredInScope(chronosInternalTmpFuture): + var chronosInternalTmpFuture {.inject.}: FutureBase chronosInternalTmpFuture = f chronosInternalRetFuture.child = chronosInternalTmpFuture yield chronosInternalTmpFuture chronosInternalRetFuture.child = nil cast[type(f)](chronosInternalTmpFuture) else: - static: - assert(false, "Await only available within {.async.}") + unsupported "awaitne is only available within {.async.}" macro async*(prc: untyped): untyped = ## Macro which processes async procedures into the appropriate