Skip to content

Commit 9b3bbe4

Browse files
committed
Merge pull request #69 from retronym/backport/ticket/52-lazy-val
[backport] Allow lazy vals without await in the initializer
2 parents 6808ce4 + cf19f02 commit 9b3bbe4

File tree

4 files changed

+39
-7
lines changed

4 files changed

+39
-7
lines changed

src/main/scala/scala/async/internal/AsyncAnalysis.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,8 @@ trait AsyncAnalysis {
6060
super.traverse(tree)
6161
case Return(_) =>
6262
abort(tree.pos, "return is illegal within a async block")
63-
case ValDef(mods, _, _, _) if mods.hasFlag(Flag.LAZY) =>
64-
// TODO lift this restriction
65-
abort(tree.pos, "lazy vals are illegal within an async block")
63+
case DefDef(mods, _, _, _, _, _) if mods.hasFlag(Flag.LAZY) && containsAwait =>
64+
reportUnsupportedAwait(tree, "lazy val initializer")
6665
case CaseDef(_, guard, _) if guard exists isAwait =>
6766
// TODO lift this restriction
6867
reportUnsupportedAwait(tree, "pattern guard")

src/main/scala/scala/async/internal/AsyncTransform.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@ trait AsyncTransform {
5555
val template = Template(List(tryToUnit, typeOf[() => Unit]).map(TypeTree(_)), emptyValDef, body)
5656

5757
val t = ClassDef(NoMods, name.stateMachineT, Nil, template)
58-
callSiteTyper.typedPos(macroPos)(Block(t :: Nil, Literal(Constant(()))))
59-
t
58+
typecheckClassDef(t)
6059
}
6160

6261
val stateMachineClass = stateMachine.symbol
@@ -218,4 +217,9 @@ trait AsyncTransform {
218217
}
219218
result
220219
}
220+
221+
def typecheckClassDef(cd: ClassDef): ClassDef = {
222+
val Block(cd1 :: Nil, _) = callSiteTyper.typedPos(macroPos)(Block(cd :: Nil, Literal(Constant(()))))
223+
cd1.asInstanceOf[ClassDef]
224+
}
221225
}

src/test/scala/scala/async/neg/NakedAwait.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ class NakedAwait {
163163

164164
@Test
165165
def lazyValIllegal() {
166-
expectError("lazy vals are illegal") {
166+
expectError("await must not be used under a lazy val initializer") {
167167
"""
168168
| import _root_.scala.async.internal.AsyncId._
169-
| def foo(): Any = async { val x = { lazy val y = 0; y } }
169+
| def foo(): Any = async { val x = { lazy val y = await(0); y } }
170170
| ()
171171
|
172172
|""".stripMargin
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright (C) 2012-2014 Typesafe Inc. <http://www.typesafe.com>
3+
*/
4+
5+
package scala.async
6+
package run
7+
package lazyval
8+
9+
import org.junit.Test
10+
import scala.async.internal.AsyncId._
11+
12+
class LazyValSpec {
13+
@Test
14+
def lazyValAllowed() {
15+
val result = async {
16+
var x = 0
17+
lazy val y = { x += 1; 42 }
18+
assert(x == 0, x)
19+
val z = await(1)
20+
val result = y + x
21+
assert(x == 1, x)
22+
identity(y)
23+
assert(x == 1, x)
24+
result
25+
}
26+
result mustBe 43
27+
}
28+
}
29+

0 commit comments

Comments
 (0)