Skip to content

Commit c856b9a

Browse files
authored
Merge pull request #10006 from dotty-staging/fix-scala2-object-selftype
Fix #9916: Properly unpickle non-toplevel Scala 2 objects
2 parents a5f8e2b + 448af4c commit c856b9a

File tree

12 files changed

+67
-7
lines changed

12 files changed

+67
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ 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 = denot.sourceModule.orElse {
79+
// For non-toplevel modules, `sourceModule` won't be set when completing
80+
// the module class, we need to go find it ourselves.
81+
NamerOps.findModuleBuddy(cls.name.sourceModuleName, denot.owner.info.decls)
82+
}
83+
denot.owner.thisType.select(sourceModule)
8284
else selfInfo
8385
val tempInfo = new TempClassInfo(denot.owner.thisType, cls, decls, ost)
8486
denot.info = tempInfo // first rough info to avoid CyclicReferences
Lines changed: 15 additions & 0 deletions
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+
)
Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 5 additions & 0 deletions
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % sys.props("plugin.version"))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
> i9916a-test/compile
Lines changed: 15 additions & 0 deletions
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+
)
Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 5 additions & 0 deletions
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+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % sys.props("plugin.version"))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
> i9916b-test/compile

0 commit comments

Comments
 (0)