Skip to content

Fix #9916: Properly unpickle non-toplevel Scala 2 objects #10006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/NamerOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
end NamerOps
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions sbt-dotty/sbt-test/scala2-compat/i9916a/build.sbt
Original file line number Diff line number Diff line change
@@ -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))
}
)
7 changes: 7 additions & 0 deletions sbt-dotty/sbt-test/scala2-compat/i9916a/lib/i9916a-lib.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package i9916a

object Lib {
trait P
object P
def P(x: Int): P = ???
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import i9916a.Lib

trait Test {
def foo: Lib.P
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % sys.props("plugin.version"))
1 change: 1 addition & 0 deletions sbt-dotty/sbt-test/scala2-compat/i9916a/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> i9916a-test/compile
15 changes: 15 additions & 0 deletions sbt-dotty/sbt-test/scala2-compat/i9916b/build.sbt
Original file line number Diff line number Diff line change
@@ -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))
}
)
7 changes: 7 additions & 0 deletions sbt-dotty/sbt-test/scala2-compat/i9916b/lib/i9916b-lib.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package i9916b

trait T {
class A
object A
def A(x: Int): Unit = ???
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import i9916b.T

trait Test {
def foo: T
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("ch.epfl.lamp" % "sbt-dotty" % sys.props("plugin.version"))
1 change: 1 addition & 0 deletions sbt-dotty/sbt-test/scala2-compat/i9916b/test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
> i9916b-test/compile