From 5c295f1626c6361cb702fbe95bcf280cbc889990 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Tue, 11 Jun 2024 21:28:25 +0100 Subject: [PATCH 1/2] 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. --- .../dotty/tools/backend/jvm/BCodeAsmCommon.scala | 2 +- tests/run/i18701.check | 1 + tests/run/i18701.fixed.check | 1 + tests/run/i18701.fixed.scala | 15 +++++++++++++++ tests/run/i18701.scala | 14 ++++++++++++++ 5 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 tests/run/i18701.check create mode 100644 tests/run/i18701.fixed.check create mode 100644 tests/run/i18701.fixed.scala create mode 100644 tests/run/i18701.scala diff --git a/compiler/src/dotty/tools/backend/jvm/BCodeAsmCommon.scala b/compiler/src/dotty/tools/backend/jvm/BCodeAsmCommon.scala index 4027cf9fb564..e1ff94be6362 100644 --- a/compiler/src/dotty/tools/backend/jvm/BCodeAsmCommon.scala +++ b/compiler/src/dotty/tools/backend/jvm/BCodeAsmCommon.scala @@ -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) diff --git a/tests/run/i18701.check b/tests/run/i18701.check new file mode 100644 index 000000000000..2c0264028887 --- /dev/null +++ b/tests/run/i18701.check @@ -0,0 +1 @@ +public TB A$$anon$1.tb() diff --git a/tests/run/i18701.fixed.check b/tests/run/i18701.fixed.check new file mode 100644 index 000000000000..6d18ba0cbb0e --- /dev/null +++ b/tests/run/i18701.fixed.check @@ -0,0 +1 @@ +public TB A$$anon$2.apply() diff --git a/tests/run/i18701.fixed.scala b/tests/run/i18701.fixed.scala new file mode 100644 index 000000000000..361f3f40dc2b --- /dev/null +++ b/tests/run/i18701.fixed.scala @@ -0,0 +1,15 @@ +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() diff --git a/tests/run/i18701.scala b/tests/run/i18701.scala new file mode 100644 index 000000000000..45df41af8f06 --- /dev/null +++ b/tests/run/i18701.scala @@ -0,0 +1,14 @@ +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() From 0c20d86bf7aa37d4c40f57d0af15947dad9947f2 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Wed, 12 Jun 2024 13:13:54 +0100 Subject: [PATCH 2/2] Disable scalajs on tests --- tests/run/i18701.fixed.scala | 2 ++ tests/run/i18701.scala | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/run/i18701.fixed.scala b/tests/run/i18701.fixed.scala index 361f3f40dc2b..f0610380ad17 100644 --- a/tests/run/i18701.fixed.scala +++ b/tests/run/i18701.fixed.scala @@ -1,3 +1,5 @@ +// scalajs: --skip +// Use of Java reflection (getEnclosingMethod) abstract class TA { def tb(): TB } abstract class TB { def chk(): Unit } class A: diff --git a/tests/run/i18701.scala b/tests/run/i18701.scala index 45df41af8f06..50001233af10 100644 --- a/tests/run/i18701.scala +++ b/tests/run/i18701.scala @@ -1,3 +1,5 @@ +// scalajs: --skip +// Use of Java reflection (getEnclosingMethod) abstract class TA { def tb(): TB } abstract class TB { def chk(): Unit } class A: