Skip to content

Add FromTasty compilation tests #3451

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 3 commits into from
Nov 12, 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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/CompilationUnit.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package dotty.tools
package dotc

import dotty.tools.dotc.core.Types.Type
import dotty.tools.dotc.core.tasty.{TastyUnpickler, TastyBuffer, TastyPickler}
import dotty.tools.dotc.core.Types.Type // Do not remove me #3383
import util.SourceFile
import ast.{tpd, untpd}
import dotty.tools.dotc.ast.tpd.{ Tree, TreeTraverser }
Expand All @@ -28,6 +27,7 @@ object CompilationUnit {

/** Make a compilation unit for top class `clsd` with the contends of the `unpickled` */
def mkCompilationUnit(clsd: ClassDenotation, unpickled: Tree, forceTrees: Boolean)(implicit ctx: Context): CompilationUnit = {
assert(!unpickled.isEmpty, unpickled)
val unit1 = new CompilationUnit(new SourceFile(clsd.symbol.sourceFile, Seq()))
unit1.tpdTree = unpickled
if (forceTrees)
Expand Down
63 changes: 38 additions & 25 deletions compiler/src/dotty/tools/dotc/FromTasty.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import SymDenotations._
import typer.FrontEnd
import Phases.Phase
import util._
import reporting.Reporter
import Decorators._
import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core._
import dotty.tools.dotc.core.Names._
import dotty.tools.dotc.core.NameOps._
import dotty.tools.dotc.transform.Pickler
import tasty.DottyUnpickler
import ast.tpd._
import NameKinds.QualifiedName

/** Compiler for TASTY files.
* Usage:
Expand Down Expand Up @@ -65,31 +65,44 @@ object FromTasty extends Driver {
override def isTyper = false

override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] =
units.map(readTASTY)
units.flatMap(readTASTY)

def readTASTY(unit: CompilationUnit)(implicit ctx: Context): CompilationUnit = unit match {
def readTASTY(unit: CompilationUnit)(implicit ctx: Context): List[CompilationUnit] = unit match {
case unit: TASTYCompilationUnit =>
assert(ctx.settings.YretainTrees.value)
val className = unit.className.toTypeName
val clsd = ctx.base.staticRef(className)
def cannotUnpickle(reason: String) = {
ctx.error(s"class $className cannot be unpickled because $reason")
unit
}
clsd match {
case clsd: ClassDenotation =>
clsd.infoOrCompleter match {
case info: ClassfileLoader =>
info.load(clsd)
val unpickled = clsd.symbol.asClass.tree
if (unpickled != null) CompilationUnit.mkCompilationUnit(clsd, unpickled, forceTrees = true)
else cannotUnpickle(s"its class file ${info.classfile} does not have a TASTY attribute")
case info =>
cannotUnpickle(s"its info of type ${info.getClass} is not a ClassfileLoader")
}
case _ =>
ctx.error(s"class not found: $className")
unit
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
}
compilationUnits
}

private def tree(className: TypeName)(implicit ctx: Context): Option[(ClassDenotation, tpd.Tree)] = {
val clsd = ctx.base.staticRef(className)
ctx.base.staticRef(className) match {
case clsd: ClassDenotation =>
def cannotUnpickle(reason: String) =
ctx.error(s"class $className cannot be unpickled because $reason")
def tryToLoad = clsd.infoOrCompleter match {
case info: ClassfileLoader =>
info.load(clsd)
Option(clsd.symbol.asClass.tree).orElse {
cannotUnpickle(s"its class file ${info.classfile} does not have a TASTY attribute")
None
}

case info =>
cannotUnpickle(s"its info of type ${info.getClass} is not a ClassfileLoader")
None
}
Option(clsd.symbol.asClass.tree).orElse(tryToLoad).map(tree => (clsd, tree))

case _ =>
ctx.error(s"class not found: $className")
None
}
}
}
}
56 changes: 56 additions & 0 deletions compiler/test/dotty/tools/dotc/FromTastyTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package dotty
package tools
package dotc

import org.junit.{AfterClass, Test}
import vulpix._

import scala.concurrent.duration._

class FromTastyTests extends ParallelTesting {
import TestConfiguration._
import FromTastyTests._

// Test suite configuration --------------------------------------------------

def maxDuration = 30.seconds
def numberOfSlaves = 5
def safeMode = Properties.testsSafeMode
def isInteractive = SummaryReport.isInteractive
def testFilter = Properties.testsFilter


@Test def posTestFromTasty: Unit = {
implicit val testGroup: TestGroup = TestGroup("posTestFromTasty")
val (step1, step2) = {
// compileTastyInDir("../tests/pos", defaultOptions) + // FIXME
compileTastyInDir("../tests/pos-from-tasty", defaultOptions) +
compileTasty("../tests/pos-from-tasty/simpleClass.scala", defaultOptions)
}
step1.checkCompile() // Compile all files to generate the class files with tasty
step2.checkCompile() // Compile from tasty
(step1 + step2).delete()
}

@Test def runTestFromTasty: Unit = {
implicit val testGroup: TestGroup = TestGroup("runTestFromTasty")
val (step1, step2) = {
// compileTastyInDir("../tests/run", defaultOptions) + // FIXME
compileTastyInDir("../tests/run-from-tasty", defaultOptions) +
compileTasty("../tests/run/t493.scala", defaultOptions)
}
step1.checkCompile() // Compile all files to generate the class files with tasty
step2.checkRuns() // Compile from tasty and run the result
(step1 + step2).delete()
}

private implicit class tastyCompilationTuples(tup: (CompilationTest, CompilationTest)) {
def +(that: (CompilationTest, CompilationTest)): (CompilationTest, CompilationTest) =
(tup._1 + that._1, tup._2 + that._2)
}
}

object FromTastyTests {
implicit val summaryReport: SummaryReporting = new SummaryReport
@AfterClass def cleanup(): Unit = summaryReport.echoSummary()
}
Loading