Skip to content

Correctly unpickle Scala 2 private case classes in traits #16519

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
Dec 14, 2022
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
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/core/NamerOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ object NamerOps:
completer.withSourceModule(findModuleBuddy(name.sourceModuleName, scope))

/** Find moduleClass/sourceModule in effective scope */
def findModuleBuddy(name: Name, scope: Scope)(using Context) = {
val it = scope.lookupAll(name).filter(_.is(Module))
if (it.hasNext) it.next()
def findModuleBuddy(name: Name, scope: Scope, alternate: Name = EmptyTermName)(using Context): Symbol =
var it = scope.lookupAll(name).filter(_.is(Module))
if !alternate.isEmpty then it ++= scope.lookupAll(alternate).filter(_.is(Module))
if it.hasNext then it.next()
else NoSymbol.assertingErrorsReported(em"no companion $name in $scope")
}

/** If a class has one of these flags, it does not get a constructor companion */
private val NoConstructorProxyNeededFlags = Abstract | Trait | Case | Synthetic | Module | Invisible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ object Scala2Unpickler {
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)
val modName = cls.name.sourceModuleName
val alternate =
if cls.privateWithin.exists && cls.owner.is(Trait) then modName.expandedName(cls.owner)
else EmptyTermName
NamerOps.findModuleBuddy(modName, denot.owner.info.decls, alternate)
}
denot.owner.thisType.select(sourceModule)
else selfInfo
Expand Down
2 changes: 2 additions & 0 deletions tests/pos/i16443/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@main def Test =
println(NoTypeHints)
15 changes: 15 additions & 0 deletions tests/pos/i16443/TypeHints_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This should be run with Scala 2.13
trait TypeHints {
val hints: List[Class[_]]
def components: List[TypeHints] = List(this)

def + (hints: TypeHints): TypeHints = CompositeTypeHints(components ::: hints.components)

private case class CompositeTypeHints(override val components: List[TypeHints]) extends TypeHints {
override val hints: List[Class[_]] = components.flatMap(_.hints)
}
}

case object NoTypeHints extends TypeHints {
override val hints = Nil
}