aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/scala/async
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2014-03-27 14:39:39 +0100
committerJason Zaugg <jzaugg@gmail.com>2014-03-27 14:53:28 +0100
commitf38a2f78243b4cc481d7150e78adef2b2f8afc8d (patch)
tree2548b81364d77bb97583c99b2c06b520c44c8ad0 /src/main/scala/scala/async
parentbdb1686c00a7fc9304c91eb5418a83529ab935fc (diff)
downloadscala-async-f38a2f78243b4cc481d7150e78adef2b2f8afc8d.tar.gz
scala-async-f38a2f78243b4cc481d7150e78adef2b2f8afc8d.tar.bz2
scala-async-f38a2f78243b4cc481d7150e78adef2b2f8afc8d.zip
Allow lazy vals without await in the initializer
We were incorrectly typechecking the `ClassDef` of the state machine in the macro in a way that discarded the resulting trees, and only kept around the symbol. The led to the the macro engine retypechecking that node, which somehow led to duplicated lazy val initiaializer `DefDef`-s in the template, which manifest as a `VerifyError`. This commit: - rescues the typechecked `ClassDef` node from the eager typechecking by the macro - loosens the restriction on lazy vals in async blocks. They are still prohibited if they contain an await on the RHS - Adds a test that shows evalution is indeed lazy. Fixes #52
Diffstat (limited to 'src/main/scala/scala/async')
-rw-r--r--src/main/scala/scala/async/internal/AsyncAnalysis.scala5
-rw-r--r--src/main/scala/scala/async/internal/AsyncTransform.scala13
2 files changed, 12 insertions, 6 deletions
diff --git a/src/main/scala/scala/async/internal/AsyncAnalysis.scala b/src/main/scala/scala/async/internal/AsyncAnalysis.scala
index ffbc04d..7be6b43 100644
--- a/src/main/scala/scala/async/internal/AsyncAnalysis.scala
+++ b/src/main/scala/scala/async/internal/AsyncAnalysis.scala
@@ -60,9 +60,8 @@ trait AsyncAnalysis {
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 DefDef(mods, _, _, _, _, _) if mods.hasFlag(Flag.LAZY) && containsAwait =>
+ reportUnsupportedAwait(tree, "lazy val initalizer")
case CaseDef(_, guard, _) if guard exists isAwait =>
// TODO lift this restriction
reportUnsupportedAwait(tree, "pattern guard")
diff --git a/src/main/scala/scala/async/internal/AsyncTransform.scala b/src/main/scala/scala/async/internal/AsyncTransform.scala
index cf9dd1c..aae9dea 100644
--- a/src/main/scala/scala/async/internal/AsyncTransform.scala
+++ b/src/main/scala/scala/async/internal/AsyncTransform.scala
@@ -54,11 +54,10 @@ trait AsyncTransform {
}
val tryToUnit = appliedType(definitions.FunctionClass(1), futureSystemOps.tryType[Any], typeOf[Unit])
- val template = Template(List(tryToUnit, typeOf[() => Unit]).map(TypeTree(_)), emptyValDef, body)
+ val template = Template(List(tryToUnit, typeOf[() => Unit]).map(TypeTree(_)), emptyValDef, body).setType(NoType)
val t = ClassDef(NoMods, name.stateMachineT, Nil, template)
- typingTransform(atPos(macroPos)(Block(t :: Nil, Literal(Constant(())))))((tree, api) => api.typecheck(tree))
- t
+ typecheckClassDef(t)
}
val stateMachineClass = stateMachine.symbol
@@ -211,4 +210,12 @@ trait AsyncTransform {
}
result
}
+
+ def typecheckClassDef(cd: ClassDef): ClassDef = {
+ val Block(cd1 :: Nil, _) = typingTransform(atPos(macroPos)(Block(cd :: Nil, Literal(Constant(())))))(
+ (tree, api) =>
+ api.typecheck(tree)
+ )
+ cd1.asInstanceOf[ClassDef]
+ }
}