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 12, 2024
1 parent f7ab683 commit 5c295f1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/backend/jvm/BCodeAsmCommon.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ final class BCodeAsmCommon[I <: DottyBackendInterface](val interface: I) {
assert(classSym.isClass, classSym)
def enclosingMethod(sym: Symbol): Option[Symbol] = {
if (sym.isClass || sym == NoSymbol) None
else if (sym.is(Method)) Some(sym)
else if (sym.is(Method, butNot=Synthetic)) Some(sym)
else enclosingMethod(sym.originalOwner)
}
enclosingMethod(classSym.originalOwner)
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 TB A$$anon$1.tb()
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 5c295f1

Please sign in to comment.