Skip to content

Commit

Permalink
Merge pull request #3461 from dotty-staging/fix-case-class-from-tasty
Browse files Browse the repository at this point in the history
Avoid loading compilation units twice
  • Loading branch information
nicolasstucki committed Nov 15, 2017
2 parents f728134 + 723993e commit 7759e00
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
18 changes: 12 additions & 6 deletions compiler/src/dotty/tools/dotc/FromTasty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,22 @@ object FromTasty extends Driver {
override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] =
units.flatMap(readTASTY)

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

private def tree(className: TypeName)(implicit ctx: Context): Option[(ClassDenotation, tpd.Tree)] = {
Expand Down
3 changes: 3 additions & 0 deletions tests/pos-from-tasty/simpleCaseObject.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package foo

case object Foo
4 changes: 4 additions & 0 deletions tests/pos-from-tasty/simpleClassWithObject.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package foo

class Foo
object Foo

0 comments on commit 7759e00

Please sign in to comment.