Skip to content

Commit 08a0f29

Browse files
KacperFKorbantgodzik
authored andcommitted
fix: record calls to constructors in lambdaLift
[Cherry-picked 5dddf7e]
1 parent a76abab commit 08a0f29

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ abstract class Dependencies(root: ast.tpd.Tree, @constructorOnly rootContext: Co
136136
if !enclosure.exists then throw NoPath()
137137
if enclosure == sym.enclosure then NoSymbol
138138
else
139+
/** is sym a constructor or a term that is nested in a constructor? */
139140
def nestedInConstructor(sym: Symbol): Boolean =
140141
sym.isConstructor
141142
|| sym.isTerm && nestedInConstructor(sym.enclosure)
@@ -236,6 +237,10 @@ abstract class Dependencies(root: ast.tpd.Tree, @constructorOnly rootContext: Co
236237
captureImplicitThis(tree.tpe)
237238
case tree: Select =>
238239
if isExpr(sym) && isLocal(sym) then markCalled(sym, enclosure)
240+
case tree: New =>
241+
val constr = tree.tpe.typeSymbol.primaryConstructor
242+
if isExpr(constr) then
243+
symSet(called, enclosure) += constr
239244
case tree: This =>
240245
narrowTo(tree.symbol.asClass)
241246
case tree: MemberDef if isExpr(sym) && sym.owner.isTerm =>
@@ -290,7 +295,6 @@ abstract class Dependencies(root: ast.tpd.Tree, @constructorOnly rootContext: Co
290295
val calleeOwner = normalizedCallee.owner
291296
if calleeOwner.isTerm then narrowLogicOwner(caller, logicOwner(normalizedCallee))
292297
else
293-
assert(calleeOwner.is(Trait))
294298
// methods nested inside local trait methods cannot be lifted out
295299
// beyond the trait. Note that we can also call a trait method through
296300
// a qualifier; in that case no restriction to lifted owner arises.

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,7 @@ object LambdaLift:
127127

128128
private def proxy(sym: Symbol)(using Context): Symbol = {
129129
def liftedEnclosure(sym: Symbol) =
130-
if sym.is(Method)
131-
then deps.logicalOwner.getOrElse(sym, sym.enclosure)
132-
else sym.enclosure
130+
deps.logicalOwner.getOrElse(sym, sym.enclosure)
133131
def searchIn(enclosure: Symbol): Symbol = {
134132
if (!enclosure.exists) {
135133
def enclosures(encl: Symbol): List[Symbol] =

tests/pos/i21931.scala

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
def f() =
2-
val NotFound: Char = 'a'
3-
class crashing() {
4-
class issue() {
5-
NotFound
6-
}
7-
class Module() {
8-
val obligatory =
9-
class anonIssue() {
10-
issue()
1+
object Test {
2+
def f() = {
3+
val NotFound: Char = 'a'
4+
class crashing() {
5+
class issue() {
6+
NotFound
7+
}
8+
class Module() {
9+
val obligatory = {
10+
def anonIssue = {
11+
issue()
12+
}
13+
anonIssue
1114
}
15+
}
1216
}
1317
}
18+
}

tests/pos/i22470.scala

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
trait A
2+
trait OuterClass
3+
trait MidClass
4+
trait InnerClass
5+
6+
object Obj:
7+
def outerDef(a: A) =
8+
new OuterClass {
9+
def midDef(): Unit = {
10+
new MidClass {
11+
val valdef = new InnerClass {
12+
def innerDef() =
13+
println(a)
14+
}
15+
}
16+
}
17+
}

0 commit comments

Comments
 (0)