Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/main/scala/scala/async/internal/AsyncAnalysis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 initializer")
case CaseDef(_, guard, _) if guard exists isAwait =>
// TODO lift this restriction
reportUnsupportedAwait(tree, "pattern guard")
Expand Down
11 changes: 9 additions & 2 deletions src/main/scala/scala/async/internal/AsyncTransform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ trait AsyncTransform {
val template = Template(List(tryToUnit, typeOf[() => Unit]).map(TypeTree(_)), emptyValDef, body)

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
Expand Down Expand Up @@ -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]
}
}
4 changes: 2 additions & 2 deletions src/test/scala/scala/async/neg/NakedAwait.scala
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ class NakedAwait {

@Test
def lazyValIllegal() {
expectError("lazy vals are illegal") {
expectError("await must not be used under a lazy val initializer") {
"""
| import _root_.scala.async.internal.AsyncId._
| def foo(): Any = async { val x = { lazy val y = 0; y } }
| def foo(): Any = async { val x = { lazy val y = await(0); y } }
| ()
|
|""".stripMargin
Expand Down
29 changes: 29 additions & 0 deletions src/test/scala/scala/async/run/lazyval/LazyValSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
*/

package scala.async
package run
package lazyval

import org.junit.Test
import scala.async.internal.AsyncId._

class LazyValSpec {
@Test
def lazyValAllowed() {
val result = async {
var x = 0
lazy val y = { x += 1; 42 }
assert(x == 0, x)
val z = await(1)
val result = y + x
assert(x == 1, x)
identity(y)
assert(x == 1, x)
result
}
result mustBe 43
}
}