diff --git a/compiler/src/dotty/tools/dotc/core/NamerOps.scala b/compiler/src/dotty/tools/dotc/core/NamerOps.scala index 2131c509ed7e..8b54c74ca122 100644 --- a/compiler/src/dotty/tools/dotc/core/NamerOps.scala +++ b/compiler/src/dotty/tools/dotc/core/NamerOps.scala @@ -51,10 +51,10 @@ object NamerOps: completer.withSourceModule(findModuleBuddy(name.sourceModuleName, scope)) /** Find moduleClass/sourceModule in effective scope */ - private def findModuleBuddy(name: Name, scope: Scope)(using Context) = { + def findModuleBuddy(name: Name, scope: Scope)(using Context) = { val it = scope.lookupAll(name).filter(_.is(Module)) if (it.hasNext) it.next() else NoSymbol.assertingErrorsReported(s"no companion $name in $scope") } -end NamerOps \ No newline at end of file +end NamerOps diff --git a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala index 6bcd689a71a3..e138a5fc89c0 100644 --- a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala @@ -74,11 +74,13 @@ object Scala2Unpickler { case cinfo => (Nil, cinfo) } val ost = - if ((selfInfo eq NoType) && denot.is(ModuleClass) && denot.sourceModule.exists) - // it seems sometimes the source module does not exist for a module class. - // An example is `scala.reflect.internal.Trees.Template$. Without the - // `denot.sourceModule.exists` provision i859.scala crashes in the backend. - denot.owner.thisType select denot.sourceModule + if (selfInfo eq NoType) && denot.is(ModuleClass) then + val sourceModule = denot.sourceModule.orElse { + // For non-toplevel modules, `sourceModule` won't be set when completing + // the module class, we need to go find it ourselves. + NamerOps.findModuleBuddy(cls.name.sourceModuleName, denot.owner.info.decls) + } + denot.owner.thisType.select(sourceModule) else selfInfo val tempInfo = new TempClassInfo(denot.owner.thisType, cls, decls, ost) denot.info = tempInfo // first rough info to avoid CyclicReferences diff --git a/sbt-dotty/sbt-test/scala2-compat/i9916a/build.sbt b/sbt-dotty/sbt-test/scala2-compat/i9916a/build.sbt new file mode 100644 index 000000000000..e9c732d4853a --- /dev/null +++ b/sbt-dotty/sbt-test/scala2-compat/i9916a/build.sbt @@ -0,0 +1,15 @@ +val scala3Version = sys.props("plugin.scalaVersion") +val scala2Version = "2.13.3" + +lazy val `i9916a-lib` = (project in file ("lib")) + .settings(scalaVersion := scala2Version) + +lazy val `i9916a-test` = (project in file ("main")) + .dependsOn(`i9916a-lib`) + .settings( + scalaVersion := scala3Version, + // https://github.com/sbt/sbt/issues/5369 + projectDependencies := { + projectDependencies.value.map(_.withDottyCompat(scalaVersion.value)) + } + ) diff --git a/sbt-dotty/sbt-test/scala2-compat/i9916a/lib/i9916a-lib.scala b/sbt-dotty/sbt-test/scala2-compat/i9916a/lib/i9916a-lib.scala new file mode 100644 index 000000000000..44882333a2f5 --- /dev/null +++ b/sbt-dotty/sbt-test/scala2-compat/i9916a/lib/i9916a-lib.scala @@ -0,0 +1,7 @@ +package i9916a + +object Lib { + trait P + object P + def P(x: Int): P = ??? +} diff --git a/sbt-dotty/sbt-test/scala2-compat/i9916a/main/i9916a-test.scala b/sbt-dotty/sbt-test/scala2-compat/i9916a/main/i9916a-test.scala new file mode 100644 index 000000000000..1b10c3504ca7 --- /dev/null +++ b/sbt-dotty/sbt-test/scala2-compat/i9916a/main/i9916a-test.scala @@ -0,0 +1,5 @@ +import i9916a.Lib + +trait Test { + def foo: Lib.P +} diff --git a/sbt-dotty/sbt-test/scala2-compat/i9916a/project/plugins.sbt b/sbt-dotty/sbt-test/scala2-compat/i9916a/project/plugins.sbt new file mode 100644 index 000000000000..c17caab2d98c --- /dev/null +++ b/sbt-dotty/sbt-test/scala2-compat/i9916a/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % sys.props("plugin.version")) diff --git a/sbt-dotty/sbt-test/scala2-compat/i9916a/test b/sbt-dotty/sbt-test/scala2-compat/i9916a/test new file mode 100644 index 000000000000..2a848354ed12 --- /dev/null +++ b/sbt-dotty/sbt-test/scala2-compat/i9916a/test @@ -0,0 +1 @@ +> i9916a-test/compile diff --git a/sbt-dotty/sbt-test/scala2-compat/i9916b/build.sbt b/sbt-dotty/sbt-test/scala2-compat/i9916b/build.sbt new file mode 100644 index 000000000000..51a2d056ddad --- /dev/null +++ b/sbt-dotty/sbt-test/scala2-compat/i9916b/build.sbt @@ -0,0 +1,15 @@ +val scala3Version = sys.props("plugin.scalaVersion") +val scala2Version = "2.13.3" + +lazy val `i9916b-lib` = (project in file ("lib")) + .settings(scalaVersion := scala2Version) + +lazy val `i9916b-test` = (project in file ("main")) + .dependsOn(`i9916b-lib`) + .settings( + scalaVersion := scala3Version, + // https://github.com/sbt/sbt/issues/5369 + projectDependencies := { + projectDependencies.value.map(_.withDottyCompat(scalaVersion.value)) + } + ) diff --git a/sbt-dotty/sbt-test/scala2-compat/i9916b/lib/i9916b-lib.scala b/sbt-dotty/sbt-test/scala2-compat/i9916b/lib/i9916b-lib.scala new file mode 100644 index 000000000000..0ce017246f86 --- /dev/null +++ b/sbt-dotty/sbt-test/scala2-compat/i9916b/lib/i9916b-lib.scala @@ -0,0 +1,7 @@ +package i9916b + +trait T { + class A + object A + def A(x: Int): Unit = ??? +} diff --git a/sbt-dotty/sbt-test/scala2-compat/i9916b/main/i9916b-test.scala b/sbt-dotty/sbt-test/scala2-compat/i9916b/main/i9916b-test.scala new file mode 100644 index 000000000000..fb702d1a8617 --- /dev/null +++ b/sbt-dotty/sbt-test/scala2-compat/i9916b/main/i9916b-test.scala @@ -0,0 +1,5 @@ +import i9916b.T + +trait Test { + def foo: T +} diff --git a/sbt-dotty/sbt-test/scala2-compat/i9916b/project/plugins.sbt b/sbt-dotty/sbt-test/scala2-compat/i9916b/project/plugins.sbt new file mode 100644 index 000000000000..c17caab2d98c --- /dev/null +++ b/sbt-dotty/sbt-test/scala2-compat/i9916b/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % sys.props("plugin.version")) diff --git a/sbt-dotty/sbt-test/scala2-compat/i9916b/test b/sbt-dotty/sbt-test/scala2-compat/i9916b/test new file mode 100644 index 000000000000..064736132510 --- /dev/null +++ b/sbt-dotty/sbt-test/scala2-compat/i9916b/test @@ -0,0 +1 @@ +> i9916b-test/compile