Skip to content

Fixes #1856: mimic Scalac sematic of recursive lazy vals. #1892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
18 changes: 16 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/LazyVals.scala
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,22 @@ class LazyVals extends MiniPhaseTransform with IdentityDenotTransformer {

def mkNonThreadSafeDef(target: Tree, flag: Tree, rhs: Tree)(implicit ctx: Context) = {
val setFlag = flag.becomes(Literal(Constants.Constant(true)))
val setTargets = if (isWildcardArg(rhs)) Nil else target.becomes(rhs) :: Nil
val init = Block(setFlag :: setTargets, target.ensureApplied)
val flagSet = setFlag :: Nil

val init = evalOnce(rhs) { rhsRef =>
val targetSet = if (isWildcardArg(rhs)) flagSet else target.becomes(rhsRef) :: flagSet
val checkValidity =
tpd.If(
target.select(defn.Any_!=).appliedTo(tpd.defaultValue(target.tpe.widenDealias)).select(defn.Boolean_&&).
appliedTo(
target.select(defn.Any_!=).appliedTo(rhsRef)
),
tpd.Throw(tpd.New(ctx.requiredClass("java.lang.IllegalStateException").namedType, Nil)),
tpd.EmptyTree
)
Block(checkValidity :: targetSet, rhsRef)
}

If(flag.ensureApplied, target.ensureApplied, init)
}

Expand Down
11 changes: 11 additions & 0 deletions tests/run/i1856.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Iteration 6
Iteration 7
Iteration 8
Iteration 9
42
14 changes: 14 additions & 0 deletions tests/run/i1856.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
object Test {
var count: Int = 0
lazy val lzy: Int = {
if (count < 10) {
println(s"Iteration $count")
count += 1
lzy
} else 42
}

def main(args: Array[String]): Unit = {
println(lzy)
}
}
22 changes: 22 additions & 0 deletions tests/run/i1856b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Iteration 0
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Iteration 6
Iteration 7
Iteration 8
Iteration 9
Lzy has been read as: 42
Lzy has been read as: 43
Lzy has been read as: 44
Lzy has been read as: 45
Lzy has been read as: 46
Lzy has been read as: 47
Lzy has been read as: 48
Lzy has been read as: 49
Lzy has been read as: 50
Lzy has been read as: 51
52
Lzy has been read as: 52
21 changes: 21 additions & 0 deletions tests/run/i1856b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
object Test {
var count: Int = 0
def getLzy = {
val read = lzy
println("Lzy has been read as: " +read)
read
}

lazy val lzy: Int = {
if (count < 10) {
println(s"Iteration $count")
count += 1
getLzy + 1
} else 42
}

def main(args: Array[String]): Unit = {
println(lzy)
getLzy
}
}