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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/
package scala.async.internal
import scala.reflect.macros.Context
import scala.collection.mutable
trait AsyncAnalysis {
self: AsyncMacro =>
import c.universe._
/**
* Analyze the contents of an `async` block in order to:
* - Report unsupported `await` calls under nested templates, functions, by-name arguments.
*
* Must be called on the original tree, not on the ANF transformed tree.
*/
def reportUnsupportedAwaits(tree: Tree): Unit = {
val analyzer = new UnsupportedAwaitAnalyzer
analyzer.traverse(tree)
// analyzer.hasUnsupportedAwaits // XB: not used?!
}
private class UnsupportedAwaitAnalyzer extends AsyncTraverser {
var hasUnsupportedAwaits = false
override def nestedClass(classDef: ClassDef) {
val kind = if (classDef.symbol.isTrait) "trait" else "class"
reportUnsupportedAwait(classDef, s"nested ${kind}")
}
override def nestedModule(module: ModuleDef) {
reportUnsupportedAwait(module, "nested object")
}
override def nestedMethod(defDef: DefDef) {
reportUnsupportedAwait(defDef, "nested method")
}
override def byNameArgument(arg: Tree) {
reportUnsupportedAwait(arg, "by-name argument")
}
override def function(function: Function) {
reportUnsupportedAwait(function, "nested function")
}
override def patMatFunction(tree: Match) {
reportUnsupportedAwait(tree, "nested function")
}
override def traverse(tree: Tree) {
def containsAwait = tree exists isAwait
tree match {
case Try(_, _, _) if containsAwait =>
reportUnsupportedAwait(tree, "try/catch")
super.traverse(tree)
case Return(_) =>
c.abort(tree.pos, "return is illegal within a async block")
case ValDef(mods, _, _, _) if mods.hasFlag(Flag.LAZY) =>
// TODO lift this restriction
c.abort(tree.pos, "lazy vals are illegal within an async block")
case CaseDef(_, guard, _) if guard exists isAwait =>
// TODO lift this restriction
reportUnsupportedAwait(tree, "pattern guard")
case _ =>
super.traverse(tree)
}
}
/**
* @return true, if the tree contained an unsupported await.
*/
private def reportUnsupportedAwait(tree: Tree, whyUnsupported: String): Boolean = {
val badAwaits: List[RefTree] = tree collect {
case rt: RefTree if isAwait(rt) => rt
}
badAwaits foreach {
tree =>
reportError(tree.pos, s"await must not be used under a $whyUnsupported.")
}
badAwaits.nonEmpty
}
private def reportError(pos: Position, msg: String) {
hasUnsupportedAwaits = true
c.abort(pos, msg)
}
}
}
|