Skip to content

Commit

Permalink
Fix EnclosingMethod for lifted anonfun
Browse files Browse the repository at this point in the history
The anonfun "() => new TB {.." code is lifted to a static method, in the
original class (A), but the GenBCode logic was still returning the TA
anon class.
  • Loading branch information
dwijnand committed Jun 11, 2024
1 parent f7ab683 commit 26dddca
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/backend/jvm/BCodeAsmCommon.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ final class BCodeAsmCommon[I <: DottyBackendInterface](val interface: I) {
if (sym.isClass) sym
else enclosingClass(sym.originalOwner.originalLexicallyEnclosingClass)
}
enclosingClass(classSym.originalOwner.originalLexicallyEnclosingClass)
enclosingMethodForEnclosingMethodAttribute(classSym) match
case Some(methodSym) => methodSym.skipLocalOwners.owner
case _ => enclosingClass(classSym.originalOwner.originalLexicallyEnclosingClass)
}

/*final*/ case class EnclosingMethodEntry(owner: String, name: String, methodDescriptor: String)
Expand Down
1 change: 1 addition & 0 deletions tests/run/i18701.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public static final TB A.A$$anon$1$$_$_$$anonfun$1()
1 change: 1 addition & 0 deletions tests/run/i18701.fixed.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public TB A$$anon$2.apply()
15 changes: 15 additions & 0 deletions tests/run/i18701.fixed.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
abstract class TA { def tb(): TB }
abstract class TB { def chk(): Unit }
class A:
def a(): TA =
new TA {
def tb(): TB =
val fn: () => TB = new Function0[TB]:
def apply(): TB = new TB {
def chk() = println(getClass.getEnclosingMethod())
}
fn()
}

object Test:
def main(args: Array[String]): Unit = new A().a().tb().chk()
14 changes: 14 additions & 0 deletions tests/run/i18701.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
abstract class TA { def tb(): TB }
abstract class TB { def chk(): Unit }
class A:
def a(): TA =
new TA {
def tb(): TB =
val fn: () => TB = () => new TB {
def chk() = println(getClass.getEnclosingMethod())
}
fn()
}

object Test:
def main(args: Array[String]): Unit = new A().a().tb().chk()

0 comments on commit 26dddca

Please sign in to comment.