Skip to content

Commit 5a49226

Browse files
committed
Merge pull request #67 from retronym/ticket/52-lazy-val
Allow lazy vals without await in the initializer
2 parents bdb1686 + 5c6ea29 commit 5a49226

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-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
c.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-
c.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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ trait AsyncTransform {
5757
val template = Template(List(tryToUnit, typeOf[() => Unit]).map(TypeTree(_)), emptyValDef, body)
5858

5959
val t = ClassDef(NoMods, name.stateMachineT, Nil, template)
60-
typingTransform(atPos(macroPos)(Block(t :: Nil, Literal(Constant(())))))((tree, api) => api.typecheck(tree))
61-
t
60+
typecheckClassDef(t)
6261
}
6362

6463
val stateMachineClass = stateMachine.symbol
@@ -211,4 +210,12 @@ trait AsyncTransform {
211210
}
212211
result
213212
}
213+
214+
def typecheckClassDef(cd: ClassDef): ClassDef = {
215+
val Block(cd1 :: Nil, _) = typingTransform(atPos(macroPos)(Block(cd :: Nil, Literal(Constant(())))))(
216+
(tree, api) =>
217+
api.typecheck(tree)
218+
)
219+
cd1.asInstanceOf[ClassDef]
220+
}
214221
}

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)