Skip to content

Commit 7e0bef5

Browse files
dwijnandWojciechMazur
authored andcommitted
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. [Cherry-picked 5c295f1]
1 parent 5489ead commit 7e0bef5

File tree

5 files changed

+32
-1
lines changed

5 files changed

+32
-1
lines changed

compiler/src/dotty/tools/backend/jvm/BCodeAsmCommon.scala

Lines changed: 1 addition & 1 deletion
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)

tests/run/i18701.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public TB A$$anon$1.tb()

tests/run/i18701.fixed.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public TB A$$anon$2.apply()

tests/run/i18701.fixed.scala

Lines changed: 15 additions & 0 deletions
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()

tests/run/i18701.scala

Lines changed: 14 additions & 0 deletions
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)