From cd89fed8598b6d34716853484a354e26052de0bd Mon Sep 17 00:00:00 2001 From: Matt Bovel Date: Tue, 27 Feb 2024 19:15:07 +0100 Subject: [PATCH] Change isStatic to isStaticOwner in hasLocalInstantiation Co-Authored-By: Dale Wijnand Co-Authored-By: noti0na1 Co-Authored-By: odersky <795990+odersky@users.noreply.github.com> --- .../tools/dotc/transform/ExplicitOuter.scala | 2 +- tests/run/19569.scala | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 tests/run/19569.scala diff --git a/compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala b/compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala index f57595293ae1..e34dbfe3e1d8 100644 --- a/compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala +++ b/compiler/src/dotty/tools/dotc/transform/ExplicitOuter.scala @@ -227,7 +227,7 @@ object ExplicitOuter { private def hasLocalInstantiation(cls: ClassSymbol)(using Context): Boolean = // Modules are normally locally instantiated, except if they are declared in a trait, // in which case they will be instantiated in the classes that mix in the trait. - cls.owner.ownersIterator.takeWhile(!_.isStatic).exists(_.isTerm) + cls.owner.ownersIterator.takeWhile(!_.isStaticOwner).exists(_.isTerm) || cls.is(Private, butNot = Module) || cls.is(Module) && !cls.owner.is(Trait) diff --git a/tests/run/19569.scala b/tests/run/19569.scala new file mode 100644 index 000000000000..5f1b65fcfbca --- /dev/null +++ b/tests/run/19569.scala @@ -0,0 +1,27 @@ +object helper { + def assertNoParams(cls: Class[?]) = assert(cls.getConstructors()(0).getParameterCount == 0) +} +import helper.assertNoParams + +object T1 { class C1; assertNoParams(classOf[C1]) } +object T2 { new AnyRef { class C2; assertNoParams(classOf[C2]) } } +object T3 { def t3(): Unit = { class C3; assertNoParams(classOf[C3]) } } +object T4 { def t4(): Unit = new AnyRef { class C4; assertNoParams(classOf[C4]) } } + +//class T5 { class C5; assertNoParams(classOf[C5]) } +class T6 { new AnyRef { class C6; assertNoParams(classOf[C6]) } } +class T7 { def t7(): Unit = { class C7; assertNoParams(classOf[C7]) } } +class T8 { def t8(): Unit = new AnyRef { class C8; assertNoParams(classOf[C8]) } } + +object Test { + def main(args: Array[String]): Unit = { + T1.toString + T2.toString + T3.t3() + T4.t4() + //new T5().toString + new T6().toString + new T7().t7() + new T8().t8() + } +}