Skip to content

Commit f00803d

Browse files
Backport "Fix EnclosingMethod for lifted anonfun" to LTS (#21141)
Backports #20558 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents babbe7e + b7b0d9b commit f00803d

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

Diff for: compiler/src/dotty/tools/backend/jvm/BCodeAsmCommon.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ final class BCodeAsmCommon[I <: DottyBackendInterface](val interface: I) {
6060
assert(classSym.isClass, classSym)
6161
def enclosingMethod(sym: Symbol): Option[Symbol] = {
6262
if (sym.isClass || sym == NoSymbol) None
63-
else if (sym.is(Method)) Some(sym)
63+
else if (sym.is(Method, butNot=Synthetic)) Some(sym)
6464
else enclosingMethod(sym.originalOwner)
6565
}
6666
enclosingMethod(classSym.originalOwner)

Diff for: tests/run/i18701.check

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public TB A$$anon$1.tb()

Diff for: tests/run/i18701.fixed.check

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public TB A$$anon$2.apply()

Diff for: tests/run/i18701.fixed.scala

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// scalajs: --skip
2+
// Use of Java reflection (getEnclosingMethod)
3+
abstract class TA { def tb(): TB }
4+
abstract class TB { def chk(): Unit }
5+
class A:
6+
def a(): TA =
7+
new TA {
8+
def tb(): TB =
9+
val fn: () => TB = new Function0[TB]:
10+
def apply(): TB = new TB {
11+
def chk() = println(getClass.getEnclosingMethod())
12+
}
13+
fn()
14+
}
15+
16+
object Test:
17+
def main(args: Array[String]): Unit = new A().a().tb().chk()

Diff for: tests/run/i18701.scala

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// scalajs: --skip
2+
// Use of Java reflection (getEnclosingMethod)
3+
abstract class TA { def tb(): TB }
4+
abstract class TB { def chk(): Unit }
5+
class A:
6+
def a(): TA =
7+
new TA {
8+
def tb(): TB =
9+
val fn: () => TB = () => new TB {
10+
def chk() = println(getClass.getEnclosingMethod())
11+
}
12+
fn()
13+
}
14+
15+
object Test:
16+
def main(args: Array[String]): Unit = new A().a().tb().chk()

0 commit comments

Comments
 (0)