Skip to content

Commit 021bfe1

Browse files
committed
Avoid using changeOwner in ByNameLambda
The regular changeOwner can cause cycles in transformations. Transformations like ByNameLambda should use the less demanding changeOwnerAfter.
1 parent 576fed5 commit 021bfe1

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ package transform
44

55
import core._
66
import Flags._
7-
import MegaPhase._
7+
import MegaPhase._, DenotTransformers.IdentityDenotTransformer
88
import Symbols._, Contexts._, Types._, Decorators._
99
import StdNames.nme
1010
import ast.Trees._
1111
import ast.TreeTypeMap
1212

1313
/** Rewrite applications `<byname>(...)` to context closures `() ?=> ...` */
14-
class ByNameLambda extends MiniPhase:
14+
class ByNameLambda extends MiniPhase, IdentityDenotTransformer:
15+
thisPhase =>
16+
1517
import ast.tpd._
1618

1719
def phaseName: String = ByNameLambda.name
@@ -22,18 +24,18 @@ class ByNameLambda extends MiniPhase:
2224
case Apply(Select(fn, nme.apply), Nil) if isPurePath(fn) && fn.tpe.widen.isByName =>
2325
fn
2426
case _ =>
25-
ByNameLambda(body)
27+
byNameClosure(body)
2628
case _ => app
2729

30+
def byNameClosure(body: Tree)(using Context): Block =
31+
val restpe = body.tpe.widenIfUnstable.deskolemized
32+
val meth = newAnonFun(ctx.owner, MethodType(Nil, restpe), coord = body.span)
33+
Closure(meth, _ => body.changeOwnerAfter(ctx.owner, meth, thisPhase),
34+
targetType = defn.ContextFunction0.typeRef.appliedTo(restpe))
35+
2836
object ByNameLambda:
2937
import ast.tpd._
3038

3139
val name = "byNameLambda"
3240

33-
def apply(body: Tree)(using Context): Block =
34-
val restpe = body.tpe.widenIfUnstable.deskolemized
35-
val meth = newAnonFun(ctx.owner, MethodType(Nil, restpe), coord = body.span)
36-
Closure(meth, _ => body.changeOwner(ctx.owner, meth),
37-
targetType = defn.ContextFunction0.typeRef.appliedTo(restpe))
38-
3941
end ByNameLambda

0 commit comments

Comments
 (0)