Skip to content

Commit 634f070

Browse files
committed
Fix #9916: Properly unpickle non-toplevel Scala 2 objects
Test cases from #9976.
1 parent ca67e4d commit 634f070

File tree

12 files changed

+68
-7
lines changed

12 files changed

+68
-7
lines changed

compiler/src/dotty/tools/dotc/core/NamerOps.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ object NamerOps:
5151
completer.withSourceModule(findModuleBuddy(name.sourceModuleName, scope))
5252

5353
/** Find moduleClass/sourceModule in effective scope */
54-
private def findModuleBuddy(name: Name, scope: Scope)(using Context) = {
54+
def findModuleBuddy(name: Name, scope: Scope)(using Context) = {
5555
val it = scope.lookupAll(name).filter(_.is(Module))
5656
if (it.hasNext) it.next()
5757
else NoSymbol.assertingErrorsReported(s"no companion $name in $scope")
5858
}
5959

60-
end NamerOps
60+
end NamerOps

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

+8-5
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ object Scala2Unpickler {
7474
case cinfo => (Nil, cinfo)
7575
}
7676
val ost =
77-
if ((selfInfo eq NoType) && denot.is(ModuleClass) && denot.sourceModule.exists)
78-
// it seems sometimes the source module does not exist for a module class.
79-
// An example is `scala.reflect.internal.Trees.Template$. Without the
80-
// `denot.sourceModule.exists` provision i859.scala crashes in the backend.
81-
denot.owner.thisType select denot.sourceModule
77+
if (selfInfo eq NoType) && denot.is(ModuleClass) then
78+
val sourceModule =
79+
denot.sourceModule.orElse {
80+
// For non-toplevel modules, `sourceModule` won't be set when completing
81+
// the module class, we need to go find it ourselves.
82+
NamerOps.findModuleBuddy(cls.name.sourceModuleName, denot.owner.info.decls)
83+
}
84+
denot.owner.thisType.select(sourceModule)
8285
else selfInfo
8386
val tempInfo = new TempClassInfo(denot.owner.thisType, cls, decls, ost)
8487
denot.info = tempInfo // first rough info to avoid CyclicReferences
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
val scala3Version = sys.props("plugin.scalaVersion")
2+
val scala2Version = "2.13.3"
3+
4+
lazy val `i9916a-lib` = (project in file ("lib"))
5+
.settings(scalaVersion := scala2Version)
6+
7+
lazy val `i9916a-test` = (project in file ("main"))
8+
.dependsOn(`i9916a-lib`)
9+
.settings(
10+
scalaVersion := scala3Version,
11+
// https://github.com/sbt/sbt/issues/5369
12+
projectDependencies := {
13+
projectDependencies.value.map(_.withDottyCompat(scalaVersion.value))
14+
}
15+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package i9916a
2+
3+
object Lib {
4+
trait P
5+
object P
6+
def P(x: Int): P = ???
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import i9916a.Lib
2+
3+
trait Test {
4+
def foo: Lib.P
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % sys.props("plugin.version"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
> i9916a-test/compile
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
val scala3Version = sys.props("plugin.scalaVersion")
2+
val scala2Version = "2.13.3"
3+
4+
lazy val `i9916b-lib` = (project in file ("lib"))
5+
.settings(scalaVersion := scala2Version)
6+
7+
lazy val `i9916b-test` = (project in file ("main"))
8+
.dependsOn(`i9916b-lib`)
9+
.settings(
10+
scalaVersion := scala3Version,
11+
// https://github.com/sbt/sbt/issues/5369
12+
projectDependencies := {
13+
projectDependencies.value.map(_.withDottyCompat(scalaVersion.value))
14+
}
15+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package i9916b
2+
3+
trait T {
4+
class A
5+
object A
6+
def A(x: Int): Unit = ???
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import i9916b.T
2+
3+
trait Test {
4+
def foo: T
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % sys.props("plugin.version"))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
> i9916b-test/compile

0 commit comments

Comments
 (0)