Skip to content

Add -Ythrough-tasty to debug from TASTY compilation direct from sources #3786

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 5 commits into from
Jan 13, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
67 changes: 67 additions & 0 deletions compiler/src/dotty/tools/dotc/fromtasty/Debug.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dotty.tools
package dotc
package fromtasty

import scala.util.control.NonFatal

import dotty.tools.io.Directory

import java.nio.file.{Files, Paths}

object Debug {
def main(args: Array[String]): Unit = {
// Preload scala.util.control.NonFatal. Otherwise, when trying to catch a StackOverflowError,
// we may try to load it but fail with another StackOverflowError and lose the original exception,
// see <https://groups.google.com/forum/#!topic/scala-user/kte6nak-zPM>.
val _ = NonFatal

assert(!args.contains("-d"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this tool support the -d option? I think it would nice. Maybe use it as the base director instead of out if specified

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone needs that feature in the future, he can implement it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would actually be unsafe. For the test to be reproducible the output directory needs to be empty.


val outPath = Paths.get("out")
Directory(outPath).createDirectory()

val tmpOut = Files.createTempDirectory(outPath.toAbsolutePath, "from-tasty-tmp")

val fromSourcesOut = Files.createDirectory(tmpOut.resolve("from-source"))

println(s"Compiling .scala")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you pointed out to me that temporary directory are not deleted on exit, I would revert to you previous version: s"Compiling from sources to $fromSourcesOut"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Compiling from source..."? Also the s interpolator is not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change it to Compiling from .scala sources as in the second step the sources are the .class files.
I will change the second one to Compiling TASTY from .class sources to also be explicit.

val compilation1 = dotc.Main.process("-d" +: fromSourcesOut.toString +: args)

if (compilation1.hasErrors) {
println("Failed compilation from sources")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this. If the compilation failed, the compiler already tells you "error found"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would not tell me which of the two compilation failed.

sys.exit(1)
}

val fromTastyOut = Files.createDirectory(tmpOut.resolve("from-tasty"))

val ext = "hasTasty"
val classes = Directory(fromSourcesOut).walk.filter(x => x.isFile && x.extension == ext).map { x =>
val source = x.toString
source.substring(fromSourcesOut.toString.length + 1, source.length - ext.length - 1).replace('/', '.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a comment to explain what this does

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}.toList

val fromTastyArgs = {
"-from-tasty" ::
"-d" :: fromTastyOut.toString ::
insertClasspathInArgs(args.filterNot(_.endsWith(".scala")).toList, fromSourcesOut.toString) :::
classes
}

println(s"Compiling TASTY")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Compiling from TASTY..."? Also the s interpolator is not needed

val compilation2 = dotc.Main.process(fromTastyArgs.toArray)

if (compilation2.hasErrors) {
println("Failed compilation from TASTY")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this. If the compilation failed, the compiler already tells you "error found"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue is that the errors will not tell you which of the compilation failed. I prefer to be explicit, so anyone that uses the tool know exactly what is happening.

println("Compilation input: " + fromSourcesOut)
sys.exit(1)
}

Directory(tmpOut).deleteRecursively()
}

private def insertClasspathInArgs(args: List[String], cp: String): List[String] = {
val (beforeCp, fromCp) = args.span(_ != "-classpath")
val classpath = fromCp.drop(1).headOption.fold(cp)(_ + ":" + cp)
"-classpath" :: classpath :: beforeCp ::: fromCp.drop(2)
}
}
4 changes: 3 additions & 1 deletion project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,13 @@ object Build {
val dottyLib = packageAll.value("dotty-library")
val args0: List[String] = spaceDelimited("<arg>").parsed.toList
val decompile = args0.contains("-decompile")
val args = args0.filter(arg => arg != "-repl" || arg != "-decompile")
val debugFromTasty = args0.contains("-Ythrough-tasty")
val args = args0.filter(arg => arg != "-repl" && arg != "-decompile" && arg != "-Ythrough-tasty")

val main =
if (repl) "dotty.tools.repl.Main"
else if (decompile) "dotty.tools.dotc.decompiler.Main"
else if (debugFromTasty) "dotty.tools.dotc.fromtasty.Debug"
else "dotty.tools.dotc.Main"

val extraClasspath =
Expand Down