Skip to content

Commit

Permalink
Fix EnclosingMethod for lifted anonfun (#20558)
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.

Fixes #18701
  • Loading branch information
lrytz authored Jun 17, 2024
2 parents 48a823f + 0c20d86 commit 4d391dc
Show file tree
Hide file tree
Showing 5 changed files with 36 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()
17 changes: 17 additions & 0 deletions tests/run/i18701.fixed.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// scalajs: --skip
// Use of Java reflection (getEnclosingMethod)
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()
16 changes: 16 additions & 0 deletions tests/run/i18701.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// scalajs: --skip
// Use of Java reflection (getEnclosingMethod)
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 4d391dc

Please sign in to comment.