Skip to content

Commit

Permalink
fix: record calls to constructors in lambdaLift (#22487)
Browse files Browse the repository at this point in the history
possible fix for #21931 #22470

Attempt no. 2 for #21931
  • Loading branch information
odersky authored Feb 18, 2025
2 parents da6c61c + 51c5c41 commit 417d542
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ abstract class Dependencies(root: ast.tpd.Tree, @constructorOnly rootContext: Co
if !enclosure.exists then throw NoPath()
if enclosure == sym.enclosure then NoSymbol
else
/** is sym a constructor or a term that is nested in a constructor? */
def nestedInConstructor(sym: Symbol): Boolean =
sym.isConstructor
|| sym.isTerm && nestedInConstructor(sym.enclosure)
Expand Down Expand Up @@ -237,6 +238,10 @@ abstract class Dependencies(root: ast.tpd.Tree, @constructorOnly rootContext: Co
captureImplicitThis(tree.tpe)
case tree: Select =>
if isExpr(sym) && isLocal(sym) then markCalled(sym, enclosure)
case tree: New =>
val constr = tree.tpe.typeSymbol.primaryConstructor
if constr.exists then
symSet(called, enclosure) += constr
case tree: This =>
narrowTo(tree.symbol.asClass)
case tree: MemberDef if isExpr(sym) && sym.owner.isTerm =>
Expand Down Expand Up @@ -291,7 +296,6 @@ abstract class Dependencies(root: ast.tpd.Tree, @constructorOnly rootContext: Co
val calleeOwner = normalizedCallee.owner
if calleeOwner.isTerm then narrowLogicOwner(caller, logicOwner(normalizedCallee))
else
assert(calleeOwner.is(Trait))
// methods nested inside local trait methods cannot be lifted out
// beyond the trait. Note that we can also call a trait method through
// a qualifier; in that case no restriction to lifted owner arises.
Expand Down
4 changes: 1 addition & 3 deletions compiler/src/dotty/tools/dotc/transform/LambdaLift.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,7 @@ object LambdaLift:

private def proxy(sym: Symbol)(using Context): Symbol = {
def liftedEnclosure(sym: Symbol) =
if sym.is(Method)
then deps.logicalOwner.getOrElse(sym, sym.enclosure)
else sym.enclosure
deps.logicalOwner.getOrElse(sym, sym.enclosure)
def searchIn(enclosure: Symbol): Symbol = {
if (!enclosure.exists) {
def enclosures(encl: Symbol): List[Symbol] =
Expand Down
25 changes: 15 additions & 10 deletions tests/pos/i21931.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
def f() =
val NotFound: Char = 'a'
class crashing() {
class issue() {
NotFound
}
class Module() {
val obligatory =
class anonIssue() {
issue()
object Test {
def f() = {
val NotFound: Char = 'a'
class crashing() {
class issue() {
NotFound
}
class Module() {
val obligatory = {
def anonIssue = {
issue()
}
anonIssue
}
}
}
}
}
17 changes: 17 additions & 0 deletions tests/pos/i22470.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
trait A
trait OuterClass
trait MidClass
trait InnerClass

object Obj:
def outerDef(a: A) =
new OuterClass {
def midDef(): Unit = {
new MidClass {
val valdef = new InnerClass {
def innerDef() =
println(a)
}
}
}
}

0 comments on commit 417d542

Please sign in to comment.