Skip to content

Commit 723993e

Browse files
committed
Avoid loading compilation units twice
For some class name foo.Foo we need to try to load the tasty from the its class. If the class is not definded, we try load it from the obeject. Previously, if both the class and the object extists we were loading the compilation unit twice. Which would duplicate symbols and then fail with duplicated symbol definitions.
1 parent f728134 commit 723993e

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

compiler/src/dotty/tools/dotc/FromTasty.scala

+12-6
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,22 @@ object FromTasty extends Driver {
6767
override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] =
6868
units.flatMap(readTASTY)
6969

70-
def readTASTY(unit: CompilationUnit)(implicit ctx: Context): List[CompilationUnit] = unit match {
70+
def readTASTY(unit: CompilationUnit)(implicit ctx: Context): Option[CompilationUnit] = unit match {
7171
case unit: TASTYCompilationUnit =>
7272
assert(ctx.settings.YretainTrees.value)
7373
val className = unit.className.toTypeName
74-
val compilationUnits = List(tree(className), tree(className.moduleClassName)).flatMap {
75-
case Some((clsd, unpickled)) if !unpickled.isEmpty =>
76-
List(CompilationUnit.mkCompilationUnit(clsd, unpickled, forceTrees = true))
77-
case _ => Nil
74+
def compilationUnit(className: TypeName): Option[CompilationUnit] = {
75+
tree(className).flatMap { case (clsd, unpickled) =>
76+
if (unpickled.isEmpty) None
77+
else Some(CompilationUnit.mkCompilationUnit(clsd, unpickled, forceTrees = true))
78+
}
7879
}
79-
compilationUnits
80+
// The TASTY section in a/b/C.class may either contain a class a.b.C, an object a.b.C, or both.
81+
// We first try to load the class and fallback to loading the object if the class doesn't exist.
82+
// Note that if both the class and the object are present, then loading the class will also load
83+
// the object, this is why we use orElse here, otherwise we could load the object twice and
84+
// create ambiguities!
85+
compilationUnit(className).orElse(compilationUnit(className.moduleClassName))
8086
}
8187

8288
private def tree(className: TypeName)(implicit ctx: Context): Option[(ClassDenotation, tpd.Tree)] = {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package foo
2+
3+
case object Foo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package foo
2+
3+
class Foo
4+
object Foo

0 commit comments

Comments
 (0)