Skip to content

Commit b9f62ac

Browse files
Merge pull request #3451 from dotty-staging/from-tasty-tests
Add FromTasty compilation tests
2 parents c272732 + 9f76f82 commit b9f62ac

File tree

10 files changed

+254
-52
lines changed

10 files changed

+254
-52
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package dotty.tools
22
package dotc
33

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

2928
/** Make a compilation unit for top class `clsd` with the contends of the `unpickled` */
3029
def mkCompilationUnit(clsd: ClassDenotation, unpickled: Tree, forceTrees: Boolean)(implicit ctx: Context): CompilationUnit = {
30+
assert(!unpickled.isEmpty, unpickled)
3131
val unit1 = new CompilationUnit(new SourceFile(clsd.symbol.sourceFile, Seq()))
3232
unit1.tpdTree = unpickled
3333
if (forceTrees)

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

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import SymDenotations._
1212
import typer.FrontEnd
1313
import Phases.Phase
1414
import util._
15-
import reporting.Reporter
1615
import Decorators._
16+
import dotty.tools.dotc.ast.tpd
17+
import dotty.tools.dotc.core._
18+
import dotty.tools.dotc.core.Names._
19+
import dotty.tools.dotc.core.NameOps._
1720
import dotty.tools.dotc.transform.Pickler
18-
import tasty.DottyUnpickler
19-
import ast.tpd._
20-
import NameKinds.QualifiedName
2121

2222
/** Compiler for TASTY files.
2323
* Usage:
@@ -65,31 +65,44 @@ object FromTasty extends Driver {
6565
override def isTyper = false
6666

6767
override def runOn(units: List[CompilationUnit])(implicit ctx: Context): List[CompilationUnit] =
68-
units.map(readTASTY)
68+
units.flatMap(readTASTY)
6969

70-
def readTASTY(unit: CompilationUnit)(implicit ctx: Context): CompilationUnit = unit match {
70+
def readTASTY(unit: CompilationUnit)(implicit ctx: Context): List[CompilationUnit] = unit match {
7171
case unit: TASTYCompilationUnit =>
72+
assert(ctx.settings.YretainTrees.value)
7273
val className = unit.className.toTypeName
73-
val clsd = ctx.base.staticRef(className)
74-
def cannotUnpickle(reason: String) = {
75-
ctx.error(s"class $className cannot be unpickled because $reason")
76-
unit
77-
}
78-
clsd match {
79-
case clsd: ClassDenotation =>
80-
clsd.infoOrCompleter match {
81-
case info: ClassfileLoader =>
82-
info.load(clsd)
83-
val unpickled = clsd.symbol.asClass.tree
84-
if (unpickled != null) CompilationUnit.mkCompilationUnit(clsd, unpickled, forceTrees = true)
85-
else cannotUnpickle(s"its class file ${info.classfile} does not have a TASTY attribute")
86-
case info =>
87-
cannotUnpickle(s"its info of type ${info.getClass} is not a ClassfileLoader")
88-
}
89-
case _ =>
90-
ctx.error(s"class not found: $className")
91-
unit
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
9278
}
79+
compilationUnits
80+
}
81+
82+
private def tree(className: TypeName)(implicit ctx: Context): Option[(ClassDenotation, tpd.Tree)] = {
83+
val clsd = ctx.base.staticRef(className)
84+
ctx.base.staticRef(className) match {
85+
case clsd: ClassDenotation =>
86+
def cannotUnpickle(reason: String) =
87+
ctx.error(s"class $className cannot be unpickled because $reason")
88+
def tryToLoad = clsd.infoOrCompleter match {
89+
case info: ClassfileLoader =>
90+
info.load(clsd)
91+
Option(clsd.symbol.asClass.tree).orElse {
92+
cannotUnpickle(s"its class file ${info.classfile} does not have a TASTY attribute")
93+
None
94+
}
95+
96+
case info =>
97+
cannotUnpickle(s"its info of type ${info.getClass} is not a ClassfileLoader")
98+
None
99+
}
100+
Option(clsd.symbol.asClass.tree).orElse(tryToLoad).map(tree => (clsd, tree))
101+
102+
case _ =>
103+
ctx.error(s"class not found: $className")
104+
None
105+
}
93106
}
94107
}
95108
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package dotty
2+
package tools
3+
package dotc
4+
5+
import org.junit.{AfterClass, Test}
6+
import vulpix._
7+
8+
import scala.concurrent.duration._
9+
10+
class FromTastyTests extends ParallelTesting {
11+
import TestConfiguration._
12+
import FromTastyTests._
13+
14+
// Test suite configuration --------------------------------------------------
15+
16+
def maxDuration = 30.seconds
17+
def numberOfSlaves = 5
18+
def safeMode = Properties.testsSafeMode
19+
def isInteractive = SummaryReport.isInteractive
20+
def testFilter = Properties.testsFilter
21+
22+
23+
@Test def posTestFromTasty: Unit = {
24+
implicit val testGroup: TestGroup = TestGroup("posTestFromTasty")
25+
val (step1, step2) = {
26+
// compileTastyInDir("../tests/pos", defaultOptions) + // FIXME
27+
compileTastyInDir("../tests/pos-from-tasty", defaultOptions) +
28+
compileTasty("../tests/pos-from-tasty/simpleClass.scala", defaultOptions)
29+
}
30+
step1.checkCompile() // Compile all files to generate the class files with tasty
31+
step2.checkCompile() // Compile from tasty
32+
(step1 + step2).delete()
33+
}
34+
35+
@Test def runTestFromTasty: Unit = {
36+
implicit val testGroup: TestGroup = TestGroup("runTestFromTasty")
37+
val (step1, step2) = {
38+
// compileTastyInDir("../tests/run", defaultOptions) + // FIXME
39+
compileTastyInDir("../tests/run-from-tasty", defaultOptions) +
40+
compileTasty("../tests/run/t493.scala", defaultOptions)
41+
}
42+
step1.checkCompile() // Compile all files to generate the class files with tasty
43+
step2.checkRuns() // Compile from tasty and run the result
44+
(step1 + step2).delete()
45+
}
46+
47+
private implicit class tastyCompilationTuples(tup: (CompilationTest, CompilationTest)) {
48+
def +(that: (CompilationTest, CompilationTest)): (CompilationTest, CompilationTest) =
49+
(tup._1 + that._1, tup._2 + that._2)
50+
}
51+
}
52+
53+
object FromTastyTests {
54+
implicit val summaryReport: SummaryReporting = new SummaryReport
55+
@AfterClass def cleanup(): Unit = summaryReport.echoSummary()
56+
}

0 commit comments

Comments
 (0)