Skip to content

Avoid loading compilation units twice #3461

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
Nov 15, 2017
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
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