Skip to content

Commit 1db6a20

Browse files
committed
Check path of module prefix for tailrec
1 parent 2660b09 commit 1db6a20

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

compiler/src/dotty/tools/dotc/transform/TailRec.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,10 @@ class TailRec extends MiniPhase {
346346
case prefix: This if prefix.symbol == enclosingClass =>
347347
// Avoid assigning `this = this`
348348
assignParamPairs
349-
case prefix if prefix.symbol.is(Module) && prefix.symbol.moduleClass == enclosingClass =>
349+
case prefix
350+
if prefix.symbol.is(Module)
351+
&& prefix.symbol.moduleClass == enclosingClass
352+
&& isPurePath(prefix) =>
350353
// Avoid assigning `this = MyObject`
351354
assignParamPairs
352355
case _ =>

tests/run/i23444.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
import annotation.*
3+
4+
class Path(action: () => Unit, parent: Option[Path]):
5+
object O:
6+
@tailrec
7+
def apply(): Unit =
8+
action()
9+
10+
parent match
11+
case Some(p) =>
12+
p.O.apply()
13+
case None =>
14+
15+
@main def Test: Unit =
16+
var counter = 0
17+
val fun = () => {
18+
counter += 1
19+
if counter > 2 then throw AssertionError("bad loop")
20+
}
21+
val path = Path(fun, Some(Path(fun, None)))
22+
path.O()

0 commit comments

Comments
 (0)