Skip to content

Commit 3b14fca

Browse files
committed
Also hoist lifted arguments in super calls
Super calls with default and named parameters can have lifted arguments in val defs preceding the constructor call. If these are complex, we need to hoist them out as well. Fixes scala#14164
1 parent d673b97 commit 3b14fca

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,23 @@ class HoistSuperArgs extends MiniPhase with IdentityDenotTransformer { thisPhase
173173
}
174174
}
175175

176+
/** Hoist super arg from a lifted parameter that gets evaluated before the call */
177+
def hoistSuperArgFromDef(paramDef: Tree, cdef: DefDef): Tree = paramDef match
178+
case vdef: ValDef =>
179+
cpy.ValDef(vdef)(rhs = hoistSuperArg(vdef.rhs, cdef))
180+
case ddef: DefDef =>
181+
cpy.DefDef(ddef)(rhs = hoistSuperArg(ddef.rhs, cdef))
182+
case _ =>
183+
paramDef
184+
176185
/** Hoist complex arguments in super call out of the class. */
177-
def hoistSuperArgsFromCall(superCall: Tree, cdef: DefDef): Tree = superCall match {
186+
def hoistSuperArgsFromCall(superCall: Tree, cdef: DefDef): Tree = superCall match
187+
case Block(defs, expr) =>
188+
cpy.Block(superCall)(defs.mapconserve(hoistSuperArgFromDef(_, cdef)), hoistSuperArgsFromCall(expr, cdef))
178189
case Apply(fn, args) =>
179190
cpy.Apply(superCall)(hoistSuperArgsFromCall(fn, cdef), args.mapconserve(hoistSuperArg(_, cdef)))
180191
case _ =>
181192
superCall
182-
}
183193

184194
/** Hoist complex arguments in this-constructor call of secondary constructor out of the class. */
185195
def hoistSuperArgsFromConstr(stat: Tree): Tree = stat match {

tests/run/i14164.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object Test:
2+
class Base(a: String = "x", param: String)
3+
4+
class Child extends Base(
5+
param =
6+
for x <- Seq("a") yield x
7+
"param"
8+
)
9+
10+
new Child
11+
12+
def main(args: Array[String]) = ()
13+

0 commit comments

Comments
 (0)