Skip to content

Commit

Permalink
Backport "Fix EnclosingMethod for lifted anonfun" to LTS (#21141)
Browse files Browse the repository at this point in the history
Backports #20558 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jul 10, 2024
2 parents babbe7e + b7b0d9b commit f00803d
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 f00803d

Please sign in to comment.