Skip to content

Commit 18df09c

Browse files
authored
Desugaring: avoid patdef optimization for lazy vals (#26770)
Fixes #26769 My bad. ## Have you relied on LLM-based tools in this contribution? No ## How was the solution tested? New automated tests (including the issue's reproducer, if applicable)
1 parent ee44579 commit 18df09c

7 files changed

Lines changed: 44 additions & 1 deletion

File tree

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1616,11 +1616,12 @@ object desugar {
16161616
case _ =>
16171617
// ... except we have one more optimization up our sleeve:
16181618
// if we're in the "simple tuple" case and the RHS is also a tuple, for `val (a, b) = (1, 2)` we can emit `val a = 1; val b = 2`.
1619-
// We don't do this if there are any types or wildcards involved, since those can require conversions or handling side-effects.
1619+
// We don't do this if there is laziness or any types or wildcards involved; the latter can require conversions or handling side effects.
16201620
val (firstDef, splitRhs) = rhs match
16211621
case TuplePattern(elems, TypeTree()) if opt == Optimization.SimpleTuple
16221622
&& elems.size == variables.size
16231623
&& !pat.isInstanceOf[Typed]
1624+
&& !mods.is(Lazy)
16241625
&& variables.forall((n, _) => n.name != nme.WILDCARD) =>
16251626
(Nil, elems)
16261627
case _ =>

tests/run/lazy-val-pattern-2.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
a
2+
c
3+
b
4+
1
5+
2
6+
1
7+
2

tests/run/lazy-val-pattern-2.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Test:
2+
lazy val (a, _, b) = ({println("a"); 1}, {println("c"); 3}, {println("b"); 2})
3+
4+
def main(args: Array[String]): Unit =
5+
println(a)
6+
println(b)
7+
println(a)
8+
println(b)

tests/run/lazy-val-pattern-3.check

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
a
2+
1
3+
2
4+
1
5+
2

tests/run/lazy-val-pattern-3.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Test:
2+
lazy val (a, b) = {println("a"); (1, 2)}
3+
4+
def main(args: Array[String]): Unit =
5+
println(a)
6+
println(b)
7+
println(a)
8+
println(b)

tests/run/lazy-val-pattern.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
a
2+
b
3+
1
4+
2
5+
1
6+
2

tests/run/lazy-val-pattern.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
object Test:
2+
lazy val (a, b) = ({println("a"); 1}, {println("b"); 2})
3+
4+
def main(args: Array[String]): Unit =
5+
println(a)
6+
println(b)
7+
println(a)
8+
println(b)

0 commit comments

Comments
 (0)