Skip to content

Commit 4cd1122

Browse files
Merge pull request #3786 from dotty-staging/add-from-tasty-debug
Add -Ythrough-tasty to debug from TASTY compilation direct from sources
2 parents df85625 + 4b1385e commit 4cd1122

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package dotty.tools
2+
package dotc
3+
package fromtasty
4+
5+
import scala.util.control.NonFatal
6+
7+
import dotty.tools.io.Directory
8+
9+
import java.nio.file.{Files, Paths}
10+
11+
object Debug {
12+
def main(args: Array[String]): Unit = {
13+
// Preload scala.util.control.NonFatal. Otherwise, when trying to catch a StackOverflowError,
14+
// we may try to load it but fail with another StackOverflowError and lose the original exception,
15+
// see <https://groups.google.com/forum/#!topic/scala-user/kte6nak-zPM>.
16+
val _ = NonFatal
17+
18+
assert(!args.contains("-d"))
19+
20+
val outPath = Paths.get("out")
21+
Directory(outPath).createDirectory()
22+
23+
val tmpOut = Files.createTempDirectory(outPath.toAbsolutePath, "from-tasty-tmp")
24+
25+
val fromSourcesOut = Files.createDirectory(tmpOut.resolve("from-source"))
26+
27+
println("Compiling from .scala sources")
28+
val compilation1 = dotc.Main.process("-d" +: fromSourcesOut.toString +: args)
29+
30+
if (compilation1.hasErrors) {
31+
println("Failed compilation from sources")
32+
Directory(tmpOut).deleteRecursively()
33+
sys.exit(1)
34+
}
35+
36+
val fromTastyOut = Files.createDirectory(tmpOut.resolve("from-tasty"))
37+
38+
val ext = "hasTasty"
39+
val classes = Directory(fromSourcesOut).walk.filter(x => x.isFile && x.extension == ext).map { x =>
40+
val source = x.toString
41+
// transform foo/bar/Baz.hasTasty into foo.bar.Baz
42+
source.substring(fromSourcesOut.toString.length + 1, source.length - ext.length - 1).replace('/', '.')
43+
}.toList
44+
45+
val fromTastyArgs = {
46+
"-from-tasty" ::
47+
"-d" :: fromTastyOut.toString ::
48+
insertClasspathInArgs(args.filterNot(_.endsWith(".scala")).toList, fromSourcesOut.toString) :::
49+
classes
50+
}
51+
52+
println("Compiling TASTY from .class sources")
53+
val compilation2 = dotc.Main.process(fromTastyArgs.toArray)
54+
55+
if (compilation2.hasErrors) {
56+
println("Failed compilation from TASTY")
57+
println("Compilation input: " + fromSourcesOut)
58+
// In this case we do not delete the generated class files to allow further debugging.
59+
// For example `dotc -decompile` on one of the intermediate class files.
60+
sys.exit(1)
61+
}
62+
63+
Directory(tmpOut).deleteRecursively()
64+
}
65+
66+
private def insertClasspathInArgs(args: List[String], cp: String): List[String] = {
67+
val (beforeCp, fromCp) = args.span(_ != "-classpath")
68+
val classpath = fromCp.drop(1).headOption.fold(cp)(_ + ":" + cp)
69+
"-classpath" :: classpath :: beforeCp ::: fromCp.drop(2)
70+
}
71+
}

project/Build.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,11 +630,13 @@ object Build {
630630
val dottyLib = packageAll.value("dotty-library")
631631
val args0: List[String] = spaceDelimited("<arg>").parsed.toList
632632
val decompile = args0.contains("-decompile")
633-
val args = args0.filter(arg => arg != "-repl" || arg != "-decompile")
633+
val debugFromTasty = args0.contains("-Ythrough-tasty")
634+
val args = args0.filter(arg => arg != "-repl" && arg != "-decompile" && arg != "-Ythrough-tasty")
634635

635636
val main =
636637
if (repl) "dotty.tools.repl.Main"
637638
else if (decompile) "dotty.tools.dotc.decompiler.Main"
639+
else if (debugFromTasty) "dotty.tools.dotc.fromtasty.Debug"
638640
else "dotty.tools.dotc.Main"
639641

640642
val extraClasspath =

0 commit comments

Comments
 (0)