summary refs log tree commit diff stats
path: root/lib/pure/asyncmacro.nim
diff options
context:
space:
mode:
authorhuantian <davidtianli@gmail.com>2022-04-13 14:03:46 -0700
committerGitHub <noreply@github.com>2022-04-13 23:03:46 +0200
commitef7d7f24594b1ea560fbf25d3fa0d6c51122725f (patch)
tree15dc4511c81bc2a9ea366789d49a9f7862ed3d9f /lib/pure/asyncmacro.nim
parent98cebad7debddfb147ee22bc6f3d81221582c4d6 (diff)
downloadNim-ef7d7f24594b1ea560fbf25d3fa0d6c51122725f.tar.gz
Better error message and tests for bad await (#19622)
* Better error message and tests for bad await

* Use compiles to check if await is valid

* temp: disable windows noasync test

* Better error report, simplify test

Co-authored-by: flywind <xzsflywind@gmail.com>
Diffstat (limited to 'lib/pure/asyncmacro.nim')
-rw-r--r--lib/pure/asyncmacro.nim16
1 files changed, 13 insertions, 3 deletions
diff --git a/lib/pure/asyncmacro.nim b/lib/pure/asyncmacro.nim
index ce538913f..63c8e6e5c 100644
--- a/lib/pure/asyncmacro.nim
+++ b/lib/pure/asyncmacro.nim
@@ -126,9 +126,19 @@ template await*(f: typed): untyped {.used.} =
     error "await expects Future[T], got " & $typeof(f)
 
 template await*[T](f: Future[T]): auto {.used.} =
-  var internalTmpFuture: FutureBase = f
-  yield internalTmpFuture
-  (cast[typeof(f)](internalTmpFuture)).read()
+  template yieldFuture = yield FutureBase()
+
+  when compiles(yieldFuture):
+    var internalTmpFuture: FutureBase = f
+    yield internalTmpFuture
+    (cast[typeof(f)](internalTmpFuture)).read()
+  else:
+    macro errorAsync(futureError: Future[T]) =
+      error(
+        "Can only 'await' inside a proc marked as 'async'. Use " &
+        "'waitFor' when calling an 'async' proc in a non-async scope instead",
+        futureError)
+    errorAsync(f)
 
 proc asyncSingleProc(prc: NimNode): NimNode =
   ## This macro transforms a single procedure into a closure iterator.
eb.de> 2008-11-16 22:08:15 +0100 committer Andreas Rumpf <rumpf_a@web.de> 2008-11-16 22:08:15 +0100 version 0.7.0' href='/ahoang/Nim/commit/whiteutils.py?h=devel&id=8b2a9401a147bd0b26cd2976ae71a1022fbde8cc'>8b2a9401a ^
405b86068
















1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68