Skip to content

Commit 26dddca

Browse files
committed
Fix EnclosingMethod for lifted anonfun
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.
1 parent f7ab683 commit 26dddca

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ final class BCodeAsmCommon[I <: DottyBackendInterface](val interface: I) {
7676
if (sym.isClass) sym
7777
else enclosingClass(sym.originalOwner.originalLexicallyEnclosingClass)
7878
}
79-
enclosingClass(classSym.originalOwner.originalLexicallyEnclosingClass)
79+
enclosingMethodForEnclosingMethodAttribute(classSym) match
80+
case Some(methodSym) => methodSym.skipLocalOwners.owner
81+
case _ => enclosingClass(classSym.originalOwner.originalLexicallyEnclosingClass)
8082
}
8183

8284
/*final*/ case class EnclosingMethodEntry(owner: String, name: String, methodDescriptor: String)

Diff for: tests/run/i18701.check

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public static final TB A.A$$anon$1$$_$_$$anonfun$1()

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

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

Diff for: tests/run/i18701.scala

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

0 commit comments

Comments
 (0)